Coloring terminal so simple, you already know how to do it!
"this is blue".blue; "this is red".red; "this is red on blue".red.on_blue; "this is also red on blue".on_blue.red; "bright colors are welcome as well".on_bright_blue.bright_red; "you can also make bold comments".bold; println!; "or change advice. This is red".yellow.blue.red; "or clear things up. This is default color and style".red.bold.clear; "purple and magenta are the same".purple.magenta; "and so are normal and clear".normal.clear; "you can specify color by string".color.on_color; Stringfrom.green.bold; format!; format!;
How to use
Add this in your
 = "1.9"
and add this to your
extern crate colored; // not needed in Rust 2018 use *; // test the example with `cargo run --example most_simple`
- Safe rust, easy to use, minimal dependencies, complete test suite
- Respect the
CLICOLOR_FORCEbehavior (see the specs)
- Respect the
NO_COLORbehavior (see the specs)
- Works on Linux, MacOS, and Windows (Powershell)
- magenta (or purple)
Bright colors: prepend the color by
bright_. So easy.
Background colors: prepend the color by
on_. Simple as that.
Bright Background colors: prepend the color by
on_bright_. Not hard at all.
You can clear color and style anytime by using
Dynamic color from str
From<String>, you can easily cast a string into a color like that:
// the easy way "blue string yo".color; // this will default to white "white string".color; // the safer way via a Result let color_res : = "zorglub".parse; "red string".color;
If you want to disable any coloring at compile time, you can simply do so by
For example, you can do this in your
Cargo.toml to disable color in tests:
 # this effectively enable the feature `no-color` of colored when testing with # `cargo test --feature dumb_terminal` = ["colored/no-color"]
You can use have even finer control by using the
Build with Docker
Use the install instructions located here
Build the Docker image
docker build -t colored_image .
Build the library
docker run --rm -it -v "$PWD":/src -u `id -u`:`id -g` colored_image /bin/bash -c "cargo build"
Test the library
docker run --rm -it -v "$PWD":/src -u `id -u`:`id -g` colored_image /bin/bash -c "cargo test"
- More tests ?: We always welcome more tests! Please contribute!
This library wouldn't have been the same without the marvelous ruby gem colored.
Thanks for the ansi_term crate for providing a reference implementation, which greatly helped making this crate output correct strings.
In non legal terms it means that:
- if you fix a bug, you MUST give me the code of the fix (it's only fair)
- if you change/extend the API, you MUST give me the code you changed in the files under MPL2.
- you CAN'T sue me for anything about this code
- apart from that, you can do almost whatever you want. See the LICENSE file for details.