NO_COLOR suppresses non-color attributes, not just color

Aperta
#337 6 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
48/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Tranquilla
Stack tecnologico
rust
Ambito
cli, devtools

Direzione di ricerca

Inizia con il percorso NO_COLOR da anstream::ColorChoice::Never passando per StripStream e StripBytes, quindi esamina anstyle::Style e csi_dispatch di anstyle-parse. Riproduci l'esempio e traccia il modo in cui i parametri SGR vengono scartati. Il lavoro è completato quando NO_COLOR preserva gli effetti non relativi al colore sopprimendo al contempo il colore, con una copertura per l'output descritto e il comportamento Never esistente.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

A-stream C-bug S-triage

NO_COLOR suppresses non-color attributes, not just color

Summary

When NO_COLOR is set, anstream strips all ANSI escape sequences —
bold, dim, italic, underline, reverse, strikethrough, etc. — not only
color. This diverges from no-color.org
(or its source),
whose FAQ is explicit:

Q: Should the presence of NO_COLOR disable other styling such as
bold, underline, and italic?

A: No. This standard only signals the user's intention regarding
adding ANSI color to text output.

Reproduction

NO_COLOR resolves to ColorChoice::NeverStripStream, whose
StripBytes engine (driven by anstyle_parse) discards every CSI escape
without inspecting its SGR parameters. Verified on anstream 1.0.0:

use std::io::Write as _;

let mut out = anstream::AutoStream::new(Vec::new(), anstream::ColorChoice::Never);
write!(out, "\x1b[1mbold\x1b[22m \x1b[4munderline\x1b[24m \x1b[31mred\x1b[39m").unwrap();
assert_eq!(String::from_utf8(out.into_inner()).unwrap(), "bold underline red");

Bold and underline are stripped along with the red color. Per the FAQ, only
color should be suppressed; the expected output is
\x1b[1mbold\x1b[22m \x1b[4munderline\x1b[24m red.

Suggested direction

A spec-correct color-only strip needs to distinguish color SGR parameters
from attribute parameters and re-emit only the latter. Most of the
primitives for this already live in the workspace: anstyle::Style
separates fg/bg/underline-color from effects, and anstyle-parse
already produces the SGR parameter list via csi_dispatch. The missing
piece is a decode step (param list → Style delta) so a strip adapter can
track the active style and render only effects. If a shared decoder
lived in anstyle, other consumers could reuse it for the same spec
alignment — but the shape is of course the maintainers' call.

One design point worth flagging: ColorChoice::Never is reached today for
NO_COLOR, for non-terminals, and for CLICOLOR disabled. The FAQ
constrains only the NO_COLOR case, so it may be cleanest for NO_COLOR
to resolve to a color-only path while the other Never callers keep the
full strip (pipe-to-file output today is fully flat, which is arguably
desirable for logs).

Notes

  • The empty-string trigger is already handled correctly upstream:
    anstyle_query::no_color() is non_empty(NO_COLOR), so NO_COLOR=""
    disables nothing, per spec. This issue is only about the scope of
    suppression.
  • This cascades to dependents routing through anstream (env_logger,
    clap with its color feature), which would inherit the fix.
  • crossterm is a working example of color-only NO_COLOR handling —
    it gates Colored (fg/bg/underline-color) but emits SetAttributes
    unconditionally.
  • Related: #192 (env-var support context).

Versioning

I'd frame this as a bug fix (patch bump) with a one-line CHANGELOG note
acknowledging the observable output change, but defer to the project's
own version policy.

Lingua principale
HTML
Stelle
171
Fork
44
Merge medio
2h 34m
PR unite (30g)
6

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di rust-cli/anstyle

Tutte le issue di rust-cli/anstyle

Issue simili

Altre issue su CLI

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.