ratatui-themekit 0.6.1

Semantic theme system for ratatui — 11 themes, widget builders, full-screen canvas, zebra rows, state-aware styles, NO_COLOR
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
//! Theme-aware builders for ratatui primitives.
//!
//! Chainable builders that produce themed `Span`, `Line`, `Block`,
//! and widget style bundles — zero boilerplate, semantic colors.
//!
//! ```rust
//! use ratatui_themekit::{CatppuccinMocha, ThemeExt};
//!
//! let t = CatppuccinMocha;
//!
//! // Span builder
//! let title = t.fg_accent("Ralph Engine").bold();
//!
//! // Line compositor
//! let line = t.line()
//!     .accent("Dashboard")
//!     .dim(" | ")
//!     .success("3 passed")
//!     .build();
//!
//! // Block builder
//! let panel = t.block(" Status ").focused(true).build();
//!
//! // Status bar
//! let status = t.status_line()
//!     .kv("Mode", "Normal")
//!     .kv("File", "main.rs")
//!     .build();
//!
//! // Widget style bundles
//! let table = t.table_styles();
//! let list = t.list_styles();
//! let tabs = t.tab_styles();
//! let state = t.state_styles();
//! ```

mod bar;
mod block;
mod line;
mod span;
mod status_line;
mod styles;

use ratatui::style::{Color, Style};
use ratatui::text::Line;

use crate::Theme;

pub use bar::ThemedBar;
pub use block::ThemedBlock;
pub use line::ThemedLine;
pub use span::ThemedSpan;
pub use status_line::ThemedStatusLine;
pub use styles::{
    GaugeStyles, InputStyles, ListStyles, NotificationStyles, ScrollbarStyles, StateStyles,
    TabStyles, TableStyles, zebra_rows,
};

/// Extension trait — adds chainable builder methods to any `Theme`.
///
/// Import `ThemeExt` to get span builders, line compositors, block
/// builders, status lines, and widget style bundles on any theme.
pub trait ThemeExt: Theme {
    // ── Span builders ───────────────────────────────────────────

    /// Span with accent color.
    fn fg_accent<'a>(&self, text: impl Into<std::borrow::Cow<'a, str>>) -> ThemedSpan<'a> {
        ThemedSpan::new(text, self.accent())
    }
    /// Span with dim text.
    fn fg_dim<'a>(&self, text: impl Into<std::borrow::Cow<'a, str>>) -> ThemedSpan<'a> {
        ThemedSpan::new(text, self.text_dim())
    }
    /// Span with bright text.
    fn fg_bright<'a>(&self, text: impl Into<std::borrow::Cow<'a, str>>) -> ThemedSpan<'a> {
        ThemedSpan::new(text, self.text_bright())
    }
    /// Span with default text.
    fn fg_text<'a>(&self, text: impl Into<std::borrow::Cow<'a, str>>) -> ThemedSpan<'a> {
        ThemedSpan::new(text, self.text())
    }
    /// Span with success color.
    fn fg_success<'a>(&self, text: impl Into<std::borrow::Cow<'a, str>>) -> ThemedSpan<'a> {
        ThemedSpan::new(text, self.success())
    }
    /// Span with error color.
    fn fg_error<'a>(&self, text: impl Into<std::borrow::Cow<'a, str>>) -> ThemedSpan<'a> {
        ThemedSpan::new(text, self.error())
    }
    /// Span with warning color.
    fn fg_warning<'a>(&self, text: impl Into<std::borrow::Cow<'a, str>>) -> ThemedSpan<'a> {
        ThemedSpan::new(text, self.warning())
    }
    /// Span with info color.
    fn fg_info<'a>(&self, text: impl Into<std::borrow::Cow<'a, str>>) -> ThemedSpan<'a> {
        ThemedSpan::new(text, self.info())
    }
    /// Span with diff-added color.
    fn fg_added<'a>(&self, text: impl Into<std::borrow::Cow<'a, str>>) -> ThemedSpan<'a> {
        ThemedSpan::new(text, self.diff_added())
    }
    /// Span with diff-removed color.
    fn fg_removed<'a>(&self, text: impl Into<std::borrow::Cow<'a, str>>) -> ThemedSpan<'a> {
        ThemedSpan::new(text, self.diff_removed())
    }
    /// Span with border color.
    fn fg_border<'a>(&self, text: impl Into<std::borrow::Cow<'a, str>>) -> ThemedSpan<'a> {
        ThemedSpan::new(text, self.border())
    }

    // ── Composite builders ──────────────────────────────────────

    /// Themed progress bar builder.
    fn bar(&self, percent: u8) -> ThemedBar {
        ThemedBar::new(percent, self.accent(), self.border())
    }

    /// Themed separator line.
    fn separator_line(&self, width: u16) -> Line<'static> {
        Line::styled(
            " \u{00b7} ".repeat((width / 3) as usize),
            Style::default().fg(self.border()),
        )
    }

    /// Badge-style span: text with colored background.
    fn badge<'a>(&self, text: impl Into<std::borrow::Cow<'a, str>>, bg: Color) -> ThemedSpan<'a> {
        ThemedSpan::new(text, self.surface()).on(bg).bold()
    }

    /// Themed block builder with title.
    fn block<'a>(&self, title: &'a str) -> ThemedBlock<'a> {
        ThemedBlock::new(self, Some(title))
    }

    /// Themed block builder without title.
    fn block_plain(&self) -> ThemedBlock<'static> {
        ThemedBlock::new(self, None)
    }

    /// Themed line compositor.
    fn line(&self) -> ThemedLine<'static> {
        ThemedLine::new(self)
    }

    /// Themed status line (key-value pairs).
    fn status_line(&self) -> ThemedStatusLine<'static> {
        ThemedStatusLine::new(self)
    }

    // ── Widget style bundles ────────────────────────────────────

    /// Style bundle for `Table` (header, row, highlight, stripe).
    fn table_styles(&self) -> TableStyles {
        TableStyles::from_theme(self)
    }

    /// Style bundle for `List` (base, highlight, symbol).
    fn list_styles(&self) -> ListStyles {
        ListStyles::from_theme(self)
    }

    /// Style bundle for `Tabs` (active, inactive).
    fn tab_styles(&self) -> TabStyles {
        TabStyles::from_theme(self)
    }

    /// Style bundle for `Gauge` (filled, base).
    fn gauge_styles(&self) -> GaugeStyles {
        GaugeStyles::from_theme(self)
    }

    /// Style bundle for input fields (text, placeholder, cursor, prompt, border).
    fn input_styles(&self) -> InputStyles {
        InputStyles::from_theme(self)
    }

    /// Style bundle for scrollbar widgets (track, thumb).
    fn scrollbar_styles(&self) -> ScrollbarStyles {
        ScrollbarStyles::from_theme(self)
    }

    /// Style bundle for notifications/toasts by severity.
    fn notification_styles(&self) -> NotificationStyles {
        NotificationStyles::from_theme(self)
    }

    /// State-aware style resolver (normal, focused, selected, disabled).
    fn state_styles(&self) -> StateStyles {
        StateStyles::from_theme(self)
    }

    // ── Style helpers (for widget APIs that take Style) ──────

    /// Style with accent foreground.
    fn style_accent(&self) -> Style {
        Style::default().fg(self.accent())
    }
    /// Style with border foreground.
    fn style_border(&self) -> Style {
        Style::default().fg(self.border())
    }
    /// Style with error foreground.
    fn style_error(&self) -> Style {
        Style::default().fg(self.error())
    }
    /// Style with warning foreground.
    fn style_warning(&self) -> Style {
        Style::default().fg(self.warning())
    }
    /// Style with success foreground.
    fn style_success(&self) -> Style {
        Style::default().fg(self.success())
    }
    /// Style with bright text foreground.
    fn style_bright(&self) -> Style {
        Style::default().fg(self.text_bright())
    }
    /// Style with dim text foreground.
    fn style_dim(&self) -> Style {
        Style::default().fg(self.text_dim())
    }
    /// Style with info foreground.
    fn style_info(&self) -> Style {
        Style::default().fg(self.info())
    }
    /// Style with surface background.
    fn style_surface(&self) -> Style {
        Style::default().bg(self.surface())
    }
    /// Base style for the full terminal canvas — background + default text.
    ///
    /// Render a `Block` with this style on `frame.area()` as the **first**
    /// widget every frame. All subsequent widgets inherit the background
    /// automatically (ratatui's style system is patch-based).
    ///
    /// ```ignore
    /// frame.render_widget(
    ///     ratatui::widgets::Block::default().style(theme.style_base()),
    ///     frame.area(),
    /// );
    /// ```
    ///
    /// Plugins can still override the background on individual widgets
    /// by setting their own `.bg(color)` — the canvas is just the default.
    fn style_base(&self) -> Style {
        Style::default().bg(self.background()).fg(self.text())
    }
}

// Blanket impl — every Theme gets ThemeExt
impl<T: Theme + ?Sized> ThemeExt for T {}

/// Creates a `Style` with the given foreground color.
///
/// Convenience for widget APIs that accept `Style`.
///
/// ```rust
/// use ratatui::style::Color;
/// use ratatui_themekit::builders::style_fg;
///
/// let s = style_fg(Color::Green);
/// ```
#[must_use]
pub fn style_fg(color: Color) -> Style {
    Style::default().fg(color)
}

#[cfg(test)]
mod tests {
    use ratatui::style::Modifier;

    use super::*;
    use crate::{CATPPUCCIN_MOCHA, CatppuccinMocha, Dracula, NoColor, Theme};

    // ── fg_* builders ───────────────────────────────────────────

    #[test]
    fn fg_accent_uses_theme_accent() {
        let span = CatppuccinMocha.fg_accent("hello").build();
        assert_eq!(span.style.fg, Some(CATPPUCCIN_MOCHA.accent));
    }

    #[test]
    fn fg_dim_uses_theme_text_dim() {
        let span = CatppuccinMocha.fg_dim("muted").build();
        assert_eq!(span.style.fg, Some(CATPPUCCIN_MOCHA.text_dim));
    }

    #[test]
    fn fg_success_uses_theme_success() {
        let span = CatppuccinMocha.fg_success("ok").build();
        assert_eq!(span.style.fg, Some(CATPPUCCIN_MOCHA.success));
    }

    #[test]
    fn fg_error_uses_theme_error() {
        let span = CatppuccinMocha.fg_error("fail").build();
        assert_eq!(span.style.fg, Some(CATPPUCCIN_MOCHA.error));
    }

    // ── Modifiers ───────────────────────────────────────────────

    #[test]
    fn bold_adds_bold_modifier() {
        let span = CatppuccinMocha.fg_accent("title").bold().build();
        assert!(span.style.add_modifier.contains(Modifier::BOLD));
    }

    #[test]
    fn chained_modifiers_combine() {
        let span = CatppuccinMocha.fg_accent("both").bold().italic().build();
        assert!(span.style.add_modifier.contains(Modifier::BOLD));
        assert!(span.style.add_modifier.contains(Modifier::ITALIC));
    }

    #[test]
    fn on_sets_background() {
        let span = CatppuccinMocha.fg_accent("badge").on(Color::Red).build();
        assert_eq!(span.style.bg, Some(Color::Red));
    }

    // ── Badge / separator ───────────────────────────────────────

    #[test]
    fn badge_has_background_and_bold() {
        let span = CatppuccinMocha.badge(" RUN ", Color::Green).build();
        assert_eq!(span.style.bg, Some(Color::Green));
        assert!(span.style.add_modifier.contains(Modifier::BOLD));
    }

    #[test]
    fn separator_produces_content() {
        let line = CatppuccinMocha.separator_line(30);
        let text: String = line.spans.iter().map(|s| s.content.to_string()).collect();
        assert!(text.contains('\u{00b7}'));
    }

    // ── Style helpers ───────────────────────────────────────────

    #[test]
    fn style_helpers_produce_correct_colors() {
        let t = CatppuccinMocha;
        assert_eq!(t.style_accent().fg, Some(t.accent));
        assert_eq!(t.style_border().fg, Some(t.border));
        assert_eq!(t.style_error().fg, Some(t.error));
    }

    #[test]
    fn style_info_and_surface() {
        let t = CatppuccinMocha;
        assert_eq!(t.style_info().fg, Some(t.info));
        assert_eq!(t.style_surface().bg, Some(t.surface));
    }

    #[test]
    fn style_fg_with_dynamic_color() {
        let s = style_fg(Color::Rgb(1, 2, 3));
        assert_eq!(s.fg, Some(Color::Rgb(1, 2, 3)));
    }

    // ── dyn Theme + NoColor ─────────────────────────────────────

    #[test]
    fn builders_work_on_dyn_theme() {
        let t: &dyn Theme = &Dracula;
        let span = t.fg_accent("test").bold().build();
        assert_eq!(span.style.fg, Some(t.accent()));
    }

    #[test]
    fn no_color_builders_produce_reset() {
        let span = NoColor.fg_accent("text").build();
        assert_eq!(span.style.fg, Some(Color::Reset));
    }

    // ── Composite builders via ThemeExt ─────────────────────────

    #[test]
    fn block_builder_via_theme_ext() {
        let block = CatppuccinMocha.block(" Test ").build();
        let _ = block;
    }

    #[test]
    fn block_plain_via_theme_ext() {
        let block = CatppuccinMocha.block_plain().build();
        let _ = block;
    }

    #[test]
    fn line_builder_via_theme_ext() {
        let line = CatppuccinMocha
            .line()
            .accent("A")
            .dim(" | ")
            .success("B")
            .build();
        assert_eq!(line.spans.len(), 3);
    }

    #[test]
    fn status_line_via_theme_ext() {
        let line = CatppuccinMocha
            .status_line()
            .kv("Mode", "Normal")
            .kv("File", "main.rs")
            .build();
        assert_eq!(line.spans.len(), 7); // pair(3) + sep(1) + pair(3)
    }

    #[test]
    fn table_styles_via_theme_ext() {
        let ts = CatppuccinMocha.table_styles();
        assert_eq!(ts.header.fg, Some(CatppuccinMocha.accent));
    }

    #[test]
    fn list_styles_via_theme_ext() {
        let ls = CatppuccinMocha.list_styles();
        assert_eq!(ls.highlight.fg, Some(CatppuccinMocha.accent));
    }

    #[test]
    fn tab_styles_via_theme_ext() {
        let ts = CatppuccinMocha.tab_styles();
        assert_eq!(ts.active.fg, Some(CatppuccinMocha.accent));
    }

    #[test]
    fn gauge_styles_via_theme_ext() {
        let gs = CatppuccinMocha.gauge_styles();
        assert_eq!(gs.filled.fg, Some(CatppuccinMocha.accent));
    }

    #[test]
    fn state_styles_via_theme_ext() {
        let ss = CatppuccinMocha.state_styles();
        assert_eq!(ss.normal.fg, Some(CatppuccinMocha.text));
        assert_eq!(ss.focused.fg, Some(CatppuccinMocha.accent));
    }

    #[test]
    fn input_styles_via_theme_ext() {
        let is = CatppuccinMocha.input_styles();
        assert_eq!(is.cursor.fg, Some(CatppuccinMocha.accent));
    }

    #[test]
    fn scrollbar_styles_via_theme_ext() {
        let ss = CatppuccinMocha.scrollbar_styles();
        assert_eq!(ss.track.fg, Some(CatppuccinMocha.border));
    }

    #[test]
    fn notification_styles_via_theme_ext() {
        let ns = CatppuccinMocha.notification_styles();
        assert_eq!(ns.error.fg, Some(CatppuccinMocha.error));
    }

    // ── Edge cases ──────────────────────────────────────────────

    #[test]
    fn empty_string_produces_empty_span() {
        let span = CatppuccinMocha.fg_accent("").build();
        assert!(span.content.is_empty());
    }

    #[test]
    fn owned_string_accepted() {
        let text = format!("dynamic {}", 42);
        let span = CatppuccinMocha.fg_accent(text).build();
        assert!(span.content.contains("42"));
    }

    #[test]
    fn separator_zero_width_no_panic() {
        let line = CatppuccinMocha.separator_line(0);
        assert!(line.spans.len() <= 1);
    }

    #[test]
    fn with_color_reset() {
        let span = ThemedSpan::with_color("text", Color::Reset).build();
        assert_eq!(span.style.fg, Some(Color::Reset));
    }

    #[test]
    fn into_span_conversion() {
        let themed = CatppuccinMocha.fg_success("ok").bold();
        let span: ratatui::text::Span<'_> = themed.into();
        assert!(span.style.add_modifier.contains(Modifier::BOLD));
    }
}