ratatui-bubbletea-theme 0.2.0

Charm/Bubble Tea-inspired theme helpers 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
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
//! Charm/Bubble Tea-inspired theme helpers for ratatui.
//!
//! This crate is intentionally usable from a normal ratatui event loop. It
//! does not own rendering, terminal setup, or event handling.

use std::borrow::Cow;

use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span, Text};
use ratatui::widgets::{Block, BorderType, Borders, Paragraph};

/// Default theme type for Charm/Bubble Tea-inspired ratatui apps.
pub type BubbleTheme = Theme;

/// Small set of symbols used by Charm-like terminal UIs.
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub struct Symbols {
    /// Prefix for ordinary list items.
    pub bullet: &'static str,
    /// Prefix for selected list items.
    pub selected: &'static str,
    /// Success marker.
    pub check: &'static str,
    /// Error/failure marker.
    pub cross: &'static str,
    /// Separator between compact help bindings.
    pub help_separator: &'static str,
}

impl Default for Symbols {
    fn default() -> Self {
        Self {
            bullet: "",
            selected: "",
            check: "",
            cross: "",
            help_separator: "",
        }
    }
}

/// Color palette behind [`Theme`]'s semantic styles.
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub struct Palette {
    /// Main foreground color.
    pub foreground: Color,
    /// Muted text color for hints and secondary copy.
    pub muted: Color,
    /// Primary accent color.
    pub accent: Color,
    /// Success color.
    pub success: Color,
    /// Warning color.
    pub warning: Color,
    /// Error color.
    pub error: Color,
    /// Normal border color.
    pub border: Color,
    /// Focused border color.
    pub focused_border: Color,
    /// Selected item background color.
    pub selected_background: Color,
}

impl Palette {
    /// Default Charm-inspired palette.
    ///
    /// This is exposed as a constant so applications can reuse color tokens in
    /// their own `const` style tables, match arms, and module-level defaults
    /// without duplicating RGB literals.
    pub const CHARM: Self = Self {
        foreground: Color::Rgb(230, 230, 230),
        muted: Color::Rgb(122, 122, 122),
        accent: Color::Rgb(255, 117, 191),
        success: Color::Rgb(4, 211, 97),
        warning: Color::Rgb(255, 193, 7),
        error: Color::Rgb(255, 83, 112),
        border: Color::Rgb(92, 92, 92),
        focused_border: Color::Rgb(123, 201, 255),
        selected_background: Color::Rgb(48, 48, 48),
    };
}

impl Default for Palette {
    fn default() -> Self {
        Self::CHARM
    }
}

/// Semantic style tokens and widget helpers for a Charm-like ratatui UI.
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub struct Theme {
    /// Source palette used to build semantic styles.
    pub palette: Palette,
    /// Shared symbols used by helper methods.
    pub symbols: Symbols,
    /// Main text style.
    pub text: Style,
    /// Muted secondary text style.
    pub muted: Style,
    /// Primary accent style.
    pub accent: Style,
    /// Success style.
    pub success: Style,
    /// Warning style.
    pub warning: Style,
    /// Error style.
    pub error: Style,
    /// Normal border style.
    pub border: Style,
    /// Focused border style.
    pub focused_border: Style,
    /// Block title style.
    pub title: Style,
    /// Selected row/item style.
    pub selected: Style,
    /// Help key style.
    pub help_key: Style,
    /// Help description style.
    pub help_desc: Style,
}

impl Theme {
    /// Builds a theme from a palette and symbol set.
    #[must_use]
    pub fn new(palette: Palette, symbols: Symbols) -> Self {
        Self {
            palette,
            symbols,
            text: Style::new().fg(palette.foreground),
            muted: Style::new().fg(palette.muted),
            accent: Style::new().fg(palette.accent),
            success: Style::new().fg(palette.success),
            warning: Style::new().fg(palette.warning),
            error: Style::new().fg(palette.error),
            border: Style::new().fg(palette.border),
            focused_border: Style::new().fg(palette.focused_border),
            title: Style::new().fg(palette.accent).add_modifier(Modifier::BOLD),
            selected: Style::new()
                .fg(palette.foreground)
                .bg(palette.selected_background)
                .add_modifier(Modifier::BOLD),
            help_key: Style::new().fg(palette.accent).add_modifier(Modifier::BOLD),
            help_desc: Style::new().fg(palette.muted),
        }
    }

    /// Creates a rounded bordered block using the theme's default border and title styles.
    #[must_use]
    pub fn block<'a>(&self) -> Block<'a> {
        self.block_with_focus(false)
    }

    /// Creates a rounded bordered block with focus-aware border styling.
    #[must_use]
    pub fn block_with_focus<'a>(&self, focused: bool) -> Block<'a> {
        Block::new()
            .borders(Borders::ALL)
            .border_type(BorderType::Rounded)
            .border_style(if focused {
                self.focused_border
            } else {
                self.border
            })
            .title_style(self.title)
    }

    /// Creates a rounded, titled block using the theme's default title style.
    #[must_use]
    pub fn titled_block<'a, T>(&self, title: T) -> Block<'a>
    where
        T: Into<Line<'a>>,
    {
        self.block().title(title)
    }

    /// Creates a modal-style block with the focused/accent border style.
    ///
    /// This is intended for the common `Clear` + centered-popup pattern.
    #[must_use]
    pub fn modal_block<'a>(&self) -> Block<'a> {
        self.block_with_focus(true)
    }

    /// Creates a titled modal-style block with the focused/accent border style.
    #[must_use]
    pub fn titled_modal_block<'a, T>(&self, title: T) -> Block<'a>
    where
        T: Into<Line<'a>>,
    {
        self.modal_block().title(title)
    }

    /// Creates a paragraph using the theme's normal text style.
    #[must_use]
    pub fn paragraph<'a, T>(&self, text: T) -> Paragraph<'a>
    where
        T: Into<Text<'a>>,
    {
        Paragraph::new(text).style(self.text)
    }

    /// Creates a paragraph with a themed block attached.
    #[must_use]
    pub fn paragraph_in_block<'a, T, L>(&self, text: T, title: L) -> Paragraph<'a>
    where
        T: Into<Text<'a>>,
        L: Into<Line<'a>>,
    {
        Paragraph::new(text)
            .style(self.text)
            .block(self.block().title(title))
    }

    /// Creates a normal text span.
    #[must_use]
    pub fn span<'a, T>(&self, content: T) -> Span<'a>
    where
        T: Into<Cow<'a, str>>,
    {
        Span::styled(content, self.text)
    }

    /// Creates a muted text span.
    #[must_use]
    pub fn muted<'a, T>(&self, content: T) -> Span<'a>
    where
        T: Into<Cow<'a, str>>,
    {
        Span::styled(content, self.muted)
    }

    /// Creates an accent text span.
    #[must_use]
    pub fn accent<'a, T>(&self, content: T) -> Span<'a>
    where
        T: Into<Cow<'a, str>>,
    {
        Span::styled(content, self.accent)
    }

    /// Creates a success text span.
    #[must_use]
    pub fn success<'a, T>(&self, content: T) -> Span<'a>
    where
        T: Into<Cow<'a, str>>,
    {
        Span::styled(content, self.success)
    }

    /// Creates a warning text span.
    #[must_use]
    pub fn warning<'a, T>(&self, content: T) -> Span<'a>
    where
        T: Into<Cow<'a, str>>,
    {
        Span::styled(content, self.warning)
    }

    /// Creates an error text span.
    #[must_use]
    pub fn error<'a, T>(&self, content: T) -> Span<'a>
    where
        T: Into<Cow<'a, str>>,
    {
        Span::styled(content, self.error)
    }

    /// Creates a title line.
    #[must_use]
    pub fn title<'a, T>(&self, content: T) -> Line<'a>
    where
        T: Into<Cow<'a, str>>,
    {
        Line::styled(content, self.title)
    }

    /// Creates a line prefixed with the default bullet symbol.
    #[must_use]
    pub fn bullet<'a, T>(&self, content: T) -> Line<'a>
    where
        T: Into<Cow<'a, str>>,
    {
        Line::from(vec![
            self.muted(self.symbols.bullet),
            self.span(" "),
            self.span(content),
        ])
    }

    /// Creates a success line prefixed with the default check symbol.
    #[must_use]
    pub fn checked<'a, T>(&self, content: T) -> Line<'a>
    where
        T: Into<Cow<'a, str>>,
    {
        Line::from(vec![
            self.success(self.symbols.check),
            self.span(" "),
            self.span(content),
        ])
    }

    /// Creates an error line prefixed with the default cross symbol.
    #[must_use]
    pub fn crossed<'a, T>(&self, content: T) -> Line<'a>
    where
        T: Into<Cow<'a, str>>,
    {
        Line::from(vec![
            self.error(self.symbols.cross),
            self.span(" "),
            self.span(content),
        ])
    }

    /// Creates a compact help line from `(key, description)` pairs.
    #[must_use]
    pub fn help_line<'a, I, K, D>(&self, bindings: I) -> Line<'a>
    where
        I: IntoIterator<Item = (K, D)>,
        K: Into<Cow<'a, str>>,
        D: Into<Cow<'a, str>>,
    {
        let mut spans = Vec::new();

        for (index, (key, description)) in bindings.into_iter().enumerate() {
            if index > 0 {
                spans.push(self.muted(self.symbols.help_separator));
            }

            spans.push(Span::styled(key, self.help_key));
            spans.push(self.muted(" "));
            spans.push(Span::styled(description, self.help_desc));
        }

        Line::from(spans)
    }
}

impl Default for Theme {
    fn default() -> Self {
        Self::new(Palette::default(), Symbols::default())
    }
}

#[cfg(test)]
mod tests {
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;
    use ratatui::layout::Rect;
    use ratatui::style::{Color, Modifier, Style};

    use super::{BubbleTheme, Palette, Symbols, Theme};

    #[test]
    fn default_theme_maps_palette_to_semantic_styles() {
        let theme = Theme::default();
        let palette = Palette::default();

        assert_eq!(theme.text, Style::new().fg(palette.foreground));
        assert_eq!(theme.muted, Style::new().fg(palette.muted));
        assert_eq!(theme.accent, Style::new().fg(palette.accent));
        assert_eq!(theme.border, Style::new().fg(palette.border));
        assert_eq!(
            theme.focused_border,
            Style::new().fg(palette.focused_border)
        );
        assert_eq!(theme.title.add_modifier, Modifier::BOLD);
        assert_eq!(theme.selected.bg, Some(palette.selected_background));
    }

    #[test]
    fn default_palette_is_charm_const() {
        const ACCENT: Color = Palette::CHARM.accent;

        assert_eq!(Palette::default(), Palette::CHARM);
        assert_eq!(ACCENT, Color::Rgb(255, 117, 191));
    }

    #[test]
    fn custom_palette_is_reflected_in_styles() {
        let palette = Palette {
            foreground: Color::White,
            muted: Color::DarkGray,
            accent: Color::Cyan,
            success: Color::Green,
            warning: Color::Yellow,
            error: Color::Red,
            border: Color::Gray,
            focused_border: Color::Blue,
            selected_background: Color::Black,
        };
        let theme = Theme::new(palette, Symbols::default());

        assert_eq!(theme.accent.fg, Some(Color::Cyan));
        assert_eq!(theme.focused_border.fg, Some(Color::Blue));
        assert_eq!(theme.selected.bg, Some(Color::Black));
    }

    #[test]
    fn helper_lines_include_expected_symbols_and_text() {
        let theme = BubbleTheme::default();

        assert_eq!(theme.bullet("Item").to_string(), "• Item");
        assert_eq!(theme.checked("Done").to_string(), "✓ Done");
        assert_eq!(theme.crossed("Failed").to_string(), "✗ Failed");
    }

    #[test]
    fn help_line_renders_compact_bindings() {
        let theme = Theme::default();
        let line = theme.help_line([("q", "quit"), ("?", "help")]);

        assert_eq!(line.to_string(), "q quit • ? help");
        assert_eq!(line.spans[0].style, theme.help_key);
        assert_eq!(line.spans[2].style, theme.help_desc);
    }

    #[test]
    fn themed_block_renders_rounded_border_and_title() -> Result<(), Box<dyn std::error::Error>> {
        let theme = Theme::default();
        let backend = TestBackend::new(12, 3);
        let mut terminal = Terminal::new(backend)?;

        terminal.draw(|frame| {
            frame.render_widget(theme.block().title("Demo"), frame.area());
        })?;

        let buffer = terminal.backend().buffer();
        assert_eq!(buffer[(0, 0)].symbol(), "");
        assert_eq!(buffer[(1, 0)].symbol(), "D");
        assert_eq!(buffer[(4, 0)].symbol(), "o");
        assert_eq!(buffer[(11, 0)].symbol(), "");
        assert_eq!(buffer[(0, 2)].symbol(), "");
        assert_eq!(buffer[(11, 2)].symbol(), "");

        assert_eq!(buffer[(0, 0)].fg, theme.palette.border);
        assert_eq!(buffer[(1, 0)].fg, theme.palette.accent);
        assert!(buffer[(1, 0)].modifier.contains(Modifier::BOLD));

        Ok(())
    }

    #[test]
    fn focused_block_uses_focused_border_style() -> Result<(), Box<dyn std::error::Error>> {
        let theme = Theme::default();
        let backend = TestBackend::new(4, 3);
        let mut terminal = Terminal::new(backend)?;

        terminal.draw(|frame| {
            frame.render_widget(theme.block_with_focus(true), frame.area());
        })?;

        assert_eq!(
            terminal.backend().buffer()[(0, 0)].fg,
            theme.palette.focused_border
        );

        Ok(())
    }

    #[test]
    fn titled_block_applies_title_style() -> Result<(), Box<dyn std::error::Error>> {
        let theme = Theme::default();
        let backend = TestBackend::new(12, 3);
        let mut terminal = Terminal::new(backend)?;

        terminal.draw(|frame| {
            frame.render_widget(theme.titled_block("Demo"), frame.area());
        })?;

        let buffer = terminal.backend().buffer();
        assert_eq!(buffer[(1, 0)].symbol(), "D");
        assert_eq!(buffer[(1, 0)].fg, theme.palette.accent);
        assert!(buffer[(1, 0)].modifier.contains(Modifier::BOLD));

        Ok(())
    }

    #[test]
    fn modal_block_uses_focused_border_style() -> Result<(), Box<dyn std::error::Error>> {
        let theme = Theme::default();
        let backend = TestBackend::new(12, 3);
        let mut terminal = Terminal::new(backend)?;

        terminal.draw(|frame| {
            frame.render_widget(theme.titled_modal_block("Edit"), frame.area());
        })?;

        let buffer = terminal.backend().buffer();
        assert_eq!(buffer[(0, 0)].fg, theme.palette.focused_border);
        assert_eq!(buffer[(1, 0)].symbol(), "E");

        Ok(())
    }

    #[test]
    fn paragraph_helper_applies_text_style() -> Result<(), Box<dyn std::error::Error>> {
        let theme = Theme::default();
        let backend = TestBackend::new(5, 1);
        let mut terminal = Terminal::new(backend)?;

        terminal.draw(|frame| {
            frame.render_widget(theme.paragraph("Hello"), Rect::new(0, 0, 5, 1));
        })?;

        let buffer = terminal.backend().buffer();
        assert_eq!(buffer[(0, 0)].symbol(), "H");
        assert_eq!(buffer[(4, 0)].symbol(), "o");
        assert_eq!(buffer[(0, 0)].fg, theme.palette.foreground);

        Ok(())
    }
}