Skip to content

Apply colors only when they are supported#238

Open
nikableh wants to merge 3 commits intoeyre-rs:masterfrom
nikableh-forks:use-colors-only-when-supported
Open

Apply colors only when they are supported#238
nikableh wants to merge 3 commits intoeyre-rs:masterfrom
nikableh-forks:use-colors-only-when-supported

Conversation

@nikableh
Copy link
Copy Markdown

@nikableh nikableh commented May 22, 2025

Closes #236.
Closes #237.

This PR wraps all calls to owo_colors::OwoColorize::style with function that calls anstream::AutoStream::choice and checks whenever colors are available for the current stderr.

Conserns

The stderr here is hardcoded it that somewhat bothers me. It is possible for that user of eyre to configure printing all of the error messages into the stderr? Because if eyre ever prints color to the stdout and then the user redirects stdout to the file, this check would detect that stderr is still a terminal and allow ANSI symbols to be printed to the file.

I am also wondering if duplicating style_if_possible in color-spantrace and color-eyre is okay. Should I move it somewhere else?

Tests

I've done some quick testing by running examples and this seems to work well. I don't see any colors in the terminal and there are no special symbols from eyre when redirecting to the file.

Here is a script to run all color-eyre examples and print the output to the file. Mind that RUSTFLAGS=-Awarnings will force everything to rebuild. But that saves you from scrolling past all the deprecation warnings (fixed here btw — #235).

#!/bin/env bash

TEST_OUTPUT_DIR=test-no-color-examples
EXAMPLES='custom_filter custom_section debug_perf github_issue multiple_errors panic_compose panic_hook theme_test_helper theme color-eyre-usage'

rm -r $TEST_OUTPUT_DIR || true
mkdir -p $TEST_OUTPUT_DIR

for example in $EXAMPLES; do
    RUSTFLAGS=-Awarnings cargo run --example $example &>> $TEST_OUTPUT_DIR/$example
done

Might be nice to write some unit tests, but I'd prefer if someone else did it.

About tracing_subscriber

The only ANSI symbols I see are the ones coming from tracing_subscriber (color-eyre/examples/theme_test_helper.rs:35). I did a little digging and found that removing this layer color-eyre/examples/theme_test_helper.rs:49 from registry fixes the issue.

The colors are still there, in the terminal, but not in the file. The only thing that changed visually is that the text is no longer cursive. Although I don't quite understand what this layer does and why is it needed. The documentation is not clear to me.

I think we should just remove this layer from every example to avoid teaching people this flawed behaviour. Let's leave it up to them to enable this layer. This way it won't be eyre's problem, but tracing_subscriber's one.

Rant about owo_colors

To be fair, this all feels like a huge blunder from owo_colors. I feel like it is it who should be responsible for detecting colors. They already have a supports-color feature. But enabling it only gives you a method that you should apply everywhere yourself. I can understand why this feature isn't turned on by default, sure. But once I enable it I kind of expect everything to work out of the box. What if you have thousands of lines of code where colors are added? Do you wrap everything with if_supports_color??? This is nonsense.

@nikableh nikableh changed the title Apply colors only when they are supported (#236, #237) Apply colors only when they are supported May 27, 2025
Copy link
Copy Markdown
Contributor

@pksunkara pksunkara left a comment

Choose a reason for hiding this comment

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

Ideally, we can just call the style_if_possible function as style. From a developer's perspective, they don't care whether that function is styling or not, depending on the env vars.

@pksunkara
Copy link
Copy Markdown
Contributor

Pinging @epage so he can get more feedback on how anstream is being used in other crates.

@nikableh nikableh force-pushed the use-colors-only-when-supported branch from 99576ec to a42ea90 Compare May 30, 2025 20:21
Copy link
Copy Markdown
Contributor

@pksunkara pksunkara left a comment

Choose a reason for hiding this comment

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

Can you please do the following changes:

  1. Replace all owo_colors::style() usage with owo_colors::Style::new() (needed for 2nd change)
  2. Rename all style_if_possible to style.

@epage
Copy link
Copy Markdown

epage commented May 30, 2025

anstream is a larger dependency for just determining color choice and has specific logic dealing with its auto-adapting to old versions of Windows.

You could pull out the choice logic, calling anstyle_query directly, and adapt to the needs here.

Or eyre could do something like clap and be neutral to this and assume the caller is using anstream with ways of stripping the color.

@nikableh nikableh requested a review from pksunkara May 30, 2025 21:11
Copy link
Copy Markdown
Contributor

@pksunkara pksunkara left a comment

Choose a reason for hiding this comment

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

Looks good. I will merge once you can get CI passing.

@pksunkara
Copy link
Copy Markdown
Contributor

@synalice Can you please fix CI?

@nikableh
Copy link
Copy Markdown
Author

nikableh commented Sep 3, 2025

Sure.

I kind of forgot about that PR, sowwy.

@nikableh nikableh force-pushed the use-colors-only-when-supported branch from 5584e56 to 60cdc40 Compare September 8, 2025 10:31
@SharksT
Copy link
Copy Markdown

SharksT commented Nov 24, 2025

Any updates on this ?

@nikableh
Copy link
Copy Markdown
Author

I think I fixed all CI issue the last time I touched this MR. Not sure why it says "Some checks haven't completed yet" right now.

@nikableh nikableh marked this pull request as draft November 24, 2025 10:44
@nikableh nikableh marked this pull request as ready for review November 24, 2025 10:44
@pksunkara
Copy link
Copy Markdown
Contributor

Weird, can you rebase and push once? If it doesn't work, I will manually merge.

@nikableh nikableh force-pushed the use-colors-only-when-supported branch 2 times, most recently from 6a09c09 to a562f91 Compare November 25, 2025 11:07
@TheToddLuci0
Copy link
Copy Markdown

@pksunkara Any chance this can be merged?

@pksunkara
Copy link
Copy Markdown
Contributor

The CI is still failing with multiple different errors.

@nikableh nikableh force-pushed the use-colors-only-when-supported branch from a562f91 to 502c19b Compare January 21, 2026 14:32
@pksunkara
Copy link
Copy Markdown
Contributor

CI still failing. @synalice I will be free this week to review and merge this if you are able to get the CI passing.

@nikableh nikableh force-pushed the use-colors-only-when-supported branch from 502c19b to 642ebee Compare January 22, 2026 19:21
@pksunkara
Copy link
Copy Markdown
Contributor

Still failing. Can you please try all the commands from CI locally first? Using github actions takes a lot of time since a maintainer needs to approve every time.

@pksunkara
Copy link
Copy Markdown
Contributor

Just a note for future ref: Looks like the failures might not be relevant to this PR

@pksunkara
Copy link
Copy Markdown
Contributor

@synalice I have been unable to push to your branch. Can you please rebase on top of master? I think this can be merged now.

@nikableh nikableh force-pushed the use-colors-only-when-supported branch from 642ebee to ca5f1f4 Compare February 23, 2026 14:42
Copy link
Copy Markdown
Contributor

@pksunkara pksunkara left a comment

Choose a reason for hiding this comment

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

Looking at the failing tests, you need to enable colors in tests so that they can be tested.

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.

Redirecting stderr to file doesn't remove special ANSI characters. NO_COLOR support

5 participants