Skip to content

Disable color when not in a terminal or when the user requests it#2801

Merged
bfirsh merged 3 commits intomainfrom
auto-detect-color-support
Mar 4, 2026
Merged

Disable color when not in a terminal or when the user requests it#2801
bfirsh merged 3 commits intomainfrom
auto-detect-color-support

Conversation

@bfirsh
Copy link
Copy Markdown
Member

@bfirsh bfirsh commented Mar 3, 2026

Summary

  • Auto-detect color support: disable ANSI colors when stderr is not a TTY, NO_COLOR is set, COG_NO_COLOR is set, or TERM=dumb
  • Add --no-color persistent flag as an explicit CLI escape hatch

Previously Console.Color was hardcoded to true with no way to disable it. The SetColor() function existed but was never called.

A responsible follow-up to #2793.

What disables color

Signal Source
NO_COLOR env var (non-empty) no-color.org standard
COG_NO_COLOR env var (non-empty) App-specific override
TERM=dumb Common terminal convention
stderr is not a TTY Piped/redirected output
--no-color flag Explicit user request

The TTY check is on stderr specifically, since that is where cog writes all its colored log output. This means piping stdout to another program still preserves colors on stderr.

References

Disable color output automatically when the environment indicates it
is not appropriate. Previously, Console.Color was hardcoded to true
regardless of context.

Color is now disabled when any of the following are true:
- NO_COLOR env var is set (per https://no-color.org/)
- COG_NO_COLOR env var is set
- TERM=dumb
- stderr is not an interactive terminal (TTY)

The TTY check is on stderr specifically, since that is where cog
writes all its colored log output.

Refs: https://clig.dev/#output, https://no-color.org/
@bfirsh bfirsh requested a review from a team as a code owner March 3, 2026 21:58
@bfirsh bfirsh force-pushed the auto-detect-color-support branch from 374fc95 to 1baf057 Compare March 3, 2026 22:09
bfirsh added 2 commits March 3, 2026 16:36
Add a persistent --no-color flag that forces color off for all
subcommands. This complements the automatic environment detection
(NO_COLOR, COG_NO_COLOR, TERM=dumb, non-TTY stderr) added in the
previous commit, giving users an explicit CLI escape hatch.

Refs: https://clig.dev/#output, https://no-color.org/
@bfirsh bfirsh force-pushed the auto-detect-color-support branch from 1baf057 to 054c332 Compare March 3, 2026 22:36
@bfirsh bfirsh merged commit 8164991 into main Mar 4, 2026
31 checks passed
@bfirsh bfirsh deleted the auto-detect-color-support branch March 4, 2026 16:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants