ratcn 0.0.1

Themeable terminal UI components and interaction runtime for Ratatui
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
use ratatui::style::Color;

use crate::color::dim;
use ratatui::symbols::border;

/// A border line style for component-local styling.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum BorderStyle {
    /// Plain single lines: `┌─┐`.
    Single,
    /// Single lines with rounded corners: `╭─╮`.
    Rounded,
    /// Double lines: `╔═╗`.
    Double,
    /// Thick lines: `┏━┓`.
    Heavy,
}

impl BorderStyle {
    /// The matching ratatui border set, for drawing with a `Block`.
    #[must_use]
    pub const fn to_border_set(self) -> border::Set<'static> {
        match self {
            Self::Single => border::PLAIN,
            Self::Rounded => border::ROUNDED,
            Self::Double => border::DOUBLE,
            Self::Heavy => border::THICK,
        }
    }
}

/// Backing storage for [`Theme::presets`].
const PRESETS: [Theme; 7] = [
    Theme::default_dark(),
    Theme::terminal(),
    Theme::catppuccin(),
    Theme::gruvbox(),
    Theme::nord(),
    Theme::tokyo_night(),
    Theme::solarized(),
];

/// Preset focus rings are derived, not stored: the preset's `primary` dimmed
/// this much toward its `background`.
const RING_DIM: u16 = 20;

/// A theme is a small color palette. Every field names a *purpose*, not a
/// component: the same `primary` paints a default button, a selected list row,
/// and a bar; the same `field` paints any inset "well" (lists, panels, bars).
///
/// Focus and disabled variants are derived by the components that need them,
/// not stored here (see [`crate::color`]): a focused fill shifts a fixed amount
/// in the direction that makes it stand out — a bright fill darkens, a dark fill
/// (secondary, the inset `field`) lightens — and a disabled fill dims toward the
/// surface.
///
/// `accent` is the palette's vivid signature color (Catppuccin mauve, Gruvbox
/// orange, Solarized magenta, …), kept for the occasional creative splash — a
/// notification, a highlighted border — distinct from `primary`, the workhorse
/// accent components reach for by default.
///
/// # Authoring a palette
///
/// The type is `#[non_exhaustive]`, so a role can be added without breaking
/// every app that names a palette. That rules out a struct literal; start from
/// a preset and assign the fields you want instead. The fields stay public and
/// the presets are `const`, so this still works in a `const` context:
///
/// ```
/// use ratatui::style::Color;
/// use ratcn::Theme;
///
/// const fn operations() -> Theme {
///     let mut theme = Theme::default_dark();
///     theme.name = "Operations";
///     theme.accent = Color::LightCyan;
///     theme
/// }
///
/// const THEME: Theme = operations();
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct Theme {
    /// Display name, for a theme picker.
    pub name: &'static str,

    // Text
    /// Ordinary text.
    pub foreground: Color,
    /// De-emphasized text: placeholders, descriptions, disabled labels.
    pub muted_foreground: Color,

    // Surfaces, back to front
    /// The furthest-back surface, behind everything else.
    pub background: Color,
    /// Raised surfaces that sit above the background — dialogs and toasts.
    pub surface: Color,
    /// Control surfaces — lists, select panels, and chart backdrops.
    pub field: Color,

    // Accents
    /// The workhorse accent: default buttons, selected rows, bars.
    pub primary: Color,
    /// Text drawn on top of `primary`, so it must contrast with it.
    pub primary_foreground: Color,
    /// The quieter accent, for secondary buttons and unselected tabs.
    pub secondary: Color,
    /// Text drawn on top of `secondary`.
    pub secondary_foreground: Color,
    /// The palette's vivid signature color, held back for occasional emphasis
    /// rather than used as a second `primary`.
    pub accent: Color,
    /// Destructive actions and error states.
    pub destructive: Color,
    /// Text drawn on top of `destructive`.
    pub destructive_foreground: Color,
    /// Warnings — less severe than `destructive`.
    pub warning: Color,

    // Lines and caret
    /// Borders and separators at rest.
    pub border: Color,
    /// The focus accent around a container — a focused pane's border, the
    /// modal dialog's frame. The shadcn `ring` role. Presets derive it from
    /// `primary`, dimmed 20% toward `background`.
    pub ring: Color,
    /// A text caret. No component ships with one today; the role is kept so
    /// text-entry components can be themed consistently when they land.
    pub cursor: Color,
}

impl Default for Theme {
    fn default() -> Self {
        Self::default_dark()
    }
}

impl Theme {
    /// A dark default theme inspired by shadcn/ui's default OKLCH palette,
    /// translated to opaque sRGB terminal colors.
    #[must_use]
    pub const fn default_dark() -> Self {
        let primary = Color::Rgb(229, 229, 229);
        let background = Color::Rgb(10, 10, 10);
        Self {
            name: "Default",
            foreground: Color::Rgb(250, 250, 250),
            muted_foreground: Color::Rgb(161, 161, 161),
            background,
            surface: Color::Rgb(18, 18, 18),
            field: Color::Rgb(31, 31, 31),
            primary,
            primary_foreground: Color::Rgb(23, 23, 23),
            secondary: Color::Rgb(38, 38, 38),
            secondary_foreground: Color::Rgb(250, 250, 250),
            accent: Color::Rgb(139, 92, 246),
            destructive: Color::Rgb(255, 100, 103),
            destructive_foreground: Color::Rgb(250, 250, 250),
            warning: Color::Rgb(250, 204, 21),
            border: Color::Rgb(94, 94, 94),
            ring: dim(primary, background, RING_DIM),
            cursor: Color::Rgb(115, 115, 115),
        }
    }

    /// Terminal-friendly colors: inherit the outer terminal background, but use
    /// concrete neutral surfaces for component wells. [`Color::Reset`] carries
    /// no RGB channels, so derived states like focused list backgrounds
    /// need real surface colors to lighten from. Named colors (`LightBlue`,
    /// `Yellow`, …) keep the terminal palette at rest; their derived
    /// focus/hover/disabled states resolve through
    /// [`resolve_rgb`](crate::color::resolve_rgb)'s fixed VGA approximation.
    #[must_use]
    pub const fn terminal() -> Self {
        let primary = Color::LightBlue;
        let background = Color::Reset;
        Self {
            name: "Terminal",
            foreground: Color::Reset,
            muted_foreground: Color::Gray,
            background,
            surface: Color::Rgb(18, 18, 18),
            field: Color::Rgb(31, 31, 31),
            primary,
            primary_foreground: Color::Black,
            secondary: Color::Rgb(38, 38, 38),
            secondary_foreground: Color::Reset,
            accent: Color::LightMagenta,
            destructive: Color::LightRed,
            destructive_foreground: Color::Black,
            warning: Color::Yellow,
            border: Color::DarkGray,
            ring: dim(primary, background, RING_DIM),
            cursor: Color::LightBlue,
        }
    }

    /// The Catppuccin Mocha palette: soft pastels on a deep indigo background.
    #[must_use]
    pub const fn catppuccin() -> Self {
        let primary = Color::Rgb(137, 180, 250);
        let background = Color::Rgb(17, 17, 27);
        Self {
            name: "Catppuccin",
            foreground: Color::Rgb(205, 214, 244),
            muted_foreground: Color::Rgb(166, 173, 200),
            background,
            surface: Color::Rgb(30, 30, 46),
            field: Color::Rgb(43, 43, 61),
            primary,
            primary_foreground: Color::Rgb(17, 17, 27),
            secondary: Color::Rgb(49, 50, 68),
            secondary_foreground: Color::Rgb(205, 214, 244),
            accent: Color::Rgb(203, 166, 247),
            destructive: Color::Rgb(243, 139, 168),
            destructive_foreground: Color::Rgb(17, 17, 27),
            warning: Color::Rgb(250, 179, 135),
            border: Color::Rgb(94, 97, 122),
            ring: dim(primary, background, RING_DIM),
            cursor: Color::Rgb(137, 180, 250),
        }
    }

    /// The Gruvbox dark palette: warm, low-contrast retro tones.
    #[must_use]
    pub const fn gruvbox() -> Self {
        let primary = Color::Rgb(250, 189, 47);
        let background = Color::Rgb(40, 40, 40);
        Self {
            name: "Gruvbox",
            foreground: Color::Rgb(235, 219, 178),
            muted_foreground: Color::Rgb(168, 153, 132),
            background,
            surface: Color::Rgb(60, 56, 54),
            field: Color::Rgb(73, 67, 64),
            primary,
            primary_foreground: Color::Rgb(40, 40, 40),
            secondary: Color::Rgb(80, 73, 69),
            secondary_foreground: Color::Rgb(235, 219, 178),
            accent: Color::Rgb(254, 128, 25),
            destructive: Color::Rgb(251, 73, 52),
            destructive_foreground: Color::Rgb(40, 40, 40),
            warning: Color::Rgb(250, 189, 47),
            border: Color::Rgb(124, 112, 102),
            ring: dim(primary, background, RING_DIM),
            cursor: Color::Rgb(250, 189, 47),
        }
    }

    /// The Nord palette: cool desaturated blues.
    #[must_use]
    pub const fn nord() -> Self {
        let primary = Color::Rgb(136, 192, 208);
        let background = Color::Rgb(46, 52, 64);
        Self {
            name: "Nord",
            foreground: Color::Rgb(216, 222, 233),
            muted_foreground: Color::Rgb(129, 161, 193),
            background,
            surface: Color::Rgb(59, 66, 82),
            field: Color::Rgb(64, 73, 90),
            primary,
            primary_foreground: Color::Rgb(46, 52, 64),
            secondary: Color::Rgb(67, 76, 94),
            secondary_foreground: Color::Rgb(216, 222, 233),
            accent: Color::Rgb(180, 142, 173),
            destructive: Color::Rgb(191, 97, 106),
            destructive_foreground: Color::Rgb(46, 52, 64),
            warning: Color::Rgb(235, 203, 139),
            border: Color::Rgb(112, 126, 155),
            ring: dim(primary, background, RING_DIM),
            cursor: Color::Rgb(136, 192, 208),
        }
    }

    /// The Tokyo Night palette: saturated blues and purples on near-black.
    #[must_use]
    pub const fn tokyo_night() -> Self {
        let primary = Color::Rgb(122, 162, 247);
        let background = Color::Rgb(26, 27, 38);
        Self {
            name: "Tokyo Night",
            foreground: Color::Rgb(192, 202, 245),
            muted_foreground: Color::Rgb(120, 134, 180),
            background,
            surface: Color::Rgb(36, 40, 59),
            field: Color::Rgb(55, 61, 89),
            primary,
            primary_foreground: Color::Rgb(26, 27, 38),
            secondary: Color::Rgb(65, 72, 104),
            secondary_foreground: Color::Rgb(192, 202, 245),
            accent: Color::Rgb(187, 154, 247),
            destructive: Color::Rgb(247, 118, 142),
            destructive_foreground: Color::Rgb(26, 27, 38),
            warning: Color::Rgb(224, 175, 104),
            border: Color::Rgb(92, 102, 146),
            ring: dim(primary, background, RING_DIM),
            cursor: Color::Rgb(122, 162, 247),
        }
    }

    /// The Solarized Dark palette: low-contrast, carefully balanced.
    #[must_use]
    pub const fn solarized() -> Self {
        let primary = Color::Rgb(39, 139, 211);
        let background = Color::Rgb(0, 20, 26);
        Self {
            name: "Solarized",
            foreground: Color::Rgb(159, 171, 173),
            muted_foreground: Color::Rgb(88, 110, 117),
            background,
            surface: Color::Rgb(0, 45, 56),
            field: Color::Rgb(0, 58, 63),
            primary,
            primary_foreground: Color::Rgb(253, 246, 227),
            secondary: Color::Rgb(7, 54, 66),
            secondary_foreground: Color::Rgb(173, 184, 184),
            accent: Color::Rgb(211, 54, 130),
            destructive: Color::Rgb(220, 49, 46),
            destructive_foreground: Color::Rgb(253, 246, 227),
            warning: Color::Rgb(181, 137, 0),
            border: Color::Rgb(94, 117, 125),
            ring: dim(primary, background, RING_DIM),
            cursor: Color::Rgb(42, 162, 152),
        }
    }

    /// Every built-in theme, in a stable order — for a theme picker or a
    /// cycle-themes hotkey.
    ///
    /// The slice is returned rather than a fixed-size array so that adding a
    /// preset does not break callers: index it, iterate it, or take its
    /// `len()`, but do not depend on the count.
    #[must_use]
    pub const fn presets() -> &'static [Self] {
        &PRESETS
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::color::{FIELD_FOCUS_LIGHTEN, lighten};

    /// Relative luminance, per WCAG 2.1.
    fn luminance(color: Color) -> f64 {
        let Color::Rgb(r, g, b) = color else {
            panic!("expected an rgb color, got {color:?}");
        };
        let channel = |v: u8| {
            let c = f64::from(v) / 255.0;
            if c <= 0.039_28 {
                c / 12.92
            } else {
                ((c + 0.055) / 1.055).powf(2.4)
            }
        };
        0.2126 * channel(r) + 0.7152 * channel(g) + 0.0722 * channel(b)
    }

    fn contrast(a: Color, b: Color) -> f64 {
        let (a, b) = (luminance(a), luminance(b));
        (a.max(b) + 0.05) / (a.min(b) + 0.05)
    }

    // A border is a UI boundary, so WCAG 2.1 non-text contrast (1.4.11) asks for
    // 3:1 against what it sits on. Every rgb preset was below that once — the
    // field borders were nearly invisible on a dark background — so this pins
    // the floor for future palette edits.
    #[test]
    fn preset_borders_meet_non_text_contrast_against_their_background() {
        for theme in Theme::presets() {
            // The terminal preset inherits the user's own ANSI palette, so its
            // contrast is not ours to measure.
            if theme.name == "Terminal" {
                continue;
            }
            let ratio = contrast(theme.border, theme.background);
            assert!(
                ratio >= 3.0,
                "{} border contrast is {ratio:.2}:1, below the 3:1 floor",
                theme.name
            );
        }
    }

    #[test]
    fn preset_fields_are_distinct_on_backgrounds_and_surfaces() {
        for theme in Theme::presets() {
            // The terminal background inherits an unknown user palette.
            if theme.name == "Terminal" {
                continue;
            }
            for (role, backdrop) in [("background", theme.background), ("surface", theme.surface)] {
                let ratio = contrast(theme.field, backdrop);
                assert!(
                    ratio >= 1.1,
                    "{} field contrast on {role} is {ratio:.2}:1, below the 1.1:1 floor",
                    theme.name
                );
            }
        }
    }

    #[test]
    fn terminal_theme_has_derived_component_surfaces() {
        let theme = Theme::terminal();

        assert_eq!(theme.background, Color::Reset);
        assert!(matches!(theme.surface, Color::Rgb(_, _, _)));
        assert!(matches!(theme.field, Color::Rgb(_, _, _)));
        assert_ne!(lighten(theme.field, FIELD_FOCUS_LIGHTEN), theme.field);
    }
}