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

Add --color setting to CLI. #721

Merged
merged 7 commits into from
Mar 29, 2024
Merged

Conversation

suaviloquence
Copy link
Contributor

Fixes #719

There is already code to recognize the CARGO_TERM_COLOR environment variable for setting color policy in GlobalConfig::new(). This PR makes it so that the --color=... flag takes precedence over the env var, but if the CLI flag is not set, then the env var still applies if it is set.

I'm not super happy with needing the if let block that comes with set_color_choice in order to not overwrite the env var - I'd definitely be willing to refactor the env var detection into a different function or make some other change.

I wasn't sure if tests were necessary/how to test this. If you want tests, let me know and I can add them.

Copy link
Owner

@obi1kenobi obi1kenobi left a comment

Choose a reason for hiding this comment

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

Looks great so far!

Re: testing, could you add some tests of the various --color options and env var settings into a new file in the tests directory? You can use existing tests like the ones in rustdoc_edge_cases.rs as inspiration for how to pass arguments, manipulate env vars, and check stdout / stderr to ensure the output includes / avoids color codes, as expected.

Ideally, we'd have both a "all lints passed" and a "some lints failed" run of cargo-semver-checks, and the test would ensure each of them produces the appropriate output (non-)colorization based on the env vars and CLI flags, with the appropriate precedence between them.

src/lib.rs Outdated Show resolved Hide resolved
@obi1kenobi obi1kenobi added A-cli Area: engine around the lints C-enhancement Category: raise the bar on expectations labels Mar 29, 2024
@pksunkara
Copy link
Contributor

Tangential comment. Please disregard since it's only for future note.

The recommended SOTA color-related logic is to use the rust-cli/anstyle crates.

suaviloquence and others added 3 commits March 29, 2024 12:30
messed up my rebase and left in lazy_static
Co-authored-by: Predrag Gruevski <2348618+obi1kenobi@users.noreply.github.com>
@suaviloquence
Copy link
Contributor Author

Added some tests in the tests/color_config.rs. Right now it just tests for the presence/absense of the ANSI color sequence \u{1b}[ in stderr based on the flag/env var combination. I didn't do anything with auto because I'm not sure how to emulate a tty with assert-cmd. I also introduced the predicates crate as a direct dev dependency (it was already required by assert_cmd; I just put it in Cargo.lock) in order to test if the stderr contained the sequence vs. matching the whole input which wouldn't be static (because of timing, for one).

Copy link
Owner

@obi1kenobi obi1kenobi left a comment

Choose a reason for hiding this comment

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

Nicely done, thank you! I'm going to slip in a couple of tiny tweaks for a couple of nitpicks I had, and mark for merge as soon as CI passes.

src/main.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
tests/color_config.rs Outdated Show resolved Hide resolved
@obi1kenobi obi1kenobi changed the title add --color setting Add --color setting to CLI. Mar 29, 2024
@obi1kenobi
Copy link
Owner

Added some tests in the tests/color_config.rs. Right now it just tests for the presence/absense of the ANSI color sequence \u{1b}[ in stderr based on the flag/env var combination. I didn't do anything with auto because I'm not sure how to emulate a tty with assert-cmd. I also introduced the predicates crate as a direct dev dependency (it was already required by assert_cmd; I just put it in Cargo.lock) in order to test if the stderr contained the sequence vs. matching the whole input which wouldn't be static (because of timing, for one).

Also, this explanation was great! It was very useful context that helped me understand the pull request much more easily. Thanks for putting it together!

@obi1kenobi obi1kenobi enabled auto-merge (squash) March 29, 2024 20:50
@obi1kenobi obi1kenobi merged commit ee67123 into obi1kenobi:main Mar 29, 2024
33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-cli Area: engine around the lints C-enhancement Category: raise the bar on expectations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support flags like --color=always and --color=never
3 participants