log-full 0.0.1

A simple, asynchronous log library
Documentation
//! 颜色输出模块
//! 
//! 提供增强的颜色输出功能

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) {
        // No color support
    }
}

/// 颜色配置构建器
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");
    }
}