pub(crate) fn use_color(stream_is_tty: bool) -> bool {
if std::env::var_os("NO_COLOR").is_some() {
return false;
}
stream_is_tty
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn use_color_respects_no_color_then_tty() {
let saved = std::env::var_os("NO_COLOR");
std::env::set_var("NO_COLOR", "1");
assert!(!use_color(true), "NO_COLOR set must force plain on a TTY");
assert!(
!use_color(false),
"NO_COLOR set must force plain when piped"
);
std::env::set_var("NO_COLOR", "");
assert!(!use_color(true), "empty NO_COLOR still disables color");
std::env::remove_var("NO_COLOR");
assert!(use_color(true), "TTY with NO_COLOR unset should color");
assert!(!use_color(false), "piped with NO_COLOR unset stays plain");
match saved {
Some(v) => std::env::set_var("NO_COLOR", v),
None => std::env::remove_var("NO_COLOR"),
}
}
}