From ef4c2e7383ae40a43d39607fa835499a6453dd83 Mon Sep 17 00:00:00 2001 From: dailz Date: Sun, 12 Apr 2026 10:51:26 +0800 Subject: [PATCH] feat(tui): add color mapping module Add color.rs with parse_color (string to ratatui::Color), level_fg (LogLevel to color via ColorConfig), and AVAILABLE_COLORS constant for the settings panel. Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus --- crates/tui/src/color.rs | 76 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 crates/tui/src/color.rs diff --git a/crates/tui/src/color.rs b/crates/tui/src/color.rs new file mode 100644 index 0000000..ee2d56b --- /dev/null +++ b/crates/tui/src/color.rs @@ -0,0 +1,76 @@ +use ratatui::style::Color; + +use log_viewer_core::config::ColorConfig; +use log_viewer_core::types::LogLevel; + +#[allow(dead_code)] +pub const AVAILABLE_COLORS: &[&str] = &[ + "red", "green", "yellow", "blue", "magenta", "cyan", "gray", "white", +]; + +#[allow(dead_code)] +pub fn parse_color(name: &str) -> Option { + name.parse::().ok() +} + +#[allow(dead_code)] +pub fn level_fg(level: Option<&LogLevel>, config: &ColorConfig) -> Option { + let color_name = match level? { + LogLevel::Error => &config.error, + LogLevel::Warn => &config.warn, + LogLevel::Info => &config.info, + LogLevel::Debug => &config.debug, + LogLevel::Trace => &config.trace, + LogLevel::Unknown(_) => &config.unknown, + }; + parse_color(color_name) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_parse_color_valid() { + assert_eq!(parse_color("red"), Some(Color::Red)); + } + + #[test] + fn test_parse_color_invalid() { + assert_eq!(parse_color("not_a_color"), None); + } + + #[test] + fn test_parse_color_darkgray() { + assert_eq!(parse_color("darkgray"), Some(Color::DarkGray)); + } + + #[test] + fn test_level_fg_none() { + let config = ColorConfig::default(); + assert_eq!(level_fg(None, &config), None); + } + + #[test] + fn test_level_fg_error() { + let config = ColorConfig::default(); + assert_eq!(level_fg(Some(&LogLevel::Error), &config), Some(Color::Red)); + } + + #[test] + fn test_level_fg_all_levels() { + let config = ColorConfig::default(); + assert_eq!(level_fg(Some(&LogLevel::Error), &config), Some(Color::Red)); + assert_eq!( + level_fg(Some(&LogLevel::Warn), &config), + Some(Color::Yellow) + ); + assert_eq!(level_fg(Some(&LogLevel::Info), &config), Some(Color::Green)); + assert_eq!(level_fg(Some(&LogLevel::Debug), &config), Some(Color::Blue)); + assert_eq!(level_fg(Some(&LogLevel::Trace), &config), Some(Color::Cyan)); + assert_eq!( + level_fg(Some(&LogLevel::Unknown("custom".into())), &config), + Some(Color::Gray) + ); + } +}