use std::io::IsTerminal;
pub fn is_color_enabled(no_color_flag: bool) -> bool {
if no_color_flag {
return false;
}
if std::env::var("NO_COLOR").is_ok() {
return false;
}
std::io::stdout().is_terminal()
}
pub fn green(text: &str, enabled: bool) -> String {
if enabled {
format!("\x1b[32m{text}\x1b[0m")
} else {
text.to_string()
}
}
pub fn red(text: &str, enabled: bool) -> String {
if enabled {
format!("\x1b[31m{text}\x1b[0m")
} else {
text.to_string()
}
}
pub fn dim(text: &str, enabled: bool) -> String {
if enabled {
format!("\x1b[2m{text}\x1b[0m")
} else {
text.to_string()
}
}
pub fn bold(text: &str, enabled: bool) -> String {
if enabled {
format!("\x1b[1m{text}\x1b[0m")
} else {
text.to_string()
}
}
pub fn checkmark(color_enabled: bool) -> &'static str {
if color_enabled {
"\x1b[32m\u{2713}\x1b[0m"
} else {
"OK"
}
}
pub fn cross(color_enabled: bool) -> &'static str {
if color_enabled {
"\x1b[31m\u{2717}\x1b[0m"
} else {
"ERR"
}
}
pub fn bullet(color_enabled: bool) -> &'static str {
if color_enabled {
" \u{00b7}"
} else {
" -"
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_is_color_enabled_no_color_flag_overrides_tty() {
assert!(!is_color_enabled(true));
}
#[test]
fn test_checkmark_returns_ok_when_color_disabled() {
assert_eq!(checkmark(false), "OK");
}
#[test]
fn test_checkmark_returns_ansi_when_color_enabled() {
let mark = checkmark(true);
assert!(
mark.contains('\u{2713}'),
"should contain checkmark unicode char"
);
assert!(mark.contains("\x1b[32m"), "should contain green ANSI code");
}
#[test]
fn test_cross_returns_err_when_color_disabled() {
assert_eq!(cross(false), "ERR");
}
#[test]
fn test_bullet_returns_hyphen_when_color_disabled() {
assert_eq!(bullet(false), " -");
}
#[test]
fn test_green_passthrough_when_disabled() {
assert_eq!(green("hello", false), "hello");
}
#[test]
fn test_green_wraps_ansi_when_enabled() {
let result = green("hello", true);
assert!(result.contains("\x1b[32m"));
assert!(result.contains("hello"));
assert!(result.contains("\x1b[0m"));
}
#[test]
fn test_red_passthrough_when_disabled() {
assert_eq!(red("error", false), "error");
}
#[test]
fn test_dim_passthrough_when_disabled() {
assert_eq!(dim("info", false), "info");
}
#[test]
fn test_bold_passthrough_when_disabled() {
assert_eq!(bold("title", false), "title");
}
}