#colored #colored-text #syntax #macro #terminal #xml-like #no-color

colored-macro

A macro to make colored text in the terminal using XML-like syntax

2 unstable releases

0.2.0 May 15, 2023
0.1.0 Apr 19, 2023

#330 in Template engine

MIT license

4KB

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>")

NO_COLOR Support

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

Output of above code

Dependencies

~0.8–1.3MB
~24K SLoC