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
Only apply colors if stderr is a terminal #127
base: main
Are you sure you want to change the base?
Conversation
@smwoj could you investigate if there's a way to force colors for our tests, even though we are not in an interactive terminal? It would be great to test both ways, but given only one I'd prefer keeping the existing tests unchanged. I had to authorize the workflow a couple of times, feel free to ping me again if you want me to run CI on this branch again |
@tommilligan the natural way to keep the existing tests (though perhaps a little bit hacky) would be to always have colors enabled if we're running tests. (keeping the existing tests is the starting point - whether we add add more tests for the nocolor mode is a separate issue) #[cfg(test)]
fn should_apply_color() -> bool {
true
}
#[cfg(not(test))]
fn should_apply_color() -> bool {
use std::io::IsTerminal;
use std::sync::OnceLock;
static IS_STDERR_TERMINAL: OnceLock<bool> = OnceLock::new();
*IS_STDERR_TERMINAL.get_or_init(|| std::io::stderr().is_terminal())
} However this doesn't work, because tests in You said that you'd prefer to keep the existing tests unchanged, but perhaps moving them to I must admit I see the value of having this kind of integration tests (that see the crate as a client would), but I don't see a way of adding no-color fallback and keeping the tests as they are in the current CI unless we add extra configuration. However this extra configuration doesn't seem to be a good idea either:
Please let me know if one of these solutions seems good enough for you or in these circumstances we should close the PR (and perhaps leave the "nocolor" setting for a PR that introduces proper configuration across the board). |
Thanks for the summary, that's a really nice writeup. I think the most natural fit would be adding a This is basically the same as your This also has the nice property that if it's a new feature, users can opt in to it so it's not a breaking change. We could also decide to make it a default feature in future. |
That's indeed a nice compromise. I pushed these changes, thanks. It should be noted that CI is running on rust 1.63, but IsTerminal trait was added in std library 1.70. I expect the CI to pass now, but users who wish to enable it will have to use at least 1.70. I think this is OK since by default there is no breakage (the feature is disabled) and if someone opts-in @tommilligan could you please rerun the CI, then? |
As discussed in #31.