Skip to main content

oo_ide/
theme.rs

1//! Theme system — loads colour palettes from YAML and exposes typed accessors.
2//!
3//! Resolution order for a theme named `<name>`:
4//!   1. `.oo/themes/<name>.yaml`          (project-local override)
5//!   2. `~/.config/oo/themes/<name>.yaml` (global user override)
6//!   3. Built-in embedded `dark` / `light` (compiled-in via `include_str!`)
7//!
8//! Non-base themes may declare `extends: dark` or `extends: light`; missing
9//! keys are then inherited from the parent.  Base themes must define every key.
10//!
11//! Per-key overrides can be set in settings under `theme.colors.<field>`.
12
13use ratatui::style::Color;
14use saphyr::{LoadableYamlNode, Yaml};
15
16use crate::settings::Settings;
17
18// ---------------------------------------------------------------------------
19// Embedded base themes
20// ---------------------------------------------------------------------------
21
22const DARK_YAML: &str = include_str!("../data/themes/dark.yaml");
23const LIGHT_YAML: &str = include_str!("../data/themes/light.yaml");
24
25// ---------------------------------------------------------------------------
26// Theme struct
27// ---------------------------------------------------------------------------
28
29/// Resolved colour palette.  All colours are stored as `ratatui::style::Color`.
30#[derive(Debug, Clone)]
31pub struct Theme {
32    bg: Color,
33    bg_active: Color,
34    bg_inactive: Color,
35    fg: Color,
36    fg_dim: Color,
37    fg_active: Color,
38    accent: Color,
39    selection_bg: Color,
40    selection_fg: Color,
41    level_error: Color,
42    level_warn: Color,
43    level_info: Color,
44    level_success: Color,
45    level_debug: Color,
46    level_trace: Color,
47    tree_dir: Color,
48    status_bar_bg: Color,
49    status_bar_fg: Color,
50}
51
52impl Theme {
53    // ── Accessors ─────────────────────────────────────────────────────────
54
55    pub fn bg(&self) -> Color { self.bg }
56    pub fn bg_active(&self) -> Color { self.bg_active }
57    pub fn bg_inactive(&self) -> Color { self.bg_inactive }
58    pub fn fg(&self) -> Color { self.fg }
59    pub fn fg_dim(&self) -> Color { self.fg_dim }
60    pub fn fg_active(&self) -> Color { self.fg_active }
61    pub fn accent(&self) -> Color { self.accent }
62    pub fn selection_bg(&self) -> Color { self.selection_bg }
63    pub fn selection_fg(&self) -> Color { self.selection_fg }
64    pub fn level_error(&self) -> Color { self.level_error }
65    pub fn level_warn(&self) -> Color { self.level_warn }
66    pub fn level_info(&self) -> Color { self.level_info }
67    pub fn level_success(&self) -> Color { self.level_success }
68    pub fn level_debug(&self) -> Color { self.level_debug }
69    pub fn level_trace(&self) -> Color { self.level_trace }
70    pub fn tree_dir(&self) -> Color { self.tree_dir }
71    pub fn status_bar_bg(&self) -> Color { self.status_bar_bg }
72    pub fn status_bar_fg(&self) -> Color { self.status_bar_fg }
73
74    // ── Resolution ────────────────────────────────────────────────────────
75
76    /// Build a `Theme` from settings.
77    ///
78    /// Reads `theme.name` (default `"dark"`), locates the YAML, resolves
79    /// inheritance from a base theme, then applies any per-key overrides
80    /// from `theme.colors.*` settings keys.
81    pub fn resolve(settings: &Settings) -> Self {
82        let theme_name = settings
83            .get_optional::<String>("theme.name")
84            .cloned()
85            .unwrap_or_else(|| "dark".to_owned());
86        let name: &str = &theme_name;
87
88        let mut theme = Self::load_named(name, settings);
89
90        // Apply per-key overrides from settings.
91        macro_rules! override_key {
92            ($field:ident) => {
93                let key = concat!("theme.colors.", stringify!($field));
94                if let Some(val) = settings.get_optional::<String>(key) {
95                    theme.$field = parse_color(val);
96                }
97            };
98        }
99        override_key!(bg);
100        override_key!(bg_active);
101        override_key!(bg_inactive);
102        override_key!(fg);
103        override_key!(fg_dim);
104        override_key!(fg_active);
105        override_key!(accent);
106        override_key!(selection_bg);
107        override_key!(selection_fg);
108        override_key!(level_error);
109        override_key!(level_warn);
110        override_key!(level_info);
111        override_key!(level_success);
112        override_key!(level_debug);
113        override_key!(level_trace);
114        override_key!(tree_dir);
115        override_key!(status_bar_bg);
116        override_key!(status_bar_fg);
117
118        theme
119    }
120
121    // ── Internals ─────────────────────────────────────────────────────────
122
123    fn load_named(name: &str, settings: &Settings) -> Self {
124        // Try to find an external YAML first.
125        let yaml_text = Self::find_external_yaml(name, settings)
126            .or_else(|| embedded_yaml(name))
127            .unwrap_or_else(|| {
128                log::warn!("Theme '{}' not found, falling back to 'dark'", name);
129                DARK_YAML.to_owned()
130            });
131
132        Self::parse_yaml_with_inheritance(&yaml_text, settings)
133    }
134
135    fn find_external_yaml(name: &str, settings: &Settings) -> Option<String> {
136        let filename = format!("{}.yaml", name);
137
138        // 1. Project-local: .oo/themes/<name>.yaml
139        let project_theme = settings.project_config_dir().join("themes").join(&filename);
140        if project_theme.exists()
141            && let Ok(text) = std::fs::read_to_string(&project_theme) {
142                return Some(text);
143            }
144
145        // 2. Global: ~/.config/oo/themes/<name>.yaml
146        let global_theme = settings.global_config_dir().join("themes").join(&filename);
147        if global_theme.exists()
148            && let Ok(text) = std::fs::read_to_string(&global_theme) {
149                return Some(text);
150            }
151
152        None
153    }
154
155    /// Parse a theme YAML, optionally inheriting from a base theme.
156    fn parse_yaml_with_inheritance(yaml_text: &str, settings: &Settings) -> Self {
157        let map = load_yaml_map(yaml_text);
158
159        // Check for `extends` key — must be "dark" or "light".
160        let parent = map
161            .get("extends")
162            .and_then(|v| embedded_yaml(v.as_str()));
163
164        let mut base = if let Some(parent_yaml) = parent {
165            // Load parent first (always a built-in base, no further inheritance).
166            Self::from_yaml_map(&load_yaml_map(&parent_yaml))
167        } else {
168            // No extends → treat as self-contained base.
169            Self::dark_fallback()
170        };
171
172        // Overlay keys present in the child theme.
173        Self::apply_map_to(&map, &mut base, settings);
174        base
175    }
176
177    fn from_yaml_map(map: &std::collections::HashMap<String, String>) -> Self {
178        let get = |key: &str| {
179            map.get(key)
180                .map(|s| parse_color(s))
181                .unwrap_or(Color::Reset)
182        };
183        Self {
184            bg: get("bg"),
185            bg_active: get("bg_active"),
186            bg_inactive: get("bg_inactive"),
187            fg: get("fg"),
188            fg_dim: get("fg_dim"),
189            fg_active: get("fg_active"),
190            accent: get("accent"),
191            selection_bg: get("selection_bg"),
192            selection_fg: get("selection_fg"),
193            level_error: get("level_error"),
194            level_warn: get("level_warn"),
195            level_info: get("level_info"),
196            level_success: get("level_success"),
197            level_debug: get("level_debug"),
198            level_trace: get("level_trace"),
199            tree_dir: get("tree_dir"),
200            status_bar_bg: get("status_bar_bg"),
201            status_bar_fg: get("status_bar_fg"),
202        }
203    }
204
205    fn apply_map_to(
206        map: &std::collections::HashMap<String, String>,
207        base: &mut Self,
208        _settings: &Settings,
209    ) {
210        macro_rules! apply {
211            ($field:ident) => {
212                if let Some(v) = map.get(stringify!($field)) {
213                    base.$field = parse_color(v);
214                }
215            };
216        }
217        apply!(bg);
218        apply!(bg_active);
219        apply!(bg_inactive);
220        apply!(fg);
221        apply!(fg_dim);
222        apply!(fg_active);
223        apply!(accent);
224        apply!(selection_bg);
225        apply!(selection_fg);
226        apply!(level_error);
227        apply!(level_warn);
228        apply!(level_info);
229        apply!(level_success);
230        apply!(level_debug);
231        apply!(level_trace);
232        apply!(tree_dir);
233        apply!(status_bar_bg);
234        apply!(status_bar_fg);
235    }
236
237    fn dark_fallback() -> Self {
238        Self::from_yaml_map(&load_yaml_map(DARK_YAML))
239    }
240}
241
242// ---------------------------------------------------------------------------
243// Helpers
244// ---------------------------------------------------------------------------
245
246fn embedded_yaml(name: &str) -> Option<String> {
247    match name {
248        "dark" => Some(DARK_YAML.to_owned()),
249        "light" => Some(LIGHT_YAML.to_owned()),
250        _ => None,
251    }
252}
253
254/// Parse a flat YAML file into a `String → String` map.
255fn load_yaml_map(text: &str) -> std::collections::HashMap<String, String> {
256    let mut map = std::collections::HashMap::new();
257    let nodes: Vec<Yaml> = match Yaml::load_from_str(text) {
258        Ok(n) => n,
259        Err(e) => {
260            log::error!("Failed to parse theme YAML: {e}");
261            return map;
262        }
263    };
264    if let Some(Yaml::Mapping(mapping)) = nodes.first() {
265        for (k, v) in mapping.iter() {
266            if let (Some(key), Some(val)) = (k.as_str(), v.as_str()) {
267                map.insert(key.to_owned(), val.to_owned());
268            }
269        }
270    }
271    map
272}
273
274/// Parse a colour string into a `ratatui::style::Color`.
275///
276/// Supports:
277/// - `"default"` → `Color::Reset`
278/// - `"#RRGGBB"` → `Color::Rgb(r, g, b)`
279/// - Named terminal colours: `black`, `red`, `green`, `yellow`, `blue`,
280///   `magenta`, `cyan`, `white`, `gray` / `grey`, `darkgray` / `darkgrey`
281pub fn parse_color(s: &str) -> Color {
282    let s = s.trim();
283    if s.eq_ignore_ascii_case("default") || s.eq_ignore_ascii_case("reset") {
284        return Color::Reset;
285    }
286    if let Some(hex) = s.strip_prefix('#')
287        && hex.len() == 6
288            && let (Ok(r), Ok(g), Ok(b)) = (
289                u8::from_str_radix(&hex[0..2], 16),
290                u8::from_str_radix(&hex[2..4], 16),
291                u8::from_str_radix(&hex[4..6], 16),
292            ) {
293                return Color::Rgb(r, g, b);
294            }
295    match s.to_ascii_lowercase().as_str() {
296        "black" => Color::Black,
297        "red" => Color::Red,
298        "green" => Color::Green,
299        "yellow" => Color::Yellow,
300        "blue" => Color::Blue,
301        "magenta" => Color::Magenta,
302        "cyan" => Color::Cyan,
303        "white" => Color::White,
304        "gray" | "grey" => Color::Gray,
305        "darkgray" | "darkgrey" | "dark_gray" | "dark_grey" => Color::DarkGray,
306        other => {
307            log::warn!("Unknown colour '{}', using Reset", other);
308            Color::Reset
309        }
310    }
311}