Disable color output when stdout is not a TTY#176
Open
rgarcia wants to merge 1 commit into
Open
Conversation
pterm v0.12.80 forces color on regardless of the output target, so the CLI emitted raw ANSI escape codes when stdout was piped or captured (e.g. in a headless/programmatic environment), garbling the output. Gate pterm styling in initConfig on an interactive-terminal check and honor the NO_COLOR convention, so non-TTY output is plain text. Interactive terminals are unaffected and the --no-color flag still works. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
|
Firetiger deploy monitoring skipped This PR didn't match the auto-monitor filter configured on your GitHub connection:
Reason: PR affects CLI color output behavior but doesn't clearly identify which service in the kernel mono repo is implicated; please confirm the affected service and opt in manually if this requires deploy monitoring. To monitor this PR anyway, reply with |
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.
Summary
When the CLI runs in a non-interactive environment (stdout piped or captured), it emitted raw ANSI escape codes instead of plain text — e.g.
^[[30;42m SUCCESS ^[[0m^[[0m. This is becausepterm(v0.12.80) forces color on regardless of the output target and does not auto-detect a TTY. The previousinitConfig()made this worse by callingpterm.EnableStyling()unconditionally.This change gates pterm styling on an interactive-terminal check:
NO_COLORis unset.--no-colorflag and its handling inPersistentPreRunEare unchanged (it runs afterinitConfigand can only further-disable).fang/lipgloss(help text and the error renderer) already strip color correctly on a non-TTY, so no change was needed there. The fix is isolated to pterm incmd/root.go.Changes
cmd/root.go: addshouldEnableColor(noColorEnv, isTTY)helper; rewriteinitConfig()to enable/disable pterm styling based on it (reusingpkg/table.IsStdoutTTY()).cmd/color_test.go: table test for the precedence logic.Test plan
go build ./...andmake buildsucceedgo test ./cmd/...passes (incl. newTestShouldEnableColor)go vet ./cmd/andgofmtcleanupgrade --dry-run) prints plainINFO:/WARNING:when piped, and that the pre-fix build leaked^[[…mon the same pathisTTY=trueunit case)Note
Low Risk
Startup-only output styling change with unit tests; no auth, data, or API behavior changes.
Overview
Fixes pterm emitting raw ANSI escapes when stdout is piped or captured by no longer calling
pterm.EnableStyling()unconditionally at startup.initConfig()now turns styling on only when stdout is a TTY andNO_COLORis unset; otherwise it callspterm.DisableStyling(). A newshouldEnableColorhelper encodes that precedence (any non-emptyNO_COLORdisables color). TTY detection reusestable.IsStdoutTTY(). Table tests cover the four combinations of TTY and env.The
--no-colorflag path inPersistentPreRunEis unchanged and can still disable styling after init.Reviewed by Cursor Bugbot for commit b86de28. Bugbot is set up for automated code reviews on this repo. Configure here.