Skip to content

Commit b4afcd8

Browse files
authored
fix(cli): suppress ANSI color when stdout is not a terminal (#3026)
* fix(cli): suppress ANSI color when stdout is not a terminal The CLI colorized output unconditionally. owo-colors is built without its `supports-colors` feature, so `.green()` and friends emitted escape sequences regardless of destination, and nothing in the CLI read NO_COLOR. Piping any command through grep or awk matched against bytes the caller could not see; `forward list` was the case that surfaced it, where an escape sits immediately before the STATUS word and defeats a pattern anchored on whitespace. Add a `color` module holding a process-wide switch resolved once in run_async, before any output. Command modules import its `Colorize` trait in place of `OwoColorize`; the method names match, so the ~450 call sites are unchanged, but each consults the switch when it renders and delegates to owo-colors so the escape bytes stay identical. The two traits collide by design: importing both in one module is an ambiguity error, which keeps unconditional coloring from returning. owo-colors is not the only styled path, and the rest each carry their own default, so the switch governs them too: - tracing_subscriber formats with ANSI on, does no terminal detection, and writes to stdout, so `openshell -v ... | ...` leaked escapes the same way the tables did. It now takes the setting via with_ansi. - indicatif and dialoguer both style through console, which has its own detection but cannot learn about --color. Overriding console's global switch covers every progress bar and prompt rather than the specific ones constructed today. Both the stdout and stderr switches are set, since prompts and progress bars draw to stderr. - miette renders errors through its own handler, likewise unaware of --color, so init installs one built from the setting. Resolution order: `--color always|never`, then NO_COLOR, then CLICOLOR_FORCE, then whether stdout is a terminal. The decision is made against stdout even for stderr text, since stdout is what gets parsed; `--color always` restores styling when redirecting. Padding is unaffected — the format spec is forwarded to the inner Display, so widths measure text rather than text plus escapes. Signed-off-by: Mrunal Patel <mrunalp@gmail.com> * fix(cli): resolve color per output stream Review feedback on #3026. Resolving one answer from stdout and handing it to every library meant a redirected stream inherited the other stream's terminal check. Running `openshell ... 2> build.log` from a terminal wrote escapes into the log, because console's stderr switch and miette's handler were both given stdout's answer. That is worse than the behavior before this branch, where both libraries did their own per-stream detection. Resolve `auto` separately for stdout and stderr and hand each library the answer for the stream it writes to: tracing and console's stdout switch get stdout, miette and console's stderr switch get stderr. The owo-colors wrapper is the exception, since its call sites are split across println! and eprintln! and a Painted value cannot tell which macro will consume it; it styles only when both streams accept escapes, erring toward plain text rather than risking a redirected stream. Existing tests could not catch this: Command::output gives both streams pipes, so a per-stream decision and a single stdout-derived one look identical. Add a test that puts stdout on a pty and stderr on a pipe, which fails when stderr is handed stdout's answer. Replace CLICOLOR_FORCE with FORCE_COLOR. The clicolors spec does not say how to treat `0`, and implementations that special-case it disagree with force-color.org, which keys on presence and non-emptiness only. Using FORCE_COLOR gives it the same rule as NO_COLOR: set and non-empty means yes, whatever the value. Nothing depended on CLICOLOR_FORCE, which was introduced earlier on this branch and never released. Carry the whole style in an owo_colors::Style rather than dispatching a local enum through a six-arm match, and merge styles when chaining so `x.green().bold()` emits one `\x1b[32;1m...\x1b[0m` instead of nesting two wrappers. No call site styles already-styled text, so merging is safe; the emitted bytes are shorter and there is a single reset. Signed-off-by: Mrunal Patel <mrunalp@gmail.com> --------- Signed-off-by: Mrunal Patel <mrunalp@gmail.com>
1 parent 5b925dd commit b4afcd8

12 files changed

Lines changed: 886 additions & 5 deletions

File tree

‎.agents/skills/openshell-cli/cli-reference.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Quick-reference for the `openshell` command-line interface. For workflow guidanc
1212
| `-g`, `--gateway <NAME>` | Gateway to operate on. Also settable via `OPENSHELL_GATEWAY` env var. Falls back to active gateway in `~/.config/openshell/active_gateway`. |
1313
| `--gateway-endpoint <URL>` | Connect directly to a gateway endpoint without looking up stored metadata. Also settable via `OPENSHELL_GATEWAY_ENDPOINT`. |
1414
| `--gateway-insecure` | Skip TLS certificate verification. Also settable via `OPENSHELL_GATEWAY_INSECURE`; use only for trusted development endpoints. |
15+
| `--color <WHEN>` | `auto` (default), `always`, or `never`. `auto` decides per stream, so a redirected stream is plain text while a stream still on the terminal stays styled. Covers tables, `-v` log lines, progress spinners, prompts, and error messages. Also settable via `OPENSHELL_COLOR`. |
1516

1617
## Environment Variables
1718

@@ -20,6 +21,9 @@ Quick-reference for the `openshell` command-line interface. For workflow guidanc
2021
| `OPENSHELL_GATEWAY` | Override active gateway name (same as `--gateway`) |
2122
| `OPENSHELL_GATEWAY_ENDPOINT` | Connect directly to a gateway endpoint (same as `--gateway-endpoint`) |
2223
| `OPENSHELL_GATEWAY_INSECURE` | Skip TLS verification when set (same as `--gateway-insecure`) |
24+
| `OPENSHELL_COLOR` | When to colorize output: `auto`, `always`, `never` (same as `--color`) |
25+
| `NO_COLOR` | Disable colorized output when set to any non-empty value ([no-color.org](https://no-color.org)) |
26+
| `FORCE_COLOR` | Force colorized output when set to any non-empty value ([force-color.org](https://force-color.org)) |
2327
| `OPENSHELL_SANDBOX_POLICY` | Path to default sandbox policy YAML (fallback when `--policy` is not provided) |
2428
| `OPENSHELL_COMMUNITY_REGISTRY` | Override the community sandbox image registry prefix used by `sandbox create --from <name>` |
2529
| `OPENSHELL_THEME` | TUI theme: `auto`, `dark`, or `light` |

‎Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/openshell-cli/Cargo.toml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ chrono = "0.4"
3737
clap = { workspace = true }
3838
clap_complete = { workspace = true }
3939
crossterm = { workspace = true }
40+
# Styling backend shared by dialoguer and indicatif; `color` overrides its
41+
# global switch so both follow the CLI's --color setting.
42+
console = "0.15"
4043
dialoguer = "0.11"
4144
indicatif = { workspace = true }
4245
owo-colors = { workspace = true }

0 commit comments

Comments
 (0)