use std::collections::HashMap;
#[derive(Debug, Clone)]
pub enum ColorTheme {
Default,
Dark,
Light,
Custom(HashMap<String, String>),
}
#[derive(Debug, Clone)]
pub struct LevelColors {
pub error: String,
pub warn: String,
pub info: String,
pub debug: String,
pub trace: String,
}
#[derive(Debug, Clone)]
pub struct ColorConfig {
pub enabled: bool,
pub theme: ColorTheme,
pub level_colors: LevelColors,
pub keyword_colors: HashMap<String, String>,
pub timestamp_color: String,
pub filename_color: String,
pub line_number_color: String,
}
impl Default for LevelColors {
fn default() -> Self {
Self {
error: "red".to_string(),
warn: "yellow".to_string(),
info: "green".to_string(),
debug: "blue".to_string(),
trace: "magenta".to_string(),
}
}
}
impl Default for ColorConfig {
fn default() -> Self {
Self {
enabled: true,
theme: ColorTheme::Default,
level_colors: LevelColors::default(),
keyword_colors: HashMap::new(),
timestamp_color: "cyan".to_string(),
filename_color: "white".to_string(),
line_number_color: "bright_black".to_string(),
}
}
}
pub struct ColorApplier {
config: ColorConfig,
}
impl ColorApplier {
pub fn new(config: ColorConfig) -> Self {
Self { config }
}
pub fn apply_level_color(&self, _level: &str, text: &str) -> String {
text.to_string()
}
pub fn apply_keyword_color(&self, _keyword: &str, text: &str) -> String {
text.to_string()
}
pub fn apply_timestamp_color(&self, text: &str) -> String {
text.to_string()
}
pub fn apply_filename_color(&self, text: &str) -> String {
text.to_string()
}
pub fn apply_line_number_color(&self, text: &str) -> String {
text.to_string()
}
pub fn supports_color() -> bool {
false
}
pub fn set_override(&self, _enabled: bool) {
}
}
pub struct ColorConfigBuilder {
config: ColorConfig,
}
impl ColorConfigBuilder {
pub fn new() -> Self {
Self {
config: ColorConfig::default(),
}
}
pub fn enabled(mut self, enabled: bool) -> Self {
self.config.enabled = enabled;
self
}
pub fn theme(mut self, theme: ColorTheme) -> Self {
self.config.theme = theme;
self
}
pub fn error_color<S: Into<String>>(mut self, color: S) -> Self {
self.config.level_colors.error = color.into();
self
}
pub fn warn_color<S: Into<String>>(mut self, color: S) -> Self {
self.config.level_colors.warn = color.into();
self
}
pub fn info_color<S: Into<String>>(mut self, color: S) -> Self {
self.config.level_colors.info = color.into();
self
}
pub fn debug_color<S: Into<String>>(mut self, color: S) -> Self {
self.config.level_colors.debug = color.into();
self
}
pub fn trace_color<S: Into<String>>(mut self, color: S) -> Self {
self.config.level_colors.trace = color.into();
self
}
pub fn keyword_color<K: Into<String>, C: Into<String>>(mut self, keyword: K, color: C) -> Self {
self.config.keyword_colors.insert(keyword.into(), color.into());
self
}
pub fn timestamp_color<S: Into<String>>(mut self, color: S) -> Self {
self.config.timestamp_color = color.into();
self
}
pub fn filename_color<S: Into<String>>(mut self, color: S) -> Self {
self.config.filename_color = color.into();
self
}
pub fn line_number_color<S: Into<String>>(mut self, color: S) -> Self {
self.config.line_number_color = color.into();
self
}
pub fn build(self) -> ColorConfig {
self.config
}
}
impl ColorTheme {
pub fn dark() -> Self {
ColorTheme::Dark
}
pub fn light() -> Self {
ColorTheme::Light
}
pub fn custom(colors: HashMap<String, String>) -> Self {
ColorTheme::Custom(colors)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_color_config_builder() {
let config = ColorConfigBuilder::new()
.enabled(true)
.error_color("bright_red")
.warn_color("bright_yellow")
.keyword_color("ERROR", "red")
.timestamp_color("blue")
.build();
assert!(config.enabled);
assert_eq!(config.level_colors.error, "bright_red");
assert_eq!(config.level_colors.warn, "bright_yellow");
assert_eq!(config.keyword_colors.get("ERROR"), Some(&"red".to_string()));
assert_eq!(config.timestamp_color, "blue");
}
#[test]
fn test_color_applier() {
let config = ColorConfig::default();
let applier = ColorApplier::new(config);
let colored_text = applier.apply_level_color("ERROR", "Test message");
assert_eq!(colored_text, "Test message");
}
}