use log::Level;
use std::env;
const RED: &str = "\x1b[31m"; const YELLOW: &str = "\x1b[33m"; const BLUE: &str = "\x1b[34m"; const CYAN: &str = "\x1b[36m"; const GRAY: &str = "\x1b[90m"; const RESET: &str = "\x1b[0m";
pub fn level_color(level: Level) -> &'static str {
match level {
Level::Error => RED,
Level::Warn => YELLOW,
Level::Info => BLUE,
Level::Debug => CYAN,
Level::Trace => GRAY,
}
}
pub fn colorize(text: &str, color: &str) -> String {
if !supports_color() {
return text.to_string();
}
format!("{}{}{}", color, text, RESET)
}
pub fn supports_color() -> bool {
if env::var("NO_COLOR").is_ok() {
return false;
}
if let Ok(term) = env::var("TERM") {
if term.contains("xterm") || term.contains("256color") {
return true;
}
}
if cfg!(windows) {
if env::var("MSYSTEM").is_ok() {
return true;
}
if env::var("ConEmuANSI").as_deref() == Ok("ON") || env::var("ANSICON").is_ok() {
return true;
}
}
if let Ok(colorterm) = env::var("COLORTERM") {
if !colorterm.is_empty() {
return true;
}
}
false
}