use ratatui::style::{Color, Style};
use std::collections::HashMap;
#[derive(Debug, Default, Clone)]
pub struct Theme {
pub name: String,
pub styles: HashMap<String, Style>,
pub colors: HashMap<String, Color>,
}
impl Theme {
pub fn new(name: &str) -> Self {
Self {
name: name.to_string(),
styles: HashMap::new(),
colors: HashMap::new(),
}
}
pub fn add_style(mut self, name: &str, style: Style) -> Self {
self.styles.insert(name.to_string(), style);
self
}
pub fn add_color(mut self, name: &str, color: Color) -> Self {
self.colors.insert(name.to_string(), color);
self
}
pub fn get_style(&self, key: &str) -> Style {
self.styles.get(key).cloned().unwrap_or_default()
}
pub fn get_color(&self, key: &str) -> Color {
self.colors.get(key).cloned().unwrap_or(Color::Reset)
}
}
#[derive(Debug, Default, Clone)]
pub struct ThemeManager {
themes: HashMap<String, Theme>,
active_theme_name: Option<String>,
}
impl ThemeManager {
pub fn new() -> Self {
Self::default()
}
pub fn add_theme(&mut self, theme: Theme) {
self.themes.insert(theme.name.clone(), theme);
}
pub fn set_active_theme(&mut self, name: &str) {
if !self.themes.contains_key(name) {
eprintln!("Warning: Theme '{}' not found.", name);
}
self.active_theme_name = Some(name.to_string());
}
pub fn get_active_theme(&self) -> Option<&Theme> {
self.active_theme_name
.as_ref()
.and_then(|name| self.themes.get(name))
}
pub fn get_current_style(&self, key: &str) -> Style {
self.get_active_theme()
.map(|theme| theme.get_style(key))
.unwrap_or_default()
}
pub fn get_current_color(&self, key: &str) -> Color {
self.get_active_theme()
.map(|theme| theme.get_color(key))
.unwrap_or(Color::Reset)
}
pub fn has_active_theme(&self) -> bool {
self.active_theme_name.is_some()
}
}