Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: color formatting detection on stdout #2613

Merged
merged 2 commits into from
Dec 3, 2024

Conversation

blmaier
Copy link
Contributor

@blmaier blmaier commented Dec 2, 2024

Text printed to stdout by pixi has colors even when stdout is redirected to a pipe or file. For example:

$ pixi list | xxd -l 64
00000000: 1b5b 316d 5061 636b 6167 651b 5b30 6d20 .[1mPackage.[0m

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 overrides console 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 override console when pixi --color, PIXI_COLOR, or NO_COLOR are set.

As a bonus, this means pixi will now support the CLICOLOR spec that is implemented by console.

Fixes: #2536

Text printed to stdout by pixi has colors even when stdout is redirected
to a pipe or file. For example:

> $ pixi list | xxd -l 64
> 00000000: 1b5b 316d 5061 636b 6167 651b 5b30 6d20  .[1mPackage.[0m

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 overrides `console` 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 override `console` when `pixi --color`,
PIXI_COLOR, or NO_COLOR are set.

As a bonus, this means pixi will now support the `CLICOLOR` spec that is
implemented by `console`.

Fixes: prefix-dev#2536
Copy link
Contributor

@ruben-arts ruben-arts left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this fix and simplification! I guess we were not properly aware of what console was already doing! Your change works for me!

@ruben-arts ruben-arts enabled auto-merge (squash) December 2, 2024 14:20
@ruben-arts ruben-arts merged commit db5c3cf into prefix-dev:main Dec 3, 2024
40 checks passed
@blmaier blmaier deleted the fix-color branch December 3, 2024 10:39
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.

pixi list >file.txt prints control characters
3 participants