#color #terminal #cli

no_color

Detect NO_COLOR environment variable

1 unstable release

0.1.0 Nov 21, 2020

#454 in Command-line interface

Download history 256/week @ 2023-11-20 145/week @ 2023-11-27 240/week @ 2023-12-04 165/week @ 2023-12-11 150/week @ 2023-12-18 118/week @ 2023-12-25 255/week @ 2024-01-01 147/week @ 2024-01-08 131/week @ 2024-01-15 149/week @ 2024-01-22 179/week @ 2024-01-29 181/week @ 2024-02-05 217/week @ 2024-02-12 222/week @ 2024-02-19 217/week @ 2024-02-26 223/week @ 2024-03-04

903 downloads per month
Used in 4 crates

MIT license

5KB

no_color

no_color is a rust library for detecting if the NO_COLOR environment variable is set.

About

From no-color.org:

An increasing number of command-line software programs output text with ANSI color escape codes by default. While some developers and users obviously prefer seeing these colors, many users don’t. Unfortunately, every new piece of software seems to have a different way of disabling colored text output and some software has no way at all.

Accepting the futility of trying to reverse this trend, an informal standard is hereby proposed:

All command-line software which outputs text with ANSI color added should check for the presence of a NO_COLOR environment variable that, when present (regardless of its value), prevents the addition of ANSI color.

By adopting this standard, users that prefer to have plain, non-colored text output can set one environment variable in their shell to have it automatically affect all supported software.

Usage

no_color is a library crate which works with Cargo.

Add to Cargo.toml

To use, add the following to your Cargo.toml dependencies section:

[dependencies]
no_color = "0.1"

Implementing Code

Suggested updates to your rust code as below:

extern crate no_color;

use no_color::*;

fn main() {
    println!(
        "Environment variable NO_COLOR {0} found. Now do something.",
        {
            if is_no_color() {
                "is"
            } else {
                "is NOT"
            }
        }
    );
}

No runtime deps