Colored
colored is a Rust crate for creating colored CLI applications.
colored can use the environment variables: NO_COLOR and CLICOLOR and it’s companion CLICOLOR_FORCE.
But how would that work? - which setting wins.
CLICOLORenables coloringCLICOLORenabled and enforces coloringNO_COLORdisables coloring
I made a quick experiment based on the example from the colored documentation.
$ mkdir colored
$ cargo init colored
$ cd $_
Edit the Cargo.toml file.
[dependencies]
colored = "2"
Install/update dependencies.
$ cargo update
Edit our example (src/main.rs)
use colored::*;
fn main() {
println!("{} {} !", "Hello,".green(), " World!".red().bold());
}
Try it out:
$ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
Running `target/debug/colored`
Hello, World! !
And you should see colorful output.
$ NO_COLOR=1 cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
Running `target/debug/colored`
Hello, World!
And you should not see colorful output.
❯ NO_COLOR=1 CLICOLOR_FORCE=1 cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
Running `target/debug/colored`
Hello, World! !
And hey, what do you know, $CLICOLOR_FORCE wins over $NO_COLOR.