Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/unix_term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub(crate) fn is_a_color_terminal(out: &Term) -> bool {
return false;
}

if env::var("NO_COLOR").is_ok() {
if env::var_os("NO_COLOR").is_some_and(|v| !v.is_empty()) {
return false;
}

Expand Down
53 changes: 53 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,16 @@ macro_rules! impl_fmt {
write!(f, "{}", self.style.attrs)?;
reset = true;
}
} else {
// NO_COLOR and similar controls disable color, but per
// <https://no-color.org/> text attributes (bold, italic,
// underline, etc.) are not "color" and should remain
// visible. Emit them outside the colors_enabled gate so
// they survive when colors are suppressed.
if !self.style.attrs.is_empty() {
write!(f, "{}", self.style.attrs)?;
reset = true;
}
}
fmt::$name::fmt(&self.val, f)?;
if reset {
Expand Down Expand Up @@ -1227,3 +1237,46 @@ fn test_style_from_non_ascii_bg() {
// silently ignores non-ascii
assert_eq!(parsed_style, Style::default());
}

#[test]
fn test_attrs_survive_colors_disabled() {
// Regression test for <https://github.com/console-rs/console/issues/291>:
// per <https://no-color.org/>, `NO_COLOR` suppresses *color*, not text
// attributes. When color output is otherwise disabled, attributes such as
// bold/italic/underline should still be emitted (with a trailing reset).
let prev = colors_enabled();
set_colors_enabled(false);

let bold = style("foo").bold().to_string();
assert!(
bold.contains("\x1b[1m"),
"bold escape sequence missing when colors are disabled: {bold:?}"
);
assert!(
bold.ends_with("\x1b[0m"),
"reset escape sequence missing after bold: {bold:?}"
);

let italic = style("bar").italic().to_string();
assert!(
italic.contains("\x1b[3m"),
"italic escape sequence missing when colors are disabled: {italic:?}"
);
assert!(
italic.ends_with("\x1b[0m"),
"reset escape sequence missing after italic: {italic:?}"
);

// Foreground color is *still* suppressed when colors are disabled.
let colored = style("baz").red().to_string();
assert!(
!colored.contains("\x1b[31m"),
"foreground color escape sequence should be suppressed when colors are disabled: {colored:?}"
);
assert!(
!colored.contains("\x1b[0m"),
"reset escape sequence should not be emitted when nothing is styled: {colored:?}"
);

set_colors_enabled(prev);
}
2 changes: 1 addition & 1 deletion src/windows_term/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub(crate) fn is_a_color_terminal(out: &Term) -> bool {
if !is_a_terminal(out) {
return false;
}
if env::var("NO_COLOR").is_ok() {
if env::var_os("NO_COLOR").is_some_and(|v| !v.is_empty()) {
return false;
}
if msys_tty_on(out) {
Expand Down
Loading