pitchfork-cli 2.34.0

Daemons with DX
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
//! Colors of the TUI.
//!
//! The UI never names a color directly: it asks for a role (`theme().red`,
//! `theme().selection`, …). The default `auto` theme only uses the terminal's
//! own palette (ANSI names, `reset` and text modifiers), so it follows any
//! terminal theme, light or dark. Other themes are built in (`catppuccin_*`)
//! or declared by the user under `[themes.<name>]`.

use indexmap::IndexMap;
use ratatui::style::{Color, Modifier, Style};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::str::FromStr;
use std::sync::OnceLock;

pub const AUTO: &str = "auto";

/// Resolved theme: every color the TUI draws with.
#[derive(Debug, Clone, PartialEq)]
pub struct Theme {
    /// Default text.
    pub fg: Color,
    /// Secondary text (placeholders, "-" cells).
    pub muted: Color,
    /// Borders and empty parts of bars.
    pub faint: Color,
    pub red: Color,
    pub orange: Color,
    pub green: Color,
    pub yellow: Color,
    /// Daemons that are only declared in a config (not started yet).
    pub cyan: Color,
    /// Completed oneshot tasks.
    pub dim_green: Color,
    /// Whole-screen background. Unset in `auto`: the terminal's own is kept.
    pub background: Style,
    /// Row under the cursor.
    pub selection: Style,
    /// Row ticked with `Space`.
    pub marked: Style,
    /// Current search match.
    pub match_current: Style,
    /// Other search matches.
    pub match_other: Style,
    /// Background of overlays (help, details, editor, …).
    pub overlay: Style,
    /// Background of the confirmation overlay.
    pub danger: Style,
}

impl Theme {
    /// The default theme: no RGB, no whole-screen background, so every color
    /// is the terminal's own and follows its light or dark theme.
    ///
    /// The cursor row is bold and underlined rather than colored: no ANSI
    /// color is guaranteed to contrast with the text under every terminal theme.
    pub fn auto() -> Self {
        Self {
            fg: Color::Reset,
            muted: Color::DarkGray,
            faint: Color::DarkGray,
            red: Color::Red,
            orange: Color::LightRed,
            green: Color::Green,
            yellow: Color::Yellow,
            cyan: Color::Cyan,
            dim_green: Color::Green,
            // No background: ANSI colors are remapped by the terminal theme, so
            // no one of them is guaranteed to contrast with the text. Reverse
            // video is out too, it would fill colored empty cells (error column).
            background: Style::new(),
            selection: Style::new().add_modifier(Modifier::BOLD | Modifier::UNDERLINED),
            marked: Style::new().add_modifier(Modifier::ITALIC),
            match_current: Style::new().add_modifier(Modifier::REVERSED),
            match_other: Style::new().add_modifier(Modifier::UNDERLINED),
            overlay: Style::new(),
            danger: Style::new(),
        }
    }

    /// Theme of one Catppuccin flavor. It paints its own background and text
    /// color, so it reads the same whatever the terminal's background is.
    fn catppuccin(p: &Palette) -> Self {
        let bg = |c: u32| Style::new().bg(hex(c));
        let panel = Style::new().fg(hex(p.text)).bg(hex(p.base));
        Self {
            fg: hex(p.text),
            muted: hex(p.subtext),
            faint: hex(p.surface),
            red: hex(p.red),
            orange: hex(p.peach),
            green: hex(p.green),
            yellow: hex(p.yellow),
            cyan: hex(p.sky),
            dim_green: hex(p.teal),
            background: panel,
            selection: bg(p.surface_alt),
            marked: bg(p.surface),
            match_current: bg(p.yellow_dim),
            match_other: bg(p.surface),
            overlay: panel,
            danger: panel,
        }
    }

    /// Replaces the roles `def` sets, keeping the others.
    ///
    /// Fails on a color that does not parse, naming `name` and the key. The
    /// full-area styles (`background`, `overlay`, `danger`) always end up with
    /// the final `fg`. A new `bg` also colors the overlays that were following
    /// the screen background (same `bg` in `self`), unless `def` sets them;
    /// overlays that `self` colored differently keep their own color.
    fn apply(mut self, name: &str, def: &ThemeDef) -> miette::Result<Self> {
        let color = |key: &str, v: &Option<String>| -> miette::Result<Option<Color>> {
            v.as_deref()
                .map(|s| {
                    Color::from_str(s).map_err(|_| {
                        miette::miette!(
                            "theme '{name}': invalid color '{s}' for '{key}' \
                             (use #rrggbb, an ANSI name such as bright_black, or reset)"
                        )
                    })
                })
                .transpose()
        };
        let overlay_follows_bg = self.overlay.bg == self.background.bg;
        let danger_follows_bg = self.danger.bg == self.background.bg;
        macro_rules! fg {
            ($($f:ident),*) => {$(
                if let Some(c) = color(stringify!($f), &def.$f)? { self.$f = c; }
            )*};
        }
        macro_rules! bg {
            ($($f:ident => $key:ident),*) => {$(
                if let Some(c) = color(stringify!($key), &def.$key)? {
                    self.$f = Style::new().bg(c);
                }
            )*};
        }
        fg!(
            fg, muted, faint, red, orange, green, yellow, cyan, dim_green
        );
        bg!(
            background => bg,
            selection => selection_bg,
            marked => marked_bg,
            match_current => match_current_bg,
            match_other => match_other_bg,
            overlay => overlay_bg,
            danger => danger_bg
        );
        // Cells are merged onto the buffer and `Clear` wipes it, so every
        // full-area style must carry the foreground itself.
        if def.bg.is_some() {
            if overlay_follows_bg && def.overlay_bg.is_none() {
                self.overlay = self.background;
            }
            if danger_follows_bg && def.danger_bg.is_none() {
                self.danger = self.background;
            }
        }
        for panel in [&mut self.background, &mut self.overlay, &mut self.danger] {
            *panel = panel.fg(self.fg);
        }
        Ok(self)
    }
}

/// A theme declared in `[themes.<name>]`. Only the roles set here differ from
/// `base`; colors are `#rrggbb`, ANSI names (`red`, `bright_black`, …),
/// `reset` (the terminal's own color) or a 0-255 palette index.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct ThemeDef {
    /// Theme to start from: `auto`, a built-in theme or another custom theme.
    /// Defaults to `auto`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub base: Option<String>,
    /// Default text color.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub fg: Option<String>,
    /// Secondary text color.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub muted: Option<String>,
    /// Borders and empty parts of bars.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub faint: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub red: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub orange: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub green: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub yellow: Option<String>,
    /// Daemons declared in a config but not started yet.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub cyan: Option<String>,
    /// Completed oneshot tasks.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub dim_green: Option<String>,
    /// Background of the whole screen.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub bg: Option<String>,
    /// Background of the row under the cursor.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub selection_bg: Option<String>,
    /// Background of rows ticked with `Space`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub marked_bg: Option<String>,
    /// Background of the current search match.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub match_current_bg: Option<String>,
    /// Background of the other search matches.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub match_other_bg: Option<String>,
    /// Background of overlays.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub overlay_bg: Option<String>,
    /// Background of the confirmation overlay.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub danger_bg: Option<String>,
}

/// Resolves `name` against the built-in themes and the user's `themes`.
///
/// A custom theme shadows a built-in one of the same name, and a custom
/// `auto` without `base` customizes the built-in `auto`. Fails, listing the
/// available names, if `name` is unknown; also fails on a `base` cycle or an
/// invalid color.
pub fn resolve(name: &str, custom: &IndexMap<String, ThemeDef>) -> miette::Result<Theme> {
    resolve_inner(name, custom, &mut Vec::new())
}

/// `seen` is the chain of themes being resolved, to detect `base` cycles.
fn resolve_inner<'a>(
    name: &'a str,
    custom: &'a IndexMap<String, ThemeDef>,
    seen: &mut Vec<&'a str>,
) -> miette::Result<Theme> {
    if seen.contains(&name) {
        miette::bail!("theme '{name}' is its own base ({})", seen.join(" -> "));
    }
    seen.push(name);
    // A custom theme shadows a built-in one of the same name.
    if let Some(def) = custom.get(name) {
        let base = match def.base.as_deref() {
            Some(base) => resolve_inner(base, custom, seen)?,
            // `[themes.auto]` customizes the built-in `auto`, not itself.
            None if name == AUTO => Theme::auto(),
            None => resolve_inner(AUTO, custom, seen)?,
        };
        return base.apply(name, def);
    }
    if name == AUTO {
        return Ok(Theme::auto());
    }
    if let Some(p) = PALETTES.iter().find(|p| p.name == name) {
        return Ok(Theme::catppuccin(p));
    }
    let builtin = PALETTES.iter().map(|p| p.name).chain([AUTO]);
    let known: Vec<_> = builtin.chain(custom.keys().map(String::as_str)).collect();
    miette::bail!(
        "unknown TUI theme '{name}' (available: {})",
        known.join(", ")
    );
}

/// Name of the theme to use: `theme` unless it is `auto` and a
/// `light`/`dark` variant is configured for the terminal's background.
///
/// `is_dark` is only called when it matters, as it queries the terminal.
pub fn pick_name<'a>(
    theme: &'a str,
    light: &'a str,
    dark: &'a str,
    is_dark: impl FnOnce() -> Option<bool>,
) -> &'a str {
    if theme != AUTO || (light.is_empty() && dark.is_empty()) {
        return theme;
    }
    let variant = match is_dark() {
        Some(true) => dark,
        Some(false) => light,
        None => "",
    };
    if variant.is_empty() { theme } else { variant }
}

/// Whether the terminal has a dark background, `None` if it does not say.
///
/// Asks the terminal (OSC 11) and waits for its answer, up to one second when
/// it stays silent, so only call it when the answer is needed. It handles the
/// terminal mode itself: call it before entering raw mode.
pub fn terminal_is_dark() -> Option<bool> {
    use terminal_colorsaurus::{QueryOptions, ThemeMode, theme_mode};
    match theme_mode(QueryOptions::default()) {
        Ok(ThemeMode::Dark) => Some(true),
        Ok(ThemeMode::Light) => Some(false),
        Err(_) => None,
    }
}

static THEME: OnceLock<Theme> = OnceLock::new();

/// Installs the theme for this process. Later calls are ignored.
///
/// Call it before the first frame is drawn: [`theme`] falls back to `auto` and
/// keeps it if it runs first.
pub fn init(theme: Theme) {
    let _ = THEME.set(theme);
}

/// The active theme; `auto` until [`init`] is called.
pub fn theme() -> &'static Theme {
    THEME.get_or_init(Theme::auto)
}

/// `0xRRGGBB` to a color.
fn hex(c: u32) -> Color {
    Color::Rgb((c >> 16) as u8, (c >> 8) as u8, c as u8)
}

struct Palette {
    name: &'static str,
    text: u32,
    /// Subtext0: secondary text, which must stay readable on `surface_alt`.
    subtext: u32,
    /// Surface1: borders, marked rows, secondary matches.
    surface: u32,
    /// Surface0: cursor row.
    surface_alt: u32,
    base: u32,
    red: u32,
    peach: u32,
    green: u32,
    yellow: u32,
    sky: u32,
    teal: u32,
    /// Current search match: yellow blended into the background.
    yellow_dim: u32,
}

const PALETTES: &[Palette] = &[
    Palette {
        name: "catppuccin_latte",
        text: 0x4c4f69,
        subtext: 0x6c6f85,
        surface: 0xbcc0cc,
        surface_alt: 0xccd0da,
        base: 0xeff1f5,
        red: 0xd20f39,
        peach: 0xfe640b,
        green: 0x40a02b,
        yellow: 0xdf8e1d,
        sky: 0x04a5e5,
        teal: 0x179299,
        yellow_dim: 0xf2d8a6,
    },
    Palette {
        name: "catppuccin_frappe",
        text: 0xc6d0f5,
        subtext: 0xa5adce,
        surface: 0x51576d,
        surface_alt: 0x414559,
        base: 0x303446,
        red: 0xe78284,
        peach: 0xef9f76,
        green: 0xa6d189,
        yellow: 0xe5c890,
        sky: 0x99d1db,
        teal: 0x81c8be,
        yellow_dim: 0x6b624a,
    },
    Palette {
        name: "catppuccin_macchiato",
        text: 0xcad3f5,
        subtext: 0xa5adcb,
        surface: 0x494d64,
        surface_alt: 0x363a4f,
        base: 0x24273a,
        red: 0xed8796,
        peach: 0xf5a97f,
        green: 0xa6da95,
        yellow: 0xeed49f,
        sky: 0x91d7e3,
        teal: 0x8bd5ca,
        yellow_dim: 0x645a41,
    },
    Palette {
        name: "catppuccin_mocha",
        text: 0xcdd6f4,
        subtext: 0xa6adc8,
        surface: 0x45475a,
        surface_alt: 0x313244,
        base: 0x1e1e2e,
        red: 0xf38ba8,
        peach: 0xfab387,
        green: 0xa6e3a1,
        yellow: 0xf9e2af,
        sky: 0x89dceb,
        teal: 0x94e2d5,
        yellow_dim: 0x5f573f,
    },
];

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

    fn custom(toml: &str) -> IndexMap<String, ThemeDef> {
        toml::from_str(toml).unwrap()
    }

    #[test]
    fn auto_only_uses_the_terminal_palette() {
        let t = resolve(AUTO, &IndexMap::new()).unwrap();
        assert_eq!(t.fg, Color::Reset);
        assert_eq!(t.selection.bg, None);
        assert!(t.selection.add_modifier.contains(Modifier::UNDERLINED));
    }

    #[test]
    fn builtin_catppuccin_is_available() {
        let t = resolve("catppuccin_mocha", &IndexMap::new()).unwrap();
        assert_eq!(t.fg, Color::Rgb(0xcd, 0xd6, 0xf4));
        assert_eq!(t.selection.bg, Some(Color::Rgb(0x31, 0x32, 0x44)));
        assert_eq!(t.background.bg, Some(Color::Rgb(0x1e, 0x1e, 0x2e)));
        assert_eq!(Theme::auto().background.bg, None);
    }

    #[test]
    fn custom_theme_overrides_only_what_it_sets() {
        let c = custom(
            r##"
            [mine]
            base = "catppuccin_latte"
            red = "bright_red"
            selection_bg = "#ff0000"
            "##,
        );
        let t = resolve("mine", &c).unwrap();
        assert_eq!(t.red, Color::LightRed);
        assert_eq!(t.selection.bg, Some(Color::Rgb(255, 0, 0)));
        assert_eq!(t.green, Color::Rgb(0x40, 0xa0, 0x2b));
    }

    #[test]
    fn custom_theme_without_base_starts_from_auto() {
        let c = custom(r#"mine = { yellow = "reset" }"#);
        let t = resolve("mine", &c).unwrap();
        assert_eq!(t.yellow, Color::Reset);
        assert_eq!(t.red, Color::Red);
    }

    #[test]
    fn custom_themes_can_chain_bases() {
        let c = custom(
            r#"
            a = { red = "blue" }
            b = { base = "a", green = "magenta" }
            "#,
        );
        let t = resolve("b", &c).unwrap();
        assert_eq!((t.red, t.green), (Color::Blue, Color::Magenta));
    }

    #[test]
    fn a_custom_auto_without_base_customizes_the_builtin() {
        let c = custom(r#"auto = { red = "magenta" }"#);
        let t = resolve(AUTO, &c).unwrap();
        assert_eq!((t.red, t.green), (Color::Magenta, Color::Green));
    }

    #[test]
    fn full_area_styles_carry_the_foreground() {
        let t = resolve("catppuccin_latte", &IndexMap::new()).unwrap();
        for s in [t.background, t.overlay, t.danger] {
            assert_eq!(s.fg, Some(t.fg));
        }
        let c = custom(r##"mine = { fg = "#111111", bg = "#eeeeee" }"##);
        let t = resolve("mine", &c).unwrap();
        for s in [t.background, t.overlay, t.danger] {
            assert_eq!((s.fg, s.bg), (Some(t.fg), t.background.bg));
        }
    }

    #[test]
    fn a_new_bg_keeps_the_dialog_colors_the_base_chose_apart_from_it() {
        let c = custom(
            r##"
            a = { bg = "#101010", overlay_bg = "#202020" }
            b = { base = "a", bg = "#303030" }
            "##,
        );
        let t = resolve("b", &c).unwrap();
        assert_eq!(t.background.bg, Some(Color::Rgb(0x30, 0x30, 0x30)));
        assert_eq!(t.overlay.bg, Some(Color::Rgb(0x20, 0x20, 0x20)));
        // The confirmation overlay was following the screen, so it still does.
        assert_eq!(t.danger.bg, t.background.bg);
    }

    #[test]
    fn base_cycles_are_rejected() {
        let c = custom(
            r#"
            a = { base = "b" }
            b = { base = "a" }
            "#,
        );
        let err = resolve("a", &c).unwrap_err().to_string();
        assert!(err.contains("own base"), "{err}");
    }

    #[test]
    fn unknown_theme_lists_the_available_ones() {
        let c = custom(r#"mine = {}"#);
        let err = resolve("nope", &c).unwrap_err().to_string();
        assert!(
            err.contains("catppuccin_mocha") && err.contains("mine"),
            "{err}"
        );
    }

    #[test]
    fn invalid_color_names_the_theme_and_key() {
        let c = custom(r#"mine = { red = "blurple" }"#);
        let err = resolve("mine", &c).unwrap_err().to_string();
        assert!(err.contains("'mine'") && err.contains("'red'"), "{err}");
    }

    #[test]
    fn pick_name_follows_the_terminal_only_for_auto() {
        let pick = |theme, light, dark, is_dark| pick_name(theme, light, dark, || is_dark);
        assert_eq!(pick("auto", "l", "d", Some(false)), "l");
        assert_eq!(pick("auto", "l", "d", Some(true)), "d");
        assert_eq!(pick("auto", "l", "", Some(true)), "auto");
        assert_eq!(pick("auto", "l", "d", None), "auto");
        assert_eq!(pick("mine", "l", "d", Some(true)), "mine");
    }

    #[test]
    fn pick_name_does_not_query_the_terminal_when_unneeded() {
        let never = || -> Option<bool> { panic!("terminal queried") };
        assert_eq!(pick_name("auto", "", "", never), "auto");
        assert_eq!(pick_name("mine", "l", "d", never), "mine");
    }

    #[test]
    fn unknown_keys_are_rejected() {
        assert!(toml::from_str::<IndexMap<String, ThemeDef>>("a = { blue = \"red\" }").is_err());
    }
}