diff --git a/CHANGELOG.md b/CHANGELOG.md index cd309a5..2255621 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/src/style.rs b/src/style.rs index 724aeaf..4d1c2cb 100644 --- a/src/style.rs +++ b/src/style.rs @@ -38,3 +38,56 @@ pub fn details(contents: impl AsRef) -> String { pub fn important(contents: impl AsRef) -> 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") + }; + } +}