Skip to content
Permalink
Browse files
env: respect NO_COLOR environment variable
This adds support for respecting the NO_COLOR environment variable,
whose spec is defined here: https://no-color.org/

The relevant portion is:

    > 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.

As such, termcolor will now detect the presence of the NO_COLOR
environment variable. If it exists, and the color choice is "auto," then
colors will be suppressed.

Closes #1186
  • Loading branch information
BurntSushi committed Jan 11, 2020
1 parent 975fcd1 commit 423e28eb69a77799179474575869c5b0433d6adb
Showing 1 changed file with 37 additions and 17 deletions.
@@ -164,40 +164,60 @@ pub enum ColorChoice {
/// than emitting ANSI color codes.
AlwaysAnsi,
/// Try to use colors, but don't force the issue. If the console isn't
/// available on Windows, or if TERM=dumb, for example, then don't use
/// colors.
/// available on Windows, or if TERM=dumb, or if `NO_COLOR` is defined, for
/// example, then don't use colors.
Auto,
/// Never emit colors.
Never,
}

impl ColorChoice {
/// Returns true if we should attempt to write colored output.
#[cfg(not(windows))]
fn should_attempt_color(&self) -> bool {
match *self {
ColorChoice::Always => true,
ColorChoice::AlwaysAnsi => true,
ColorChoice::Never => false,
ColorChoice::Auto => match env::var("TERM") {
Err(_) => false,
Ok(k) => k != "dumb",
},
ColorChoice::Auto => self.env_allows_color(),
}
}

/// Returns true if we should attempt to write colored output.
#[cfg(not(windows))]
fn env_allows_color(&self) -> bool {
match env::var_os("TERM") {
// If TERM isn't set, then we are in a weird environment that
// probably doesn't support colors.
None => return false,
Some(k) => {
if k == "dumb" {
return false;
}
}
}
// If TERM != dumb, then the only way we don't allow colors at this
// point is if NO_COLOR is set.
if env::var_os("NO_COLOR").is_some() {
return false;
}
true
}

#[cfg(windows)]
fn should_attempt_color(&self) -> bool {
match *self {
ColorChoice::Always => true,
ColorChoice::AlwaysAnsi => true,
ColorChoice::Never => false,
ColorChoice::Auto => match env::var("TERM") {
Err(_) => true,
Ok(k) => k != "dumb",
},
fn env_allows_color(&self) -> bool {
// On Windows, if TERM isn't set, then we shouldn't automatically
// assume that colors aren't allowed. This is unlike Unix environments
// where TERM is more rigorously set.
if let Some(k) = env::var_os("TERM") {
if k == "dumb" {
return false;
}
}
// If TERM != dumb, then the only way we don't allow colors at this
// point is if NO_COLOR is set.
if env::var_os("NO_COLOR").is_some() {
return false;
}
true
}

/// Returns true if this choice should forcefully use ANSI color codes.

0 comments on commit 423e28e

Please sign in to comment.