Colorize and stylize strings at compile-time, by using a XML-like syntax.
This library provides three macros:
cformat!(<FORMAT_STRING> [, ARGS...])
cprint!(<FORMAT_STRING> [, ARGS...])
cprintln!(<FORMAT_STRING> [, ARGS...])
Each of them have the same syntax as format!()
, print!()
and println!()
respectively, but
they accept an additional syntax inside the format string: XML-like tags which add terminal
colors/styles at compile-time.
Note: these tags are commonly named "color tags" in the documentation below.
By default, the provided macros will replace the color tags found in the format string by ANSI hexadecimal escape codes. E.g.:
rust
cprintln!("HELLO <green>WORLD</green>")
will be replaced by:
rust
println!("HELLO \x1b[31mWORLD\x1b[39m")
However it is possible to change this behaviour by activating the feature terminfo
. Then it
will question the terminfo
database at runtime in order to know which sequence to write for
each kind of styling/colorizing (see below for more detail).
terminfo
is activated);"<green>...<blue>...</blue>...</green>"
;"my <b>blue</> word"
;format!
-like
macros; e.g., positional arguments and named arguments can be used as usual.``` rust use color_print::cprintln;
fn main() {
cprintln!("Hello
</>
tagBasically, tags must be closed by giving exactly the same colors/styles of the matching open
tag (with a slash /
at the beginning). But it can be tedious!
So, it is also possible to close the last open tag simply with </>
:
rust
cprintln!("Hello <green>world</>!");
Multiple styles and color(s) can be combined into a single tag by separating them with the ;
semicolon character:
rust
cprintln!("This a <green;bold>green and bold text</green;bold>.");
// The same, but closing with the </> tag:
cprintln!("This a <green;bold>green and bold text</>.");
Of course, combining multiple foreground colors or multiple background colors into the same tag is useless (in such a case, only the last one is taken into account).
Any tag can be nested with any other, as long as the closing tags match correctly (following the basic rules of nesting for XML tags):
``` rust
cprintln!("
// Colors can be nested as well:
cprintln!("
Tags which have not been closed manually will be closed automatically, which means that the ANSI sequences needed to go back to the original state will be added:
rust
// The two following lines are strictly equivalent:
cprintln!("<green><bold>Hello");
cprintln!("<green><bold>Hello</></>");
<
and >
verbatimAs for {
and }
, the chars <
and >
have to be doubled in order to display them verbatim:
rust
cprintln!("This is an angle bracket character: <<, and here is another one: >>");
The expanded format string will only contain the needed ANSI codes. This is done by making a diff of the different style attributes, each time a color tag is encountered, instead of mechanically adding the ANSI codes.
E.g., several nested <bold>
tags will only produce one bold ANSI sequence:
rust
cprintln!("<bold><bold>A <bold;blue>B C D</> E</></>")
will be replaced by:
rust
println!("\x1b[1mA \x1b[34mB C D\x1b[39m E\x1b[22m")
// ^-----^ ^------^ ^------^ ^------^
// bold blue color no bold
// reset
terminfo
Instead of inserting ANSI sequences directly into the format string, it is possible to activate
the feature terminfo
: this will add the format sequences at runtime, by consulting the
terminfo
database.
This has one pro and several cons:
lazy_static
] and [terminfo
];<strike>
and <conceal>
are not handled;Each color/style tag has at least a long name, like <magenta>
or <underline>
.
The tags directly relative to colors (like <red>
, <bg-blue>
, <bg-bright-green>
..., as
opposed to style tags like <bold>
, <italics>
...) have some common naming rules:
<mycolor>
: the normal, foreground color;<bright-mycolor>
: the bright, foreground color;<bg-mycolor>
: the normal, background color;<bg-bright-mycolor>
: the bright, background color;x
letter):
<x>
: the normal, foreground color;<x!>
: the bright, foreground color;<X>
: the normal, background color;<X!>
: the bright, background color;<black>
, each color's shortcut letter is simply the first letter of
its name, e.g. <y>
is the shortcut for <yellow>
;!
is a bright color;!
.| ANSI | Terminfo | Long names | Aliases | Shortcuts |
| ---- | -------- | ----------------------- | ---------------------- | --------- |
| X | X | <bold>
| <em>
, <strong>
| <s>
|
| X | X | <dim>
| | |
| X | X | <underline>
| | <u>
|
| X | | <strike>
| | |
| X | X | <reverse>
| <rev>
| |
| X | | <conceal>
| <hide>
| |
| X | X | <italics>
| <italic>
| <i>
|
| X | X | <blink>
| | |
| X | X | <black>
| | <k>
|
| X | X | <red>
| | <r>
|
| X | X | <green>
| | <g>
|
| X | X | <yellow>
| | <y>
|
| X | X | <blue>
| | <b>
|
| X | X | <magenta>
| | <m>
|
| X | X | <cyan>
| | <c>
|
| X | X | <white>
| | <w>
|
| X | X | <bright-black>
| <black!>
| <k!>
|
| X | X | <bright-red>
| <red!>
| <r!>
|
| X | X | <bright-green>
| <green!>
| <g!>
|
| X | X | <bright-yellow>
| <yellow!>
| <y!>
|
| X | X | <bright-blue>
| <blue!>
| <b!>
|
| X | X | <bright-magenta>
| <magenta!>
| <m!>
|
| X | X | <bright-cyan>
| <cyan!>
| <c!>
|
| X | X | <bright-white>
| <white!>
| <w!>
|
| X | X | <bg-black>
| | <K>
|
| X | X | <bg-red>
| | <R>
|
| X | X | <bg-green>
| | <G>
|
| X | X | <bg-yellow>
| | <Y>
|
| X | X | <bg-blue>
| | <B>
|
| X | X | <bg-magenta>
| | <M>
|
| X | X | <bg-cyan>
| | <C>
|
| X | X | <bg-white>
| | <W>
|
| X | X | <bg-bright-black>
| <bg-black!>
| <K!>
|
| X | X | <bg-bright-red>
| <bg-red!>
| <R!>
|
| X | X | <bg-bright-green>
| <bg-green!>
| <G!>
|
| X | X | <bg-bright-yellow>
| <bg-yellow!>
| <Y!>
|
| X | X | <bg-bright-blue>
| <bg-blue!>
| <B!>
|
| X | X | <bg-bright-magenta>
| <bg-magenta!>
| <M!>
|
| X | X | <bg-bright-cyan>
| <bg-cyan!>
| <C!>
|
| X | X | <bg-bright-white>
| <bg-white!>
| <W!>
|