use std::io::IsTerminal;
fn enabled() -> bool {
std::env::var_os("NO_COLOR").is_none() && std::io::stdout().is_terminal()
}
fn paint(s: &str, code: &str) -> String {
if enabled() { format!("\x1b[{code}m{s}\x1b[0m") } else { s.to_string() }
}
pub fn green(s: &str) -> String { paint(s, "32") }
pub fn red(s: &str) -> String { paint(s, "31") }
pub fn yellow(s: &str) -> String { paint(s, "33") }
pub fn bold(s: &str) -> String { paint(s, "1") }
pub fn dim(s: &str) -> String { paint(s, "2") }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn paint_wraps_in_escape_codes_when_forced_on() {
std::env::set_var("NO_COLOR", "1");
assert_eq!(green("ok"), "ok", "NO_COLOR must disable color entirely");
std::env::remove_var("NO_COLOR");
}
#[test]
fn disabled_when_not_a_terminal() {
assert_eq!(red("x"), "x");
assert_eq!(bold("x"), "x");
}
}