saorsa-tui 0.4.0

Retained-mode, CSS-styled terminal UI framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
//! Theme data structures for terminal UI styling.
//!
//! This module provides strongly-typed color themes with named semantic color slots,
//! theme metadata (name, author, variant), and a registry for managing multiple themes.

pub mod catppuccin;
pub mod dracula;
pub mod nord;
pub mod solarized;

use std::collections::HashMap;

use crate::Color;

pub use catppuccin::{catppuccin_frappe, catppuccin_latte, catppuccin_macchiato, catppuccin_mocha};
pub use dracula::{dracula_dark, dracula_light};
pub use nord::nord_dark;
pub use solarized::{solarized_dark, solarized_light};

/// Semantic color slots for a theme.
///
/// Each field represents a specific UI purpose, allowing themes to be
/// swapped without changing widget code.
#[derive(Clone, Debug, PartialEq)]
pub struct ThemeColors {
    /// Primary background color.
    pub background: Color,
    /// Primary foreground (text) color.
    pub foreground: Color,
    /// Accent color for highlights and interactive elements.
    pub accent: Color,
    /// Error/danger color.
    pub error: Color,
    /// Warning/caution color.
    pub warning: Color,
    /// Success/confirmation color.
    pub success: Color,
    /// Surface color (panels, cards, containers).
    pub surface: Color,
    /// Border color.
    pub border: Color,
    /// Muted/disabled color.
    pub muted: Color,
    /// Secondary text color (less prominent than foreground).
    pub text_secondary: Color,
}

impl ThemeColors {
    /// Create a new ThemeColors with all slots specified.
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        background: Color,
        foreground: Color,
        accent: Color,
        error: Color,
        warning: Color,
        success: Color,
        surface: Color,
        border: Color,
        muted: Color,
        text_secondary: Color,
    ) -> Self {
        Self {
            background,
            foreground,
            accent,
            error,
            warning,
            success,
            surface,
            border,
            muted,
            text_secondary,
        }
    }

    /// Create the default dark theme colors.
    pub fn default_dark() -> Self {
        use crate::color::NamedColor;
        Self {
            background: Color::Rgb {
                r: 30,
                g: 30,
                b: 46,
            },
            foreground: Color::Named(NamedColor::White),
            accent: Color::Rgb {
                r: 137,
                g: 180,
                b: 250,
            },
            error: Color::Rgb {
                r: 243,
                g: 139,
                b: 168,
            },
            warning: Color::Rgb {
                r: 249,
                g: 226,
                b: 175,
            },
            success: Color::Rgb {
                r: 166,
                g: 227,
                b: 161,
            },
            surface: Color::Rgb {
                r: 49,
                g: 50,
                b: 68,
            },
            border: Color::Rgb {
                r: 88,
                g: 91,
                b: 112,
            },
            muted: Color::Rgb {
                r: 108,
                g: 112,
                b: 134,
            },
            text_secondary: Color::Rgb {
                r: 186,
                g: 194,
                b: 222,
            },
        }
    }

    /// Create the default light theme colors.
    pub fn default_light() -> Self {
        use crate::color::NamedColor;
        Self {
            background: Color::Rgb {
                r: 239,
                g: 241,
                b: 245,
            },
            foreground: Color::Rgb {
                r: 76,
                g: 79,
                b: 105,
            },
            accent: Color::Rgb {
                r: 30,
                g: 102,
                b: 245,
            },
            error: Color::Rgb {
                r: 210,
                g: 15,
                b: 57,
            },
            warning: Color::Rgb {
                r: 223,
                g: 142,
                b: 29,
            },
            success: Color::Rgb {
                r: 64,
                g: 160,
                b: 43,
            },
            surface: Color::Rgb {
                r: 204,
                g: 208,
                b: 218,
            },
            border: Color::Rgb {
                r: 156,
                g: 160,
                b: 176,
            },
            muted: Color::Rgb {
                r: 140,
                g: 143,
                b: 161,
            },
            text_secondary: Color::Named(NamedColor::BrightBlack),
        }
    }
}

/// Theme variant: light or dark.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ThemeVariant {
    /// Light theme (dark text on light background).
    Light,
    /// Dark theme (light text on dark background).
    Dark,
}

/// A complete theme with colors and metadata.
#[derive(Clone, Debug, PartialEq)]
pub struct Theme {
    /// Theme name.
    pub name: String,
    /// Theme author.
    pub author: String,
    /// Theme variant (light or dark).
    pub variant: ThemeVariant,
    /// Semantic color slots.
    pub colors: ThemeColors,
}

impl Theme {
    /// Create a new theme.
    pub fn new(name: String, author: String, variant: ThemeVariant, colors: ThemeColors) -> Self {
        Self {
            name,
            author,
            variant,
            colors,
        }
    }

    /// Create the built-in default dark theme.
    pub fn default_dark() -> Self {
        Self {
            name: "Default Dark".to_string(),
            author: "Fae".to_string(),
            variant: ThemeVariant::Dark,
            colors: ThemeColors::default_dark(),
        }
    }

    /// Create the built-in default light theme.
    pub fn default_light() -> Self {
        Self {
            name: "Default Light".to_string(),
            author: "Fae".to_string(),
            variant: ThemeVariant::Light,
            colors: ThemeColors::default_light(),
        }
    }
}

/// Registry for managing multiple themes.
#[derive(Clone, Debug)]
pub struct ThemeRegistry {
    themes: HashMap<String, Theme>,
}

impl ThemeRegistry {
    /// Create a new empty registry.
    pub fn new() -> Self {
        Self {
            themes: HashMap::new(),
        }
    }

    /// Create a registry with the default dark theme pre-registered.
    pub fn with_default() -> Self {
        let mut registry = Self::new();
        registry.register_theme(Theme::default_dark());
        registry
    }

    /// Register a theme.
    ///
    /// If a theme with the same name already exists, it will be replaced.
    pub fn register_theme(&mut self, theme: Theme) {
        self.themes.insert(theme.name.clone(), theme);
    }

    /// Get a theme by name.
    pub fn get_theme(&self, name: &str) -> Option<&Theme> {
        self.themes.get(name)
    }

    /// List all registered theme names.
    pub fn list_themes(&self) -> Vec<&str> {
        self.themes.keys().map(String::as_str).collect()
    }

    /// Check if a theme is registered.
    pub fn has_theme(&self, name: &str) -> bool {
        self.themes.contains_key(name)
    }

    /// Remove a theme by name. Returns `true` if the theme was present.
    pub fn remove_theme(&mut self, name: &str) -> bool {
        self.themes.remove(name).is_some()
    }

    /// Get the number of registered themes.
    pub fn len(&self) -> usize {
        self.themes.len()
    }

    /// Check if the registry is empty.
    pub fn is_empty(&self) -> bool {
        self.themes.is_empty()
    }
}

impl Default for ThemeRegistry {
    fn default() -> Self {
        Self::with_default()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::color::NamedColor;

    #[test]
    fn theme_colors_new() {
        let colors = ThemeColors::new(
            Color::Named(NamedColor::Black),
            Color::Named(NamedColor::White),
            Color::Named(NamedColor::Blue),
            Color::Named(NamedColor::Red),
            Color::Named(NamedColor::Yellow),
            Color::Named(NamedColor::Green),
            Color::Named(NamedColor::BrightBlack),
            Color::Named(NamedColor::BrightWhite),
            Color::Named(NamedColor::BrightBlack),
            Color::Named(NamedColor::BrightWhite),
        );

        assert_eq!(colors.background, Color::Named(NamedColor::Black));
        assert_eq!(colors.foreground, Color::Named(NamedColor::White));
        assert_eq!(colors.accent, Color::Named(NamedColor::Blue));
        assert_eq!(colors.error, Color::Named(NamedColor::Red));
    }

    #[test]
    fn theme_colors_default_dark() {
        let colors = ThemeColors::default_dark();
        assert_eq!(
            colors.background,
            Color::Rgb {
                r: 30,
                g: 30,
                b: 46
            }
        );
        assert_eq!(colors.foreground, Color::Named(NamedColor::White));
    }

    #[test]
    fn theme_colors_default_light() {
        let colors = ThemeColors::default_light();
        assert_eq!(
            colors.background,
            Color::Rgb {
                r: 239,
                g: 241,
                b: 245
            }
        );
        assert_eq!(
            colors.foreground,
            Color::Rgb {
                r: 76,
                g: 79,
                b: 105
            }
        );
    }

    #[test]
    fn theme_new() {
        let colors = ThemeColors::default_dark();
        let theme = Theme::new(
            "Test".to_string(),
            "Author".to_string(),
            ThemeVariant::Dark,
            colors.clone(),
        );

        assert_eq!(theme.name, "Test");
        assert_eq!(theme.author, "Author");
        assert_eq!(theme.variant, ThemeVariant::Dark);
        assert_eq!(theme.colors, colors);
    }

    #[test]
    fn theme_default_dark() {
        let theme = Theme::default_dark();
        assert_eq!(theme.name, "Default Dark");
        assert_eq!(theme.author, "Fae");
        assert_eq!(theme.variant, ThemeVariant::Dark);
    }

    #[test]
    fn theme_default_light() {
        let theme = Theme::default_light();
        assert_eq!(theme.name, "Default Light");
        assert_eq!(theme.author, "Fae");
        assert_eq!(theme.variant, ThemeVariant::Light);
    }

    #[test]
    fn registry_new_empty() {
        let registry = ThemeRegistry::new();
        assert!(registry.is_empty());
        assert_eq!(registry.len(), 0);
    }

    #[test]
    fn registry_with_default() {
        let registry = ThemeRegistry::with_default();
        assert!(!registry.is_empty());
        assert_eq!(registry.len(), 1);
        assert!(registry.has_theme("Default Dark"));
    }

    #[test]
    fn registry_register_and_get() {
        let mut registry = ThemeRegistry::new();
        let theme = Theme::default_dark();
        registry.register_theme(theme.clone());

        assert_eq!(registry.len(), 1);
        let retrieved = registry.get_theme("Default Dark");
        assert!(retrieved.is_some());
        assert_eq!(retrieved.map(|t| t.name.as_str()), Some("Default Dark"));
    }

    #[test]
    fn registry_list_themes() {
        let mut registry = ThemeRegistry::new();
        registry.register_theme(Theme::default_dark());
        registry.register_theme(Theme::default_light());

        let mut names = registry.list_themes();
        names.sort();
        assert_eq!(names, vec!["Default Dark", "Default Light"]);
    }

    #[test]
    fn registry_has_theme() {
        let mut registry = ThemeRegistry::new();
        registry.register_theme(Theme::default_dark());

        assert!(registry.has_theme("Default Dark"));
        assert!(!registry.has_theme("Nonexistent"));
    }

    #[test]
    fn registry_remove_theme() {
        let mut registry = ThemeRegistry::new();
        registry.register_theme(Theme::default_dark());

        assert!(registry.has_theme("Default Dark"));
        assert!(registry.remove_theme("Default Dark"));
        assert!(!registry.has_theme("Default Dark"));
        assert!(!registry.remove_theme("Default Dark"));
    }

    #[test]
    fn registry_replace_theme() {
        let mut registry = ThemeRegistry::new();
        let theme1 = Theme::default_dark();
        registry.register_theme(theme1);

        let colors = ThemeColors::default_light();
        let theme2 = Theme::new(
            "Default Dark".to_string(),
            "New Author".to_string(),
            ThemeVariant::Light,
            colors,
        );
        registry.register_theme(theme2);

        assert_eq!(registry.len(), 1);
        let retrieved = registry.get_theme("Default Dark");
        assert_eq!(retrieved.map(|t| t.author.as_str()), Some("New Author"));
    }

    #[test]
    fn registry_default() {
        let registry = ThemeRegistry::default();
        assert!(!registry.is_empty());
        assert!(registry.has_theme("Default Dark"));
    }
}