til

Today I Learned: collection of notes, tips and tricks and stuff I learn from day to day working with computers and technology as an open source contributor and product manager

View project on GitHub

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.

  • CLICOLOR enables coloring
  • CLICOLOR enabled and enforces coloring
  • NO_COLOR disables 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.

Resources and References

  1. crates.io: colored
  2. NO_COLOR specification
  3. CLI_COLORS specification