#colored #colored-text #syntax #macro #terminal #ansi #colored-macro

colored-macro-impl

A macro to make colored text in the terminal using XML-like syntax. You should depend on the colored-macro crate instead.

1 unstable release

0.2.0 May 15, 2023

#9 in #colored-text

30 downloads per month
Used in 2 crates (via colored-macro-macro)

MIT license

12KB
270 lines

Colored Macro

Colored macro is a Rust crate for creating colored and formatted strings using ANSI codes in a readable way. It uses XML-like syntax:

<red>red text</red> <bold>bold text</bold> <green>{fmt_expr}</green>

The Problem

With current crates, messages quickly become hard to quickly understand at a glance. This is best explained with an example:

println!("[{}] ({}): {}", level.green(), timestamp.blue(), message.bold());

You can see that even with a simple message, the format string becomes hard to quickly understand, compared to the equivalent using this crate:

println!("{}", colored!("[<green>{level}</green>] (<blue>{timestamp}</blue>): <bold>{message}</bold>"));

Documentation

The following styles are available:

  • Any CSS named color, as long as their alpha is 1.
  • Any RGB color in the regular CSS format, as long as its alpha is 1 (e.g. rgb(10, 12, 200), #0012G4).
  • reset (resets current styles)
  • bold
  • dim
  • italic
  • underline
  • blink
  • reverse
  • hidden
  • strikethrough

Styles can also be nested:

colored_macro::colored!("<red>all red <yellow>yellow <blue>now to blue</blue> back to yellow</yellow> back to red</red>")

Output of above code

NO_COLOR Support

The NO_COLOR environment variable is supported if the no-color feature is enabled.


lib.rs:

This crate implements the macro for colored_macro and should not be used directly.

Dependencies

~0.9–1.3MB
~25K SLoC