frankensearch-tui 0.1.0

Shared TUI framework for frankensearch products (fsfs, ops TUI)
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
//! Color schemes, theme presets, and theming infrastructure.
//!
//! Provides a [`Theme`] type that defines colors for all TUI surfaces.
//! Ships with dark and light presets. Product crates can define custom
//! themes by implementing [`ColorScheme`].

use ftui_render::cell::PackedRgba;
use serde::{Deserialize, Serialize};

// ─── Theme Presets ──────────────────────────────────────────────────────────

/// Built-in theme presets.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ThemePreset {
    /// Dark background theme (default).
    Dark,
    /// Light background theme.
    Light,
    /// Deep purple bg, neon cyan fg, hot pink accent.
    CyberpunkAurora,
    /// IntelliJ-style dark gray, soft blue fg, blue accent.
    Darcula,
    /// Nord-palette blue-gray bg, white fg, cool blue accent.
    NordicFrost,
    /// Warm white bg, dark text, amber accent.
    LumenLight,
}

impl ThemePreset {
    /// All presets in cycling order.
    pub const ALL: [Self; 6] = [
        Self::Dark,
        Self::Light,
        Self::CyberpunkAurora,
        Self::Darcula,
        Self::NordicFrost,
        Self::LumenLight,
    ];

    /// Whether this preset uses a light background.
    #[must_use]
    pub const fn is_light(self) -> bool {
        matches!(self, Self::Light | Self::LumenLight)
    }

    /// Advance to the next preset (wrapping).
    #[must_use]
    pub const fn next(self) -> Self {
        match self {
            Self::Dark => Self::Light,
            Self::Light => Self::CyberpunkAurora,
            Self::CyberpunkAurora => Self::Darcula,
            Self::Darcula => Self::NordicFrost,
            Self::NordicFrost => Self::LumenLight,
            Self::LumenLight => Self::Dark,
        }
    }
}

impl std::fmt::Display for ThemePreset {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Dark => write!(f, "dark"),
            Self::Light => write!(f, "light"),
            Self::CyberpunkAurora => write!(f, "cyberpunk_aurora"),
            Self::Darcula => write!(f, "darcula"),
            Self::NordicFrost => write!(f, "nordic_frost"),
            Self::LumenLight => write!(f, "lumen_light"),
        }
    }
}

// ─── Color Scheme ───────────────────────────────────────────────────────────

/// Semantic color assignments for TUI surfaces.
///
/// Product crates can implement custom schemes by providing values
/// for all fields. Use [`Theme::dark`] or [`Theme::light`] for presets.
pub trait ColorScheme {
    /// Primary background color.
    fn bg(&self) -> PackedRgba;
    /// Primary foreground (text) color.
    fn fg(&self) -> PackedRgba;
    /// Status bar background.
    fn status_bar_bg(&self) -> PackedRgba;
    /// Status bar text.
    fn status_bar_fg(&self) -> PackedRgba;
    /// Selected/focused item highlight.
    fn highlight_bg(&self) -> PackedRgba;
    /// Highlight text.
    fn highlight_fg(&self) -> PackedRgba;
    /// Border color for panels and widgets.
    fn border(&self) -> PackedRgba;
    /// Muted/secondary text.
    fn muted(&self) -> PackedRgba;
    /// Error/alert color.
    fn error(&self) -> PackedRgba;
    /// Warning color.
    fn warning(&self) -> PackedRgba;
    /// Success/ok color.
    fn success(&self) -> PackedRgba;
    /// Info/accent color.
    fn info(&self) -> PackedRgba;
    /// Accent color for active tabs and focused borders.
    fn accent(&self) -> PackedRgba {
        self.info()
    }
    /// Elevated surface background (cards, panels).
    fn surface(&self) -> PackedRgba {
        self.bg()
    }
    /// Dimmed/inactive surface background.
    fn surface_dim(&self) -> PackedRgba {
        self.bg()
    }
}

// ─── Theme ──────────────────────────────────────────────────────────────────

/// Concrete theme with all color assignments.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct Theme {
    pub preset: ThemePreset,
    pub bg: SerColor,
    pub fg: SerColor,
    pub status_bar_bg: SerColor,
    pub status_bar_fg: SerColor,
    pub highlight_bg: SerColor,
    pub highlight_fg: SerColor,
    pub border: SerColor,
    pub muted: SerColor,
    pub error: SerColor,
    pub warning: SerColor,
    pub success: SerColor,
    pub info: SerColor,
    pub accent: SerColor,
    pub surface: SerColor,
    pub surface_dim: SerColor,
}

/// Serializable wrapper around `PackedRgba`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct SerColor {
    pub r: u8,
    pub g: u8,
    pub b: u8,
}

impl SerColor {
    #[must_use]
    pub const fn new(r: u8, g: u8, b: u8) -> Self {
        Self { r, g, b }
    }

    /// Convert to `PackedRgba`.
    #[must_use]
    pub const fn to_color(self) -> PackedRgba {
        PackedRgba::rgb(self.r, self.g, self.b)
    }
}

impl Theme {
    /// Dark theme preset.
    #[must_use]
    pub const fn dark() -> Self {
        Self {
            preset: ThemePreset::Dark,
            bg: SerColor::new(0x1a, 0x1b, 0x26), // dark navy
            fg: SerColor::new(0xc0, 0xca, 0xf5), // soft white-blue
            status_bar_bg: SerColor::new(0x24, 0x28, 0x3b), // slightly lighter
            status_bar_fg: SerColor::new(0x7a, 0xa2, 0xf7), // bright blue
            highlight_bg: SerColor::new(0x33, 0x46, 0x7c), // muted blue
            highlight_fg: SerColor::new(0xff, 0xff, 0xff), // white
            border: SerColor::new(0x3b, 0x40, 0x61), // dark border
            muted: SerColor::new(0x56, 0x5f, 0x89), // gray
            error: SerColor::new(0xf7, 0x76, 0x8e), // red
            warning: SerColor::new(0xe0, 0xaf, 0x68), // orange
            success: SerColor::new(0x9e, 0xce, 0x6a), // green
            info: SerColor::new(0x7d, 0xcf, 0xff), // cyan
            accent: SerColor::new(0x7d, 0xcf, 0xff), // cyan (same as info)
            surface: SerColor::new(0x24, 0x28, 0x3b), // elevated surface
            surface_dim: SerColor::new(0x1a, 0x1b, 0x26), // dimmed (same as bg)
        }
    }

    /// Light theme preset.
    #[must_use]
    pub const fn light() -> Self {
        Self {
            preset: ThemePreset::Light,
            bg: SerColor::new(0xf5, 0xf5, 0xf5),
            fg: SerColor::new(0x34, 0x35, 0x4e),
            status_bar_bg: SerColor::new(0xe1, 0xe2, 0xe7),
            status_bar_fg: SerColor::new(0x34, 0x54, 0x8a),
            highlight_bg: SerColor::new(0xb6, 0xd4, 0xf0),
            highlight_fg: SerColor::new(0x00, 0x00, 0x00),
            border: SerColor::new(0xc8, 0xc8, 0xd0),
            muted: SerColor::new(0x8c, 0x8c, 0xa0),
            error: SerColor::new(0xc0, 0x3c, 0x3c),
            warning: SerColor::new(0x96, 0x5f, 0x00),
            success: SerColor::new(0x40, 0x7f, 0x00),
            info: SerColor::new(0x00, 0x6f, 0xaf),
            accent: SerColor::new(0x00, 0x6f, 0xaf), // blue (same as info)
            surface: SerColor::new(0xff, 0xff, 0xff), // white elevated
            surface_dim: SerColor::new(0xf5, 0xf5, 0xf5), // dimmed (same as bg)
        }
    }

    /// Cyberpunk Aurora theme preset.
    #[must_use]
    pub const fn cyberpunk_aurora() -> Self {
        Self {
            preset: ThemePreset::CyberpunkAurora,
            bg: SerColor::new(0x13, 0x05, 0x2a), // deep purple
            fg: SerColor::new(0x00, 0xf0, 0xd0), // neon cyan
            status_bar_bg: SerColor::new(0x1e, 0x0a, 0x3e), // slightly lighter purple
            status_bar_fg: SerColor::new(0xff, 0x6e, 0xb4), // hot pink
            highlight_bg: SerColor::new(0x3a, 0x15, 0x6a), // vivid purple
            highlight_fg: SerColor::new(0xff, 0xff, 0xff), // white
            border: SerColor::new(0x2d, 0x10, 0x52), // mid purple
            muted: SerColor::new(0x6a, 0x4c, 0x93), // lavender gray
            error: SerColor::new(0xff, 0x30, 0x60), // neon red
            warning: SerColor::new(0xff, 0xd7, 0x00), // gold
            success: SerColor::new(0x39, 0xff, 0x14), // neon green
            info: SerColor::new(0x00, 0xf0, 0xd0), // neon cyan
            accent: SerColor::new(0xff, 0x6e, 0xb4), // hot pink
            surface: SerColor::new(0x1e, 0x0a, 0x3e), // elevated purple
            surface_dim: SerColor::new(0x13, 0x05, 0x2a), // dimmed (same as bg)
        }
    }

    /// Darcula theme preset (IntelliJ-inspired).
    #[must_use]
    pub const fn darcula() -> Self {
        Self {
            preset: ThemePreset::Darcula,
            bg: SerColor::new(0x2b, 0x2b, 0x2b), // dark gray
            fg: SerColor::new(0xa9, 0xb7, 0xc6), // soft blue-gray
            status_bar_bg: SerColor::new(0x3c, 0x3f, 0x41), // lighter gray
            status_bar_fg: SerColor::new(0x68, 0x97, 0xbb), // soft blue
            highlight_bg: SerColor::new(0x21, 0x4a, 0x83), // selection blue
            highlight_fg: SerColor::new(0xff, 0xff, 0xff), // white
            border: SerColor::new(0x4b, 0x4b, 0x4b), // mid gray
            muted: SerColor::new(0x78, 0x78, 0x78), // dim gray
            error: SerColor::new(0xbc, 0x35, 0x51), // muted red
            warning: SerColor::new(0xbb, 0xb5, 0x29), // olive yellow
            success: SerColor::new(0x6a, 0x87, 0x59), // forest green
            info: SerColor::new(0x68, 0x97, 0xbb), // soft blue
            accent: SerColor::new(0x68, 0x97, 0xbb), // soft blue
            surface: SerColor::new(0x31, 0x31, 0x35), // elevated dark
            surface_dim: SerColor::new(0x2b, 0x2b, 0x2b), // dimmed (same as bg)
        }
    }

    /// Nordic Frost theme preset (Nord-inspired).
    #[must_use]
    pub const fn nordic_frost() -> Self {
        Self {
            preset: ThemePreset::NordicFrost,
            bg: SerColor::new(0x2e, 0x34, 0x40), // nord0 polar night
            fg: SerColor::new(0xec, 0xef, 0xf4), // nord6 snow storm
            status_bar_bg: SerColor::new(0x3b, 0x42, 0x52), // nord1
            status_bar_fg: SerColor::new(0x88, 0xc0, 0xd0), // nord8 frost
            highlight_bg: SerColor::new(0x43, 0x4c, 0x5e), // nord2
            highlight_fg: SerColor::new(0xec, 0xef, 0xf4), // nord6
            border: SerColor::new(0x4c, 0x56, 0x6a), // nord3
            muted: SerColor::new(0x61, 0x6e, 0x88), // dimmed nord3
            error: SerColor::new(0xbf, 0x61, 0x6a), // nord11 red
            warning: SerColor::new(0xeb, 0xcb, 0x8b), // nord13 yellow
            success: SerColor::new(0xa3, 0xbe, 0x8c), // nord14 green
            info: SerColor::new(0x88, 0xc0, 0xd0), // nord8 frost
            accent: SerColor::new(0x5e, 0x81, 0xac), // nord10 cool blue
            surface: SerColor::new(0x3b, 0x42, 0x52), // nord1 elevated
            surface_dim: SerColor::new(0x2e, 0x34, 0x40), // dimmed (same as bg)
        }
    }

    /// Lumen Light theme preset.
    #[must_use]
    pub const fn lumen_light() -> Self {
        Self {
            preset: ThemePreset::LumenLight,
            bg: SerColor::new(0xfd, 0xf6, 0xe3), // warm cream
            fg: SerColor::new(0x3b, 0x38, 0x30), // dark brown
            status_bar_bg: SerColor::new(0xee, 0xe8, 0xd5), // muted cream
            status_bar_fg: SerColor::new(0xcb, 0x76, 0x16), // amber
            highlight_bg: SerColor::new(0xf5, 0xdc, 0xa0), // soft amber highlight
            highlight_fg: SerColor::new(0x2a, 0x27, 0x20), // dark
            border: SerColor::new(0xd6, 0xd0, 0xc0), // warm gray
            muted: SerColor::new(0x93, 0xa1, 0xa1), // cool gray
            error: SerColor::new(0xdc, 0x32, 0x2f), // red
            warning: SerColor::new(0xcb, 0x76, 0x16), // amber
            success: SerColor::new(0x85, 0x99, 0x00), // olive green
            info: SerColor::new(0x26, 0x8b, 0xd2), // blue
            accent: SerColor::new(0xcb, 0x76, 0x16), // amber
            surface: SerColor::new(0xff, 0xff, 0xf0), // warm white
            surface_dim: SerColor::new(0xfd, 0xf6, 0xe3), // dimmed (same as bg)
        }
    }

    /// Create a theme from a preset.
    #[must_use]
    pub const fn from_preset(preset: ThemePreset) -> Self {
        match preset {
            ThemePreset::Dark => Self::dark(),
            ThemePreset::Light => Self::light(),
            ThemePreset::CyberpunkAurora => Self::cyberpunk_aurora(),
            ThemePreset::Darcula => Self::darcula(),
            ThemePreset::NordicFrost => Self::nordic_frost(),
            ThemePreset::LumenLight => Self::lumen_light(),
        }
    }
}

impl ColorScheme for Theme {
    fn bg(&self) -> PackedRgba {
        self.bg.to_color()
    }
    fn fg(&self) -> PackedRgba {
        self.fg.to_color()
    }
    fn status_bar_bg(&self) -> PackedRgba {
        self.status_bar_bg.to_color()
    }
    fn status_bar_fg(&self) -> PackedRgba {
        self.status_bar_fg.to_color()
    }
    fn highlight_bg(&self) -> PackedRgba {
        self.highlight_bg.to_color()
    }
    fn highlight_fg(&self) -> PackedRgba {
        self.highlight_fg.to_color()
    }
    fn border(&self) -> PackedRgba {
        self.border.to_color()
    }
    fn muted(&self) -> PackedRgba {
        self.muted.to_color()
    }
    fn error(&self) -> PackedRgba {
        self.error.to_color()
    }
    fn warning(&self) -> PackedRgba {
        self.warning.to_color()
    }
    fn success(&self) -> PackedRgba {
        self.success.to_color()
    }
    fn info(&self) -> PackedRgba {
        self.info.to_color()
    }
    fn accent(&self) -> PackedRgba {
        self.accent.to_color()
    }
    fn surface(&self) -> PackedRgba {
        self.surface.to_color()
    }
    fn surface_dim(&self) -> PackedRgba {
        self.surface_dim.to_color()
    }
}

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

    #[test]
    fn dark_theme_colors() {
        let theme = Theme::dark();
        assert_eq!(theme.preset, ThemePreset::Dark);
        assert_ne!(theme.bg, theme.fg);
    }

    #[test]
    fn light_theme_colors() {
        let theme = Theme::light();
        assert_eq!(theme.preset, ThemePreset::Light);
    }

    #[test]
    fn theme_serde_roundtrip() {
        let theme = Theme::dark();
        let json = serde_json::to_string(&theme).unwrap();
        let decoded: Theme = serde_json::from_str(&json).unwrap();
        assert_eq!(decoded, theme);
    }

    #[test]
    fn preset_serde_roundtrip() {
        for preset in ThemePreset::ALL {
            let json = serde_json::to_string(&preset).unwrap();
            let decoded: ThemePreset = serde_json::from_str(&json).unwrap();
            assert_eq!(decoded, preset);
        }
    }

    #[test]
    fn from_preset_matches_direct() {
        assert_eq!(Theme::from_preset(ThemePreset::Dark), Theme::dark());
        assert_eq!(Theme::from_preset(ThemePreset::Light), Theme::light());
        assert_eq!(
            Theme::from_preset(ThemePreset::CyberpunkAurora),
            Theme::cyberpunk_aurora()
        );
        assert_eq!(Theme::from_preset(ThemePreset::Darcula), Theme::darcula());
        assert_eq!(
            Theme::from_preset(ThemePreset::NordicFrost),
            Theme::nordic_frost()
        );
        assert_eq!(
            Theme::from_preset(ThemePreset::LumenLight),
            Theme::lumen_light()
        );
    }

    #[test]
    fn ser_color_to_color() {
        let c = SerColor::new(0xff, 0x00, 0x80);
        assert_eq!(c.to_color(), PackedRgba::rgb(0xff, 0x00, 0x80));
    }

    #[test]
    fn color_scheme_trait() {
        let theme = Theme::dark();
        // Verify trait methods work
        assert_ne!(theme.bg(), theme.error());
        assert_ne!(theme.success(), theme.warning());
    }

    #[test]
    fn preset_cycling_wraps() {
        let mut preset = ThemePreset::Dark;
        for _ in 0..ThemePreset::ALL.len() {
            preset = preset.next();
        }
        assert_eq!(
            preset,
            ThemePreset::Dark,
            "cycling should wrap back to start"
        );
    }

    #[test]
    fn preset_cycling_visits_all() {
        let mut visited = std::collections::HashSet::new();
        let mut preset = ThemePreset::Dark;
        for _ in 0..ThemePreset::ALL.len() {
            visited.insert(preset);
            preset = preset.next();
        }
        assert_eq!(visited.len(), ThemePreset::ALL.len());
    }

    #[test]
    fn accent_colors_distinct_per_theme() {
        let accents: Vec<SerColor> = ThemePreset::ALL
            .iter()
            .map(|p| Theme::from_preset(*p).accent)
            .collect();
        // At least 4 distinct accents out of 6 themes
        let unique: std::collections::HashSet<_> =
            accents.iter().map(|c| (c.r, c.g, c.b)).collect();
        assert!(
            unique.len() >= 4,
            "expected at least 4 distinct accents, got {}",
            unique.len()
        );
    }

    #[test]
    fn all_presets_have_all_const() {
        assert_eq!(ThemePreset::ALL.len(), 6);
    }
}