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 <clio-agent@sisyphuslabs.ai>
This commit is contained in:
dailz
2026-04-12 10:51:26 +08:00
parent c914912389
commit ef4c2e7383

76
crates/tui/src/color.rs Normal file
View File

@@ -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<Color> {
name.parse::<Color>().ok()
}
#[allow(dead_code)]
pub fn level_fg(level: Option<&LogLevel>, config: &ColorConfig) -> Option<Color> {
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)
);
}
}