Repository navigation
fix(cli): disable ANSI colors for redirected output - #38
PandaHUN777 wants to merge 2 commits into
Conversation
|
Hey, this is a great PR — I really like that you checked stdout and stderr separately instead of just one global flag, and the little truth table for One thing I'd change though: reading Something more like: // command.rs — the one place that touches the environment
let no_color = env::var_os("NO_COLOR");
let output = OutputConfig {
color_stdout: color_enabled(io::stdout().is_terminal(), no_color.as_deref()),
color_stderr: color_enabled(io::stderr().is_terminal(), no_color.as_deref()),
..OutputConfig::new(args.quiet, args.verbose)
};
// console.rs — formatter just gets told yes or no
fn color(text: &str, code: &str, enabled: bool) -> String {
if enabled { format!("{code}{text}{RESET}") } else { text.to_owned() }
}
pub fn success(message: &str, config: &OutputConfig) {
if config.quiet { return; }
println!("{}", green(message, config.color_stdout));
}Then |
|
Small thing on the test side — just a heads up so you don't waste time on it. A Your unit test on let cfg = /* resolved with NO_COLOR=1 and a terminal */;
assert!(!cfg.color_stdout); |
Disables ANSI colors independently for stdout and stderr when redirected, and honors a non-empty NO_COLOR environment variable. Resolves output color policy during CLI initialization in command.rs and keeps console formatting functions pure. Closes opensource-nepal#30
0c1f4ef to
315be6c
Compare
|
Thanks for the thorough review and architectural suggestions, @JustBipin! I have refactored the color resolution to align with your design:
Validated with:
|
|
Nice — the refactor landed exactly how I hoped. Much cleaner than reading the env inside the formatter. One last thing before merge: clippy is unhappy.
The suggestion works as-is — is_terminal && no_color.is_none_or(|value| value.is_empty())Verified |
Disables ANSI colors independently for stdout and stderr when the corresponding stream is redirected, and honors a non-empty
NO_COLORenvironment variable. Adds regression tests for redirected output and the color selection rules, with a README note.Closes #30
Validation:
cargo +stable buildcargo +stable testcargo +stable fmt --all --checkcargo +stable clippy --all-targets --all-features -- -D warningsgit diff --check