lool » cli.stylize
is a set of utilities for colorizing console outputs.
This crate is for internal use. It's only published privately.
cargo add lool --registry=lugit --features cli cli.stylize
This library provides a set of functions to colorize and stylize console outputs. The "low-level" API works by parsing a string with special instructions to apply styles to the text.
use lool::cli::stylize::stylize;
fn main() {
let text = "Hello, World!";
let styled_text = stylize(text, "blue on red+bold|underline");
println!("{}", styled_text);
}
The above code will print "Hello, World!" with the following styles:
- Foreground color: blue
- Background color: red
- Attributes: bold and underline
The instructions are a string with the following format:
<fg-color> on <bg-color>+<attr1>|<attr2>|<attr...>
All instructions are optional. Which means that the following are all valid instructions:
red
on green
+bold
red+bold
on green+bold
on green+bold|underline
It accepts all standard ANSI colors:
- black
- red
- green
- yellow
- blue
- magenta
- cyan
- white
- bright-black
- bright-red
- bright-green
- bright-yellow
- bright-blue
- bright-magenta
- bright-cyan
- bright-white
It also supports TrueColor by providing a hex code:
e.g.
#ff0000 on #00ff00
Which means, basically, red on green.
The following attributes are supported:
- bold
- dim
- italic
- underline
- blink
- reverse
- hidden
- strikethrough
Attributes can be combined with the |
character.
The library also provides an abstraction over the low-level API. The high-level API provides a set of functions to colorize and stylize console outputs.
It builds on top of str
and String
types, providing a set of methods to colorize and stylize text.
// import the Stylish trait
use lool::cli::stylize::{Stylize};
fn main() {
let styled_text = "Hello, World!".blue().on_red().bold().underline();
println!("{}", styled_text);
}
The above code will print "Hello, World!" with the following styles:
- Foreground color: blue
- Background color: red
- Attributes: bold and underline