diff --git a/.beans/claudbg-37cj--add-no-color-global-flag-and-no-color-env-var-supp.md b/.beans/claudbg-37cj--add-no-color-global-flag-and-no-color-env-var-supp.md new file mode 100644 index 0000000..0024637 --- /dev/null +++ b/.beans/claudbg-37cj--add-no-color-global-flag-and-no-color-env-var-supp.md @@ -0,0 +1,11 @@ +--- +# claudbg-37cj +title: Add --[no-]color global flag and NO_COLOR env var support +status: todo +type: task +created_at: 2026-03-31T00:32:57Z +updated_at: 2026-03-31T00:32:57Z +parent: claudbg-qpfe +--- + +Add `--color` / `--no-color` as a global CLI flag (available on all commands). When not specified, auto-detect: enable color if stdout is a tty and NO_COLOR is unset/empty. Honor the NO_COLOR environment variable (any non-empty value → disable color), per the NO_COLOR spec. This flag controls all ANSI output in the CLI (transcripts etc). diff --git a/src/cli.rs b/src/cli.rs index b4f7ea8..bce3429 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -116,6 +116,37 @@ pub struct GlobalOpts { /// Valid values: thinking, output. Example: --include thinking,output #[arg(long, global = true, default_value = "")] pub include: IncludeList, + /// Force color output even when not writing to a terminal. + #[arg(long, global = true, overrides_with = "no_color")] + pub color: bool, + /// Disable color output (also honored via the NO_COLOR env var). + #[arg(long = "no-color", global = true, overrides_with = "color")] + pub no_color: bool, +} + +impl GlobalOpts { + /// Determine whether color output should be enabled. + /// + /// Priority (highest → lowest): + /// 1. `NO_COLOR` env var set to a non-empty value → disabled. + /// 2. `--no-color` flag passed → disabled. + /// 3. `--color` flag passed → enabled. + /// 4. Auto-detect: enabled iff stdout is a TTY. + pub fn color_enabled(&self) -> bool { + use std::io::IsTerminal as _; + + // NO_COLOR spec: any non-empty value disables color. + if std::env::var("NO_COLOR").is_ok_and(|v| !v.is_empty()) { + return false; + } + if self.no_color { + return false; + } + if self.color { + return true; + } + std::io::stdout().is_terminal() + } } /// Top-level subcommands. diff --git a/src/commands/agents.rs b/src/commands/agents.rs index 61fd215..0e56417 100644 --- a/src/commands/agents.rs +++ b/src/commands/agents.rs @@ -464,6 +464,8 @@ mod tests { output: OutputFormat::Table, verbose: false, include: IncludeList::default(), + color: false, + no_color: false, } } diff --git a/src/commands/sessions.rs b/src/commands/sessions.rs index 8537b1a..bed4f5e 100644 --- a/src/commands/sessions.rs +++ b/src/commands/sessions.rs @@ -647,6 +647,8 @@ mod tests { output: OutputFormat::Table, verbose: false, include: IncludeList::default(), + color: false, + no_color: false, } }