fix(cli): honor NO_COLOR, add --no-color, disable colors when stdout is not a TTY (#1281)#1303
Open
maxmilian wants to merge 1 commit into
Open
fix(cli): honor NO_COLOR, add --no-color, disable colors when stdout is not a TTY (#1281)#1303maxmilian wants to merge 1 commit into
maxmilian wants to merge 1 commit into
Conversation
…is not a TTY (colbymchenry#1281) Human-format output (query, status, callers, callees, impact, files, …) embedded ANSI color codes unconditionally: piping to a file or another tool captured the escapes, and the no-color.org escape hatch was ignored (NO_COLOR=1 produced byte-identical output). All CLI coloring already flows through the colors palette in src/bin/codegraph.ts (and a mini-palette in src/upgrade/index.ts), so the fix is a single shared gate (src/ui/colors.ts): - --no-color anywhere on the command line disables color (stripped before commander parses, same pre-parse intercept pattern as -v, so it also works after subcommands) - a non-empty NO_COLOR environment variable disables color - otherwise color is enabled only when stdout is a TTY With color disabled the palette collapses to empty strings, so every existing colors.X interpolation and chalk.X() helper becomes a plain-text no-op — no call sites change. Exercised end-to-end against the built binary: piped query/status/ callers output must be escape-free, NO_COLOR is honored, --no-color is accepted after a subcommand, and an empty NO_COLOR does not disable color (per spec). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XWwajmLbgJbK6JBCY8nKfk
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1281
Problem
Human-format output (
query,status,callers,callees,impact,files, …) embedded ANSI color codes unconditionally: piping to a file or another tool captured the escapes, and the standard escape hatch (https://no-color.org) was ignored —NO_COLOR=1produced byte-identical output. For agent/tooling consumers the escapes are wasted bytes and transcript noise, and the workaround (--json) is ~5× the bytes of the human format.Fix
All CLI coloring already flows through the
colorspalette insrc/bin/codegraph.ts(plus a mini-palette insrc/upgrade/index.ts), so the fix is one shared gate —colorEnabled()in a newsrc/ui/colors.ts:--no-coloranywhere on the command line disables color — it's stripped from argv before commander parses (same pre-parse intercept pattern as-v), so it also works after subcommands (codegraph query foo --no-color) where commander would otherwise reject it as an unknown option; still registered on the program so it shows in--helpNO_COLORenvironment variable disables color (an emptyNO_COLORdoes not, per the spec)With color disabled the palette collapses to empty strings, so every existing
colors.Xinterpolation andchalk.X()helper becomes a plain-text no-op — no call sites change.Verification
__tests__/cli-no-color.test.ts(5 tests, end-to-end against the built binary, red before / green after): pipedquery/status/callersoutput must be escape-free;NO_COLOR=1honored;--no-coloraccepted after a subcommand; emptyNO_COLORdoes not disable colornpm run build && npm test)script -q /dev/null …),queryoutput still renders colors;--helplists the new flag[Unreleased]→ FixesNotes
src/ui/shimmer-worker.ts(index progress animation) still writes ANSI cursor control when piped — that's a rendering concern distinct from command output and untouched here; happy to follow up separately if wanted.🤖 Generated with Claude Code
https://claude.ai/code/session_01XWwajmLbgJbK6JBCY8nKfk