Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose a way to style constants #17

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Added `bullet_stream::important_lit!` macro for colorizing constants (https://github.com/schneems/bullet_stream/pull/17)

## v0.3.0 - 2024/08/14

- Added `bullet_stream::strip_ansi` (https://github.com/schneems/bullet_stream/pull/11)
Expand Down
53 changes: 53 additions & 0 deletions src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,56 @@ pub fn details(contents: impl AsRef<str>) -> String {
pub fn important(contents: impl AsRef<str>) -> String {
ansi_escape::wrap_ansi_escape_each_line(&ANSI::BoldCyan, contents)
}

// Style macros defined here, but due to the way that #[macro_export] works they're defined
// on the top level module
mod macros {
/// Colorize important text literals
///
/// Wraps "important" color around a plain string literal. The main purpose is to be used in
/// constants when used with a string literal:
///
/// ```rust
/// use bullet_stream::important_lit;
///
/// const DEBUG_INFO: &str = important_lit!("Debug info:");
/// # assert_eq!(DEBUG_INFO, "\u{1b}[1;36mDebug info:\u{1b}[0m");
/// ```
///
/// It does NOT include any other logic such as preserving other colors, or handling newlines.
/// If you need newlines in your constant you should use the concat! macro:
///
/// ```rust
/// use bullet_stream::important_lit;
///
/// const DEBUG_INFO: &str = concat!(
/// important_lit!("Debug info:"), "\n",
/// important_lit!("This will also be colorized"), "\n"
/// );
/// # assert_eq!("\u{1b}[1;36mDebug info:\u{1b}[0m\n\u{1b}[1;36mThis will also be colorized\u{1b}[0m\n", DEBUG_INFO);
/// ```
///
/// Note, if you try to use it like `format!` by accident, it will return the wrapped literal
/// and not embed the replaced values:
///
/// ```rust
/// use bullet_stream::important_lit;
///
/// let url = "https://example.com";
/// let message = important_lit!("Url {url}");
///
/// // Does NOT interpolate it like "Url https://example.com"
/// // Instead it contains the literal string input including visible curly brackets:
/// assert!(message.contains("Url {url}"));
///
/// // If you need to use this with a format string instead use:
/// let message = bullet_stream::style::important(format!("Url {url}"));
/// # assert!(message.contains("Url https://example.com"));
/// ```
#[macro_export]
macro_rules! important_lit {
($input:literal) => {
concat!("\x1B[1;36m", $input, "\x1B[0m")
};
}
}
Loading