use ratatui::style::Color;
use std::collections::HashMap;
#[allow(dead_code)]
#[derive(Clone, Debug, PartialEq)]
pub struct Theme {
pub cursor_normal: Color,
pub cursor_insert: Color,
pub cursor_visual: Color,
pub border: Color,
pub line_number_fg: Color,
pub statusbar_fg: Color,
pub statusbar_bg: Color,
pub candidate_fg: Color,
pub candidate_bg: Color,
pub candidate_sel_bg: Color,
pub echo_fg: Color,
}
impl Default for Theme {
fn default() -> Self {
Self {
cursor_normal: Color::Yellow,
cursor_insert: Color::White,
cursor_visual: Color::Yellow,
border: Color::Cyan,
line_number_fg: Color::DarkGray,
statusbar_fg: Color::Black,
statusbar_bg: Color::Cyan,
candidate_fg: Color::Black,
candidate_bg: Color::White,
candidate_sel_bg: Color::DarkGray,
echo_fg: Color::White,
}
}
}
impl Theme {
pub fn from_config(config: &HashMap<String, String>) -> Self {
let mut theme = Self::default();
let get = |name: &str| config.get(name).and_then(|v| parse_color(v));
if let Some(c) = get("theme.cursor-normal") {
theme.cursor_normal = c;
}
if let Some(c) = get("theme.cursor-insert") {
theme.cursor_insert = c;
}
if let Some(c) = get("theme.cursor-visual") {
theme.cursor_visual = c;
}
if let Some(c) = get("theme.border") {
theme.border = c;
}
if let Some(c) = get("theme.line-number-fg") {
theme.line_number_fg = c;
}
if let Some(c) = get("theme.statusbar-fg") {
theme.statusbar_fg = c;
}
if let Some(c) = get("theme.statusbar-bg") {
theme.statusbar_bg = c;
}
if let Some(c) = get("theme.candidate-fg") {
theme.candidate_fg = c;
}
if let Some(c) = get("theme.candidate-bg") {
theme.candidate_bg = c;
}
if let Some(c) = get("theme.candidate-sel-bg") {
theme.candidate_sel_bg = c;
}
if let Some(c) = get("theme.echo-fg") {
theme.echo_fg = c;
}
theme
}
}
pub fn parse_color(s: &str) -> Option<Color> {
let s = s.trim();
match s.to_ascii_lowercase().as_str() {
"black" => Some(Color::Black),
"red" => Some(Color::Red),
"green" => Some(Color::Green),
"yellow" => Some(Color::Yellow),
"blue" => Some(Color::Blue),
"magenta" => Some(Color::Magenta),
"cyan" => Some(Color::Cyan),
"gray" | "grey" => Some(Color::Gray),
"darkgray" | "dark-grey" => Some(Color::DarkGray),
"white" => Some(Color::White),
"reset" => Some(Color::Reset),
_ => {
let hex = s.strip_prefix('#')?;
if hex.len() == 6 {
let r = u8::from_str_radix(&hex[0..2], 16).ok()?;
let g = u8::from_str_radix(&hex[2..4], 16).ok()?;
let b = u8::from_str_radix(&hex[4..6], 16).ok()?;
Some(Color::Rgb(r, g, b))
} else {
None
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_named_colors() {
assert_eq!(parse_color("yellow"), Some(Color::Yellow));
assert_eq!(parse_color("MAGENTA"), Some(Color::Magenta));
assert_eq!(parse_color("cyan"), Some(Color::Cyan));
}
#[test]
fn parse_hex_colors() {
assert_eq!(parse_color("#ff0000"), Some(Color::Rgb(255, 0, 0)));
assert_eq!(parse_color("#00ff00"), Some(Color::Rgb(0, 255, 0)));
assert_eq!(parse_color("#000000"), Some(Color::Rgb(0, 0, 0)));
}
#[test]
fn parse_invalid_returns_none() {
assert_eq!(parse_color("notacolor"), None);
assert_eq!(parse_color("#ff00"), None);
assert_eq!(parse_color("#zzzzzz"), None);
}
#[test]
fn from_config_overrides_defaults() {
let mut config = HashMap::new();
config.insert("theme.cursor-normal".to_string(), "magenta".to_string());
let theme = Theme::from_config(&config);
assert_eq!(theme.cursor_normal, Color::Magenta);
assert_eq!(theme.cursor_insert, Color::White);
}
#[test]
fn defaults_match_classic_look() {
let t = Theme::default();
assert_eq!(t.cursor_normal, Color::Yellow);
assert_eq!(t.cursor_visual, Color::Yellow);
assert_eq!(t.cursor_insert, Color::White);
}
}