Honor NO_COLOR without suppressing text attributes - #294
Open
MsfPablo wants to merge 1 commit into
Open
Conversation
Per <https://no-color.org/>, the NO_COLOR convention is intended to disable *color* output, not text attributes such as bold, italic, or underline. The current behavior of the StyledObject fmt implementation drops the attribute escapes whenever colors are disabled (which NO_COLOR triggers), contradicting the spec. Likewise, an empty value like `NO_COLOR=""` should not disable color, but `env::var("NO_COLOR").is_ok()` returns true for any value, including the empty string. This commit: * moves the attribute escape emission in impl_fmt! outside the colors_enabled() gate so attrs survive when colors are suppressed; * changes `env::var("NO_COLOR").is_ok()` in the unix and windows backends to a non-empty check that matches the spec; * adds a regression test exercising both behaviors. Fixes console-rs#291
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The current
StyledObjectformatting path inconsoledrops theattribute escapes (bold, italic, underline, blink, reverse, hidden,
strikethrough) whenever colors are disabled — and
NO_COLOR(perno-color.org) is what disables colors. That contradicts the spec, which
is explicit that only color is suppressed:
consolealso currently disables color whenNO_COLOR=""is set,because
env::var("NO_COLOR").is_ok()returnstruefor any value(including the empty string). Per the spec the presence of the variable
alone should disable color — but a deliberately-empty variable should be
a no-op.
Changes
impl_fmt!(src/utils.rs)outside the
colors_enabled()/colors_enabled_stderr()gate, soattributes are still emitted (with a trailing reset) when colors are
suppressed.
env::var("NO_COLOR").is_ok()with a non-empty check(
env::var_os("NO_COLOR").is_some_and(|v| !v.is_empty())) insrc/unix_term.rsandsrc/windows_term/mod.rs, matching thespec.
test_attrs_survive_colors_disabled) coveringboth behaviors.
Verification
The new test verifies:
style("foo").bold()still emits\x1b[1m…\x1b[0mwhencolors_enabled()isfalse.style("bar").italic()still emits\x1b[3m…\x1b[0min thesame condition.
style("baz").red()does not emit\x1b[31mand doesnot emit a trailing reset when colors are disabled.
AI disclosure
This PR was drafted with AI assistance (Claude) and self-reviewed. I
read the relevant code paths, reproduced the failing behavior locally
against the crate's existing test harness, and verified the fix does not
change the rendered output for the unaffected code paths.
Fixes #291