use crate::palette::ansi;
#[must_use]
pub fn detect_color_support() -> bool {
if std::env::var("NO_COLOR").is_ok() {
return false;
}
if std::env::var("FORCE_COLOR").is_ok() {
return true;
}
if std::env::var("CI").is_ok() {
return true;
}
if let Ok(term) = std::env::var("TERM") {
return term != "dumb";
}
false
}
#[must_use]
pub fn colorize(text: &str, color: &str, enabled: bool) -> String {
if enabled {
format!("{}{}{}", color, text, ansi::RESET)
} else {
text.to_string()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::palette::ansi;
#[test]
fn colorize_enabled_wraps_with_ansi() {
let result = colorize("hello", ansi::GREEN, true);
assert_eq!(result, format!("{}hello{}", ansi::GREEN, ansi::RESET));
}
#[test]
fn colorize_disabled_returns_plain_text() {
let result = colorize("hello", ansi::RED, false);
assert_eq!(result, "hello");
}
#[test]
fn colorize_empty_text() {
let result = colorize("", ansi::CYAN, true);
assert_eq!(result, format!("{}{}", ansi::CYAN, ansi::RESET));
}
#[test]
fn detect_color_support_returns_bool() {
let _supports_color: bool = detect_color_support();
}
}