use crate::data_paths::data_path;
use anyhow::{Context, Result};
use colored::Color;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::Path;
use std::sync::{Arc, LazyLock, RwLock};
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct ThemeColor {
pub r: u8,
pub g: u8,
pub b: u8,
}
impl ThemeColor {
pub fn new(r: u8, g: u8, b: u8) -> Self {
Self { r, g, b }
}
pub fn to_color(&self) -> Color {
Color::TrueColor {
r: self.r,
g: self.g,
b: self.b,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Theme {
pub name: String,
pub description: String,
pub colors: ThemeColors,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ThemeColors {
pub prompt: ThemeColor,
pub prompt_bg: ThemeColor,
pub status: ThemeColor,
pub highlight: ThemeColor,
pub transition: ThemeColor,
pub subheading: ThemeColor,
pub item: ThemeColor,
pub item_text: ThemeColor,
pub npc: ThemeColor,
pub npc_quote: ThemeColor,
pub npc_movement: ThemeColor,
pub room: ThemeColor,
pub room_titlebar: ThemeColor,
pub description: ThemeColor,
pub overlay: ThemeColor,
pub triggered: ThemeColor,
pub trig_icon: ThemeColor,
pub ambient_icon: ThemeColor,
pub ambient_trig: ThemeColor,
pub exit_visited: ThemeColor,
pub exit_locked: ThemeColor,
pub exit_unvisited: ThemeColor,
pub error: ThemeColor,
pub error_icon: ThemeColor,
pub denied: ThemeColor,
pub goal_active: ThemeColor,
pub goal_complete: ThemeColor,
pub section: ThemeColor,
}
impl Default for Theme {
fn default() -> Self {
Self {
name: "default".to_string(),
description: "The default color scheme".to_string(),
colors: ThemeColors::default(),
}
}
}
impl Default for ThemeColors {
fn default() -> Self {
Self {
prompt: ThemeColor::new(250, 200, 100),
prompt_bg: ThemeColor::new(50, 51, 50),
status: ThemeColor::new(20, 220, 100),
highlight: ThemeColor::new(255, 255, 0), transition: ThemeColor::new(40, 210, 160),
subheading: ThemeColor::new(255, 255, 255),
item: ThemeColor::new(220, 180, 40),
item_text: ThemeColor::new(40, 180, 40),
npc: ThemeColor::new(50, 200, 50),
npc_quote: ThemeColor::new(100, 250, 250),
npc_movement: ThemeColor::new(40, 180, 220),
room: ThemeColor::new(223, 77, 10),
room_titlebar: ThemeColor::new(223, 77, 10),
description: ThemeColor::new(102, 208, 250),
overlay: ThemeColor::new(75, 180, 255),
triggered: ThemeColor::new(230, 230, 30),
trig_icon: ThemeColor::new(230, 80, 80),
ambient_icon: ThemeColor::new(80, 80, 230),
ambient_trig: ThemeColor::new(150, 230, 30),
exit_visited: ThemeColor::new(110, 220, 110),
exit_locked: ThemeColor::new(200, 50, 50),
exit_unvisited: ThemeColor::new(220, 180, 40),
error: ThemeColor::new(230, 30, 30),
error_icon: ThemeColor::new(255, 0, 0), denied: ThemeColor::new(230, 30, 30),
goal_active: ThemeColor::new(220, 40, 220),
goal_complete: ThemeColor::new(110, 20, 110),
section: ThemeColor::new(75, 80, 75),
}
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct ThemeData {
pub themes: Vec<Theme>,
}
pub struct ThemeManager {
themes: HashMap<String, Theme>,
current_theme: Arc<RwLock<Theme>>,
}
impl ThemeManager {
pub fn new() -> Self {
let mut themes = HashMap::new();
let default_theme = Theme::default();
themes.insert("default".to_string(), default_theme.clone());
Self {
themes,
current_theme: Arc::new(RwLock::new(default_theme)),
}
}
pub fn load_themes_from_file(&mut self, path: &Path) -> Result<()> {
if !path.exists() {
return Ok(());
}
let contents =
fs::read_to_string(path).with_context(|| format!("Failed to read themes file at {}", path.display()))?;
let theme_data: ThemeData = toml::from_str(&contents).with_context(|| "Failed to parse themes TOML")?;
for theme in theme_data.themes {
self.themes.insert(theme.name.clone(), theme);
}
Ok(())
}
pub fn list_themes(&self) -> Vec<String> {
let mut names: Vec<String> = self.themes.keys().cloned().collect();
names.sort();
names
}
pub fn set_theme(&self, name: &str) -> Result<()> {
let theme = self
.themes
.get(name)
.ok_or_else(|| anyhow::anyhow!("Theme '{name}' not found"))?;
let mut current = self
.current_theme
.write()
.map_err(|_| anyhow::anyhow!("Failed to acquire theme lock"))?;
*current = theme.clone();
Ok(())
}
pub fn current(&self) -> Arc<RwLock<Theme>> {
Arc::clone(&self.current_theme)
}
pub fn current_name(&self) -> String {
self.current_theme
.read()
.map_or_else(|_| "default".to_string(), |t| t.name.clone())
}
}
impl Default for ThemeManager {
fn default() -> Self {
Self::new()
}
}
pub static THEME_MANAGER: LazyLock<RwLock<ThemeManager>> = LazyLock::new(|| RwLock::new(ThemeManager::new()));
pub fn init_themes() -> Result<()> {
let themes_path = data_path("themes.toml");
let mut manager = THEME_MANAGER
.write()
.map_err(|_| anyhow::anyhow!("Failed to acquire theme manager lock"))?;
manager.load_themes_from_file(&themes_path)?;
Ok(())
}
pub fn current_theme_colors() -> ThemeColors {
THEME_MANAGER
.read()
.ok()
.and_then(|m| {
let current = m.current();
current.read().ok().map(|t| t.colors.clone())
})
.unwrap_or_default()
}