use std::io::{self, IsTerminal};
pub fn color_enabled() -> bool {
std::env::var_os("NO_COLOR").is_none()
}
pub fn color_on_stdout() -> bool {
color_enabled() && io::stdout().is_terminal()
}
pub fn color_on_stderr() -> bool {
color_enabled() && io::stderr().is_terminal()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_env;
#[test]
fn respects_no_color() {
let _guard = test_env::lock();
unsafe {
std::env::remove_var("NO_COLOR");
}
assert!(color_enabled());
unsafe {
std::env::set_var("NO_COLOR", "1");
}
assert!(!color_enabled());
assert!(!color_on_stdout());
assert!(!color_on_stderr());
unsafe {
std::env::remove_var("NO_COLOR");
}
}
}