fix: color formatting detection on stdout #2613
Merged
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.
Text printed to stdout by pixi has colors even when stdout is redirected to a pipe or file. For example:
It is a common feature for command-line tools to automatically detect if stdout is a pipe or file and disable colors. And in fact pixi uses the crate
console
for color formatting which has built-in support for detecting if stdout and stderr are terminals and automatically disabling color output, so this should work.However pixi does its own color management which overrides how
console
manages color styling. Pixi checks if only stderr is a terminal, and based on that overridesconsole
handling of stdout and stderr. Thus causing stdout to be color formatted when stdout is redirected to a file but stderr isn't.Since
console
already has detection for both stdout and stderr, lets default to that. We only overrideconsole
whenpixi --color
, PIXI_COLOR, or NO_COLOR are set.As a bonus, this means pixi will now support the
CLICOLOR
spec that is implemented byconsole
.Fixes: #2536