C0024 cli-plain-when-piped¶
Rule: A CLI writes no colour escapes into a pipe.
| Code | C0024 (stable, never reused) |
| Id | cli-plain-when-piped |
| Category | cli |
| Source | spirit_level |
| Fix | by hand |
| Needs | cargo installed |
Level per tier¶
| Tier | Level | Reason |
|---|---|---|
| small | warn |
colour escapes in a pipe break grep, diffs and scripts |
| small-workspace | warn |
colour escapes in a pipe break grep, diffs and scripts |
| medium | deny |
colour escapes in a pipe break grep, diffs and scripts |
| big | deny |
colour escapes in a pipe break grep, diffs and scripts |
| ai | off | |
| embedded | off | |
| ui | off | |
| egui | off | |
| units-strict | off | |
| units-paranoid | off |
Why¶
Colour escapes in a pipe or a file are noise: grep misses words, diffs show ^[[1m, and scripts parse garbage. A tool colours a stream only when that stream is a terminal and NO_COLOR is not set.
How to fix¶
Decide colour per stream: anstream does it for print! through its stdout(), clap does it for help and errors with ColorChoice::Auto, and vhrd_cli's Paint::stdout() and Paint::stderr() do it for painted text. Never force ColorChoice::Always as a default. The lint runs the help of every command, --version and a usage error with both streams on pipes, and reports the first escape it finds.
Configure¶
Allow one finding in the code with // spirit-level: allow(cli-plain-when-piped) <reason> (waivers); the code works there too, as in [lints.C0024].