pub struct AnsiColors;
#[allow(dead_code)]
impl AnsiColors {
pub const RESET: &'static str = "\x1b[0m";
pub const BOLD: &'static str = "\x1b[1m";
pub const DIM: &'static str = "\x1b[2m";
pub const BLACK: &'static str = "\x1b[30m";
pub const RED: &'static str = "\x1b[31m";
pub const GREEN: &'static str = "\x1b[32m";
pub const YELLOW: &'static str = "\x1b[33m";
pub const BLUE: &'static str = "\x1b[34m";
pub const MAGENTA: &'static str = "\x1b[35m";
pub const CYAN: &'static str = "\x1b[36m";
pub const WHITE: &'static str = "\x1b[37m";
pub const BRIGHT_BLACK: &'static str = "\x1b[90m";
pub const BRIGHT_RED: &'static str = "\x1b[91m";
pub const BRIGHT_GREEN: &'static str = "\x1b[92m";
pub const BRIGHT_YELLOW: &'static str = "\x1b[93m";
pub const BRIGHT_BLUE: &'static str = "\x1b[94m";
pub const BRIGHT_MAGENTA: &'static str = "\x1b[95m";
pub const BRIGHT_CYAN: &'static str = "\x1b[96m";
pub const BRIGHT_WHITE: &'static str = "\x1b[97m";
}
pub struct Colorizer {
enabled: bool,
}
impl Colorizer {
pub fn new(enabled: bool) -> Self {
Self { enabled }
}
pub fn error(&self, text: &str) -> String {
if self.enabled {
format!("{}Error: {}{}", AnsiColors::RED, text, AnsiColors::RESET)
} else {
format!("Error: {}", text)
}
}
pub fn field_key(&self, text: &str) -> String {
if self.enabled {
format!("{}{}{}", AnsiColors::CYAN, text, AnsiColors::RESET)
} else {
text.to_string()
}
}
pub fn string(&self, text: &str) -> String {
if self.enabled {
format!("{}'{}'{}", AnsiColors::GREEN, text, AnsiColors::RESET)
} else {
format!("'{}'", text)
}
}
pub fn number(&self, text: &str) -> String {
if self.enabled {
format!("{}{}{}", AnsiColors::YELLOW, text, AnsiColors::RESET)
} else {
text.to_string()
}
}
pub fn type_wrapper(&self, type_name: &str, value: &str) -> String {
if self.enabled {
format!(
"{}{}{}('{}{}{}')",
AnsiColors::BLUE,
type_name,
AnsiColors::RESET,
AnsiColors::YELLOW,
value,
AnsiColors::RESET
)
} else {
format!("{}('{}')", type_name, value)
}
}
pub fn iso_date(&self, iso_string: &str) -> String {
if self.enabled {
format!(
"{}ISODate{}('{}{}{}')",
AnsiColors::BLUE,
AnsiColors::RESET,
AnsiColors::GREEN,
iso_string,
AnsiColors::RESET
)
} else {
format!("ISODate('{}')", iso_string)
}
}
pub fn bin_data(&self, subtype: u8, hex_data: &str) -> String {
if self.enabled {
format!(
"{}BinData{}({}{}{}, '{}{}{}')",
AnsiColors::MAGENTA,
AnsiColors::RESET,
AnsiColors::YELLOW,
subtype,
AnsiColors::RESET,
AnsiColors::CYAN,
hex_data,
AnsiColors::RESET
)
} else {
format!("BinData({}, '{}')", subtype, hex_data)
}
}
pub fn regex(&self, pattern: &str, options: &str) -> String {
if self.enabled {
format!(
"{}{}{}{}{}{}{}{}",
AnsiColors::RED,
"/",
AnsiColors::YELLOW,
pattern,
AnsiColors::RED,
"/",
options,
AnsiColors::RESET
)
} else {
format!("/{}/{}", pattern, options)
}
}
pub fn timestamp(&self, time: u32, increment: u32) -> String {
if self.enabled {
format!(
"{}Timestamp{}({}{}{}, {}{}{})",
AnsiColors::BLUE,
AnsiColors::RESET,
AnsiColors::YELLOW,
time,
AnsiColors::RESET,
AnsiColors::YELLOW,
increment,
AnsiColors::RESET
)
} else {
format!("Timestamp({}, {})", time, increment)
}
}
pub fn null(&self, text: &str) -> String {
if self.enabled {
format!("{}{}{}", AnsiColors::BRIGHT_BLACK, text, AnsiColors::RESET)
} else {
text.to_string()
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_colorizer_no_colors() {
let colorizer = Colorizer::new(false);
let result = colorizer.error("test error");
assert_eq!(result, "Error: test error");
assert!(!result.contains("\x1b"));
}
#[test]
fn test_colorizer_with_colors() {
let colorizer = Colorizer::new(true);
let result = colorizer.string("test");
assert!(result.contains("\x1b"));
assert!(result.contains("'test'"));
}
#[test]
fn test_colorizer_field_key() {
let colorizer = Colorizer::new(true);
let result = colorizer.field_key("name");
assert!(result.contains("\x1b[36m")); assert!(result.contains("name"));
}
#[test]
fn test_colorizer_null() {
let colorizer = Colorizer::new(false);
let result = colorizer.null("null");
assert_eq!(result, "null");
}
#[test]
fn test_colorizer_type_wrapper() {
let colorizer = Colorizer::new(true);
let result = colorizer.type_wrapper("ObjectId", "65705d84dfc3f3b5094e1f72");
assert!(result.contains("ObjectId"));
assert!(result.contains("65705d84dfc3f3b5094e1f72"));
assert!(result.contains("\x1b[")); }
#[test]
fn test_colorizer_iso_date() {
let colorizer = Colorizer::new(false);
let result = colorizer.iso_date("2023-12-06T11:39:48.373Z");
assert_eq!(result, "ISODate('2023-12-06T11:39:48.373Z')");
}
}