colorize!() { /* proc-macro */ }
Expand description
Adds ANSI color escape sequences to inputs
§Usage
colorize!
takes a series of inputs, with or without tokens, and converts the inputs into a String
with ANSI escape sequences added in.
The returned String
is primarily useful for printing out to a terminal which is capable of showing color.
However, if all you want to do is print, and want to cut out the extra code, use print_color
instead.
§Valid inputs
colorize!
uses the same formatting style as format!
so it follows the same
argument rules.
use std::path::PathBuf;
use colorize::colorize;
let user_path = PathBuf::from("/home/color/my_new_file.txt");
let pretty_path = colorize!("{:?}", Fgu->user_path.clone());
assert_eq!(
String::from("\x1b[32;4m\"/home/color/my_new_file.txt\"\x1b[0m"),
pretty_path
);
§Tokens
Tokens can change color or font styling depending on their order and usage.
§Styling
- b -> bold
- u -> underline
- i -> italic
§Color
Color tokens start with an F
(for foreground) or B
(for background)
- Fb/Bb -> blue
- Fr/Br -> red
- Fg/Bg -> green
- Fy/By -> yellow
- Fm/By -> magenta
- Fc/Bc -> cyan
- Fw/Bw -> white
- Fk/Bk -> black
§Special Newline Token
If you want to add a newline within the string, include a N
token at the start
of the word(s) you wish to be on the newline. This is the same as just adding ‘\n’ to the
string, so it’s up to you to use it or not.
Example -
use colorize::colorize;
let color_string = colorize!(
"{} {}",
b->"Hello", // First line
Nb->"world, it's me!" // "world..." will be on the new line
);
let same_color_string = colorize!(
"{} \n{}",
b->"Hello",
b->"world, it's me!"
);
assert_eq!(color_string, same_color_string);
§Format Multiple Inputs
You also have the ability to apply a token to multiple inputs by using =>
at the beginning of the call.
use colorize::colorize;
let color_string = colorize!("{} {}", b => Fg->"Hello", By->"world");
In the above example, “Hello” will have a green foreground, and “world” will have a yellow background. The preceeding b =>
applies bold formatting to both.
§Examples
use colorize::colorize;
// Returns "Hello" in bold green
let color_string = colorize!("{}", Fgb->"Hello");
assert_eq!(String::from("\x1b[32;1mHello\x1b[0m"), color_string);
// Returns "Hello" in italic blue and "World" underlined in magenta
// ", it's me" will be unformatted
let color_string = colorize!("{} {} {}", iFb->"Hello", Fmu->"world", ", it's me!");
assert_eq!(String::from("\x1b[3;34mHello\x1b[0m \x1b[35;4mworld\x1b[0m , it's me!"), color_string);