Skip to content

Commit

Permalink
Expose a way to style constants
Browse files Browse the repository at this point in the history
Issue #16 outlined how the current API is not usable to create const variables.

```rust
// Does not work
const DEBUG_INFO: String = bullet_stream::style::important("Debug info:");
```

This adds a macro that can work with constant declarations. It has caveats outlined in the docs.

```rust
const DEBUG_INFO: &str = important_lit!("Debug info:"); 
```
  • Loading branch information
schneems committed Nov 4, 2024
1 parent ec6a37f commit a453fa4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
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")
};
}
}

0 comments on commit a453fa4

Please sign in to comment.