Skip to content

Commit 423e28e

Browse files
committed
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
1 parent 975fcd1 commit 423e28e

File tree

1 file changed

+37
-17
lines changed

1 file changed

+37
-17
lines changed

src/lib.rs

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -164,40 +164,60 @@ pub enum ColorChoice {
164164
/// than emitting ANSI color codes.
165165
AlwaysAnsi,
166166
/// Try to use colors, but don't force the issue. If the console isn't
167-
/// available on Windows, or if TERM=dumb, for example, then don't use
168-
/// colors.
167+
/// available on Windows, or if TERM=dumb, or if `NO_COLOR` is defined, for
168+
/// example, then don't use colors.
169169
Auto,
170170
/// Never emit colors.
171171
Never,
172172
}
173173

174174
impl ColorChoice {
175175
/// Returns true if we should attempt to write colored output.
176-
#[cfg(not(windows))]
177176
fn should_attempt_color(&self) -> bool {
178177
match *self {
179178
ColorChoice::Always => true,
180179
ColorChoice::AlwaysAnsi => true,
181180
ColorChoice::Never => false,
182-
ColorChoice::Auto => match env::var("TERM") {
183-
Err(_) => false,
184-
Ok(k) => k != "dumb",
185-
},
181+
ColorChoice::Auto => self.env_allows_color(),
186182
}
187183
}
188184

189-
/// Returns true if we should attempt to write colored output.
185+
#[cfg(not(windows))]
186+
fn env_allows_color(&self) -> bool {
187+
match env::var_os("TERM") {
188+
// If TERM isn't set, then we are in a weird environment that
189+
// probably doesn't support colors.
190+
None => return false,
191+
Some(k) => {
192+
if k == "dumb" {
193+
return false;
194+
}
195+
}
196+
}
197+
// If TERM != dumb, then the only way we don't allow colors at this
198+
// point is if NO_COLOR is set.
199+
if env::var_os("NO_COLOR").is_some() {
200+
return false;
201+
}
202+
true
203+
}
204+
190205
#[cfg(windows)]
191-
fn should_attempt_color(&self) -> bool {
192-
match *self {
193-
ColorChoice::Always => true,
194-
ColorChoice::AlwaysAnsi => true,
195-
ColorChoice::Never => false,
196-
ColorChoice::Auto => match env::var("TERM") {
197-
Err(_) => true,
198-
Ok(k) => k != "dumb",
199-
},
206+
fn env_allows_color(&self) -> bool {
207+
// On Windows, if TERM isn't set, then we shouldn't automatically
208+
// assume that colors aren't allowed. This is unlike Unix environments
209+
// where TERM is more rigorously set.
210+
if let Some(k) = env::var_os("TERM") {
211+
if k == "dumb" {
212+
return false;
213+
}
200214
}
215+
// If TERM != dumb, then the only way we don't allow colors at this
216+
// point is if NO_COLOR is set.
217+
if env::var_os("NO_COLOR").is_some() {
218+
return false;
219+
}
220+
true
201221
}
202222

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

0 commit comments

Comments
 (0)