whirr 0.3.7

A whirring macOS system dashboard for your terminal
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
//! What the user has chosen, and the palette that follows from it.
//!
//! Kept apart from `ui::theme` because a `Theme` is the *result* — eleven
//! resolved colours — while these are the handful of choices a person
//! actually makes. `Settings::theme()` is the only bridge between them, which
//! keeps the widgets ignorant of how their colours were picked.

use std::path::PathBuf;

use ratatui::style::Color;
use serde::{Deserialize, Serialize};

use crate::ui::theme::Theme;

/// Which base palette to build on.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Palette {
    Dark,
    Light,
}

impl Palette {
    pub const ALL: [Palette; 2] = [Palette::Dark, Palette::Light];

    pub fn next(self) -> Self {
        match self {
            Palette::Dark => Palette::Light,
            Palette::Light => Palette::Dark,
        }
    }

    pub fn label(self) -> &'static str {
        match self {
            Palette::Dark => "dark",
            Palette::Light => "light",
        }
    }
}

/// The one colour that is genuinely a matter of taste. It drives gauges,
/// selection, sparklines and the burst fan, so it gets its own choice rather
/// than being welded to the palette.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Accent {
    Teal,
    Blue,
    Violet,
    Amber,
    Green,
}

impl Accent {
    pub const ALL: [Accent; 5] =
        [Accent::Teal, Accent::Blue, Accent::Violet, Accent::Amber, Accent::Green];

    pub fn next(self) -> Self {
        let i = Self::ALL.iter().position(|&a| a == self).expect("ALL covers every Accent");
        Self::ALL[(i + 1) % Self::ALL.len()]
    }

    pub fn label(self) -> &'static str {
        match self {
            Accent::Teal => "teal",
            Accent::Blue => "blue",
            Accent::Violet => "violet",
            Accent::Amber => "amber",
            Accent::Green => "green",
        }
    }

    /// Two versions of each: a light background needs a darker, more saturated
    /// accent to hold the same contrast a bright one holds against near-black.
    fn color(self, palette: Palette) -> Color {
        match (self, palette) {
            (Accent::Teal, Palette::Dark) => Color::Rgb(45, 225, 194),
            (Accent::Teal, Palette::Light) => Color::Rgb(0, 132, 112),
            (Accent::Blue, Palette::Dark) => Color::Rgb(94, 178, 255),
            (Accent::Blue, Palette::Light) => Color::Rgb(21, 101, 192),
            (Accent::Violet, Palette::Dark) => Color::Rgb(178, 148, 255),
            (Accent::Violet, Palette::Light) => Color::Rgb(94, 53, 177),
            (Accent::Amber, Palette::Dark) => Color::Rgb(255, 180, 84),
            (Accent::Amber, Palette::Light) => Color::Rgb(160, 90, 0),
            (Accent::Green, Palette::Dark) => Color::Rgb(74, 214, 109),
            (Accent::Green, Palette::Light) => Color::Rgb(28, 122, 54),
        }
    }

    /// The dim end of the heatmap gradient.
    ///
    /// A table rather than "the accent mixed 25% into the background", for the
    /// same reason the light palette is hand-picked: the shipped teal-on-dark
    /// gradient starts at (14, 58, 58), and no single mix ratio reproduces it
    /// — it was tuned by eye. Computing it would have quietly shifted the
    /// default look, which is the one thing this feature must not do.
    fn gradient_from(self, palette: Palette) -> (u8, u8, u8) {
        match (self, palette) {
            (Accent::Teal, Palette::Dark) => (14, 58, 58),
            (Accent::Teal, Palette::Light) => (200, 228, 222),
            (Accent::Blue, Palette::Dark) => (16, 48, 76),
            (Accent::Blue, Palette::Light) => (190, 211, 235),
            (Accent::Violet, Palette::Dark) => (46, 42, 78),
            (Accent::Violet, Palette::Light) => (208, 199, 231),
            (Accent::Amber, Palette::Dark) => (68, 50, 28),
            (Accent::Amber, Palette::Light) => (225, 209, 187),
            (Accent::Green, Palette::Dark) => (20, 60, 34),
            (Accent::Green, Palette::Light) => (192, 217, 200),
        }
    }
}

/// Everything the settings dialog can change.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Settings {
    pub palette: Palette,
    pub accent: Accent,
    /// Leave the frame unpainted so the terminal's own background — including
    /// transparency and blur — shows through.
    pub terminal_bg: bool,
    pub fan: bool,
}

impl Default for Settings {
    /// Exactly what whirr shipped before any of this existed. A default that
    /// changes the look would make the feature a redesign.
    fn default() -> Self {
        Self { palette: Palette::Dark, accent: Accent::Teal, terminal_bg: false, fan: true }
    }
}

/// The on-disk shape, kept separate from `Settings` so the file format can
/// stay stable while the in-memory type moves.
///
/// Every field is `Option<String>` rather than a typed enum: serde would
/// reject the *whole file* over one unrecognised value, so a typo in the
/// accent would silently discard a deliberate theme choice. Parsing each
/// field by hand keeps a bad value costing only itself.
#[derive(Serialize, Deserialize, Default)]
struct ConfigFile {
    #[serde(default)]
    appearance: Appearance,
    #[serde(default)]
    behaviour: Behaviour,
}

#[derive(Serialize, Deserialize, Default)]
struct Appearance {
    theme: Option<String>,
    accent: Option<String>,
    background: Option<String>,
}

#[derive(Serialize, Deserialize, Default)]
struct Behaviour {
    fan: Option<bool>,
}

impl Palette {
    fn from_label(s: &str) -> Option<Self> {
        Self::ALL.into_iter().find(|p| p.label() == s)
    }
}

impl Accent {
    fn from_label(s: &str) -> Option<Self> {
        Self::ALL.into_iter().find(|a| a.label() == s)
    }
}

impl Settings {
    /// Where the choices are remembered between runs.
    pub fn config_path() -> Option<PathBuf> {
        let base = std::env::var_os("XDG_CONFIG_HOME")
            .map(PathBuf::from)
            .or_else(|| std::env::var_os("HOME").map(|h| PathBuf::from(h).join(".config")))?;
        Some(base.join("whirr").join("config.toml"))
    }

    /// Read the config, or fall back to defaults.
    ///
    /// Never fails: a dashboard that refuses to start over a stray character
    /// in a preferences file would be worse than one that ignores it.
    pub fn load() -> Self {
        Self::config_path()
            .and_then(|p| std::fs::read_to_string(p).ok())
            .map(|t| Self::from_toml(&t))
            .unwrap_or_default()
    }

    /// Write the current choices. Errors are dropped — failing to persist a
    /// preference is not worth interrupting the dashboard for.
    pub fn save(&self) {
        let Some(path) = Self::config_path() else { return };
        if let Some(dir) = path.parent() {
            let _ = std::fs::create_dir_all(dir);
        }
        let _ = std::fs::write(path, self.to_toml());
    }

    /// Apply whatever the file got right, on top of the defaults.
    pub fn from_toml(text: &str) -> Self {
        let file: ConfigFile = toml::from_str(text).unwrap_or_default();
        let mut s = Self::default();
        if let Some(p) = file.appearance.theme.as_deref().and_then(Palette::from_label) {
            s.palette = p;
        }
        if let Some(a) = file.appearance.accent.as_deref().and_then(Accent::from_label) {
            s.accent = a;
        }
        match file.appearance.background.as_deref() {
            Some("terminal") => s.terminal_bg = true,
            Some("painted") => s.terminal_bg = false,
            _ => {}
        }
        if let Some(fan) = file.behaviour.fan {
            s.fan = fan;
        }
        s
    }

    pub fn to_toml(&self) -> String {
        let file = ConfigFile {
            appearance: Appearance {
                theme: Some(self.palette.label().to_string()),
                accent: Some(self.accent.label().to_string()),
                background: Some(
                    if self.terminal_bg { "terminal" } else { "painted" }.to_string(),
                ),
            },
            behaviour: Behaviour { fan: Some(self.fan) },
        };
        // The only way this fails is a bug in this function, not in user input.
        toml::to_string_pretty(&file).unwrap_or_default()
    }

    /// Whether "use the terminal's background" is a choice at all right now.
    ///
    /// It isn't under the light palette: light means *dark* text, and leaving
    /// the frame unpainted would put that text straight onto whatever the
    /// terminal's own background is. On a dark terminal that is unreadable,
    /// and whirr has no reliable way to find out which the user has. The dark
    /// palette has no such problem — its text is light, and the terminals
    /// people run translucent are overwhelmingly dark.
    pub fn terminal_bg_available(&self) -> bool {
        matches!(self.palette, Palette::Dark)
    }

    /// Resolve the choices into the eleven colours the widgets draw with.
    pub fn theme(&self) -> Theme {
        let base = match self.palette {
            Palette::Dark => Theme::dark(),
            Palette::Light => Theme::light(),
        };
        let accent = self.accent.color(self.palette);
        Theme {
            accent,
            grad_from: self.accent.gradient_from(self.palette),
            grad_to: match accent {
                Color::Rgb(r, g, b) => (r, g, b),
                _ => base.grad_to,
            },
            // `base` keeps its colour even when the frame is left unpainted:
            // it is what `ramp` and `blend` darken toward, and `Color::Reset`
            // there would panic inside `blend` on every frame of the fan.
            // Overridden rather than cleared when unavailable: someone who
            // turned the background off, tried light, and went back to dark
            // should not have to set it again.
            paint_bg: !(self.terminal_bg && self.terminal_bg_available()),
            ..base
        }
    }
}

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

    fn rgb(c: Color) -> (u8, u8, u8) {
        match c {
            Color::Rgb(r, g, b) => (r, g, b),
            other => panic!("the palette must be Rgb throughout, got {other:?}"),
        }
    }

    fn luma(c: Color) -> f32 {
        let (r, g, b) = rgb(c);
        0.2126 * r as f32 + 0.7152 * g as f32 + 0.0722 * b as f32
    }

    #[test]
    fn the_light_palette_is_actually_light_and_the_dark_one_dark() {
        let dark = Settings { palette: Palette::Dark, ..Settings::default() }.theme();
        let light = Settings { palette: Palette::Light, ..Settings::default() }.theme();
        assert!(luma(dark.base) < 40.0, "dark base should be near-black");
        assert!(luma(light.base) > 200.0, "light base should be near-white");
        // Text has to invert with it, or a light theme is unreadable.
        assert!(luma(dark.text) > luma(dark.base), "dark theme needs light text");
        assert!(luma(light.text) < luma(light.base), "light theme needs dark text");
    }

    #[test]
    fn a_light_palette_is_not_an_inversion_of_the_dark_one() {
        // Every colour hand-picked for its background. If light were just
        // dark with base and text swapped, the accent and the dim would keep
        // dark-background contrast ratios and read as washed out.
        let dark = Settings { palette: Palette::Dark, ..Settings::default() }.theme();
        let light = Settings { palette: Palette::Light, ..Settings::default() }.theme();
        for (name, d, l) in [
            ("accent", dark.accent, light.accent),
            ("dim", dark.dim, light.dim),
            ("bg_cell", dark.bg_cell, light.bg_cell),
        ] {
            assert_ne!(d, l, "{name} must be picked for its own background");
        }
    }

    #[test]
    fn every_palette_and_accent_stays_rgb() {
        // `theme::blend` panics on anything but Rgb, and it runs on every
        // frame of the burst fan. A palette carrying Color::Reset would take
        // the whole dashboard down.
        for palette in Palette::ALL {
            for accent in Accent::ALL {
                let t = Settings { palette, accent, ..Settings::default() }.theme();
                for c in [t.accent, t.amber, t.red, t.green, t.text, t.dim, t.bg_cell, t.base, t.bg_modal] {
                    rgb(c);
                }
            }
        }
    }

    #[test]
    fn choosing_an_accent_overrides_the_palettes_own() {
        let teal = Settings { accent: Accent::Teal, ..Settings::default() }.theme();
        let violet = Settings { accent: Accent::Violet, ..Settings::default() }.theme();
        assert_ne!(teal.accent, violet.accent);
        // The gradient is built from the accent, so it has to follow.
        assert_ne!(teal.gradient(1.0), violet.gradient(1.0));
        assert_eq!(violet.gradient(1.0), violet.accent, "gradient should top out at the accent");
    }

    #[test]
    fn using_the_terminal_background_leaves_the_blend_colour_intact() {
        // The frame simply isn't painted; `base` keeps its real colour so
        // `ramp` and `blend` still have something to darken toward. Setting it
        // to Color::Reset instead would panic in blend.
        let painted = Settings { terminal_bg: false, ..Settings::default() }.theme();
        let bare = Settings { terminal_bg: true, ..Settings::default() }.theme();
        assert_eq!(painted.base, bare.base, "base is the blend anchor, not just a fill");
        assert!(painted.paint_bg && !bare.paint_bg);
        rgb(bare.ramp(bare.accent, 0.5));
    }

    #[test]
    fn a_light_palette_always_paints_its_own_background() {
        // Light means dark text. Leaving the frame unpainted would put that
        // text on whatever the terminal's background is — unreadable on a
        // dark one, and whirr has no way to know which it has.
        let s = Settings { palette: Palette::Light, terminal_bg: true, ..Settings::default() };
        assert!(s.theme().paint_bg, "a light palette must paint, whatever was asked for");
        assert!(!s.terminal_bg_available(), "and the dialog must say so");
    }

    #[test]
    fn switching_to_light_and_back_keeps_the_background_choice() {
        // The preference is overridden, not destroyed: someone who turned the
        // background off, tried light, and went back to dark should not have
        // to set it again.
        let mut s = Settings { terminal_bg: true, ..Settings::default() };
        assert!(!s.theme().paint_bg, "dark honours the choice");
        s.palette = Palette::Light;
        assert!(s.theme().paint_bg, "light overrides it");
        s.palette = Palette::Dark;
        assert!(!s.theme().paint_bg, "and dark gets it back");
    }

    #[test]
    fn the_dark_palette_still_offers_the_terminal_background() {
        let s = Settings { palette: Palette::Dark, ..Settings::default() };
        assert!(s.terminal_bg_available());
    }

    #[test]
    fn settings_start_as_the_palette_whirr_has_always_shipped() {
        let d = Settings::default();
        assert_eq!(d.theme(), crate::ui::theme::Theme::dark(), "defaults must not change the look");
        assert!(d.fan, "the fan is on unless asked otherwise");
    }

    #[test]
    fn a_written_config_reads_back_as_the_same_settings() {
        let s = Settings {
            palette: Palette::Light,
            accent: Accent::Violet,
            terminal_bg: true,
            fan: false,
        };
        assert_eq!(Settings::from_toml(&s.to_toml()), s);
    }

    #[test]
    fn the_file_reads_the_way_a_person_would_write_it() {
        let text = Settings::default().to_toml();
        assert!(text.contains("[appearance]"), "{text}");
        assert!(text.contains("theme = \"dark\""), "{text}");
        assert!(text.contains("background = \"painted\""), "{text}");
        assert!(text.contains("[behaviour]"), "{text}");
    }

    #[test]
    fn a_partial_config_keeps_the_defaults_for_everything_it_omits() {
        // A file written by an older whirr must not reset the settings it
        // never knew about.
        let s = Settings::from_toml("[appearance]\ntheme = \"light\"\n");
        assert_eq!(s.palette, Palette::Light);
        assert_eq!(s.accent, Settings::default().accent, "omitted keys keep their default");
        assert_eq!(s.fan, Settings::default().fan);
    }

    #[test]
    fn one_bad_value_costs_only_that_setting() {
        // Per-field tolerance rather than all-or-nothing: a typo in the accent
        // must not silently throw away a deliberate theme choice.
        let s = Settings::from_toml(
            "[appearance]\ntheme = \"light\"\naccent = \"chartreuse\"\n",
        );
        assert_eq!(s.palette, Palette::Light, "the valid setting survives");
        assert_eq!(s.accent, Settings::default().accent, "the invalid one falls back");
    }

    #[test]
    fn an_unreadable_config_is_ignored_rather_than_fatal() {
        // A dashboard that refuses to start because of a stray character in a
        // preferences file would be worse than one that ignores it.
        for junk in ["", "not toml at all {{{", "[appearance", "theme = "] {
            assert_eq!(Settings::from_toml(junk), Settings::default(), "{junk:?}");
        }
    }

    #[test]
    fn unknown_keys_from_a_newer_whirr_are_ignored() {
        let s = Settings::from_toml(
            "[appearance]\ntheme = \"light\"\nsparkles = true\n\n[future]\nx = 1\n",
        );
        assert_eq!(s.palette, Palette::Light);
    }

    #[test]
    fn each_choice_cycles_through_all_of_its_options_and_wraps() {
        let mut p = Palette::Dark;
        for _ in 0..Palette::ALL.len() {
            p = p.next();
        }
        assert_eq!(p, Palette::Dark, "cycling every option returns to the start");

        let mut a = Accent::Teal;
        let mut seen = vec![a];
        for _ in 1..Accent::ALL.len() {
            a = a.next();
            assert!(!seen.contains(&a), "cycle must not repeat before it wraps");
            seen.push(a);
        }
        assert_eq!(a.next(), Accent::Teal);
    }
}