syswatch 0.8.0

Single-host, read-only system diagnostics TUI. Twelve tabs covering CPU, memory, disks, processes, GPU, power, services, network, plus a Timeline scrubber and an Insights anomaly engine. Sibling to netwatch.
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
//! Color themes for the syswatch TUI.
//!
//! Mirrors netwatch's theme conventions: the same semantic field set and
//! the same six built-in palettes. Two syswatch-specific extensions:
//! `bg` (panel fill — netwatch keeps the terminal background, syswatch
//! paints panels) and `warn_bg` / `err_bg` (insights row tints).
//!
//! A global `RwLock<Theme>` holds the active theme; `palette::*` accessors
//! delegate here, so `theme::set` / `theme::cycle` re-color the whole UI on
//! the next draw.

use ratatui::style::Color;
use std::sync::RwLock;

/// Semantic color slots — netwatch-compatible plus syswatch panel-bg extensions.
///
/// All netwatch slots are kept intentionally — even `text_secondary`,
/// `text_inverse`, `rx_rate`, `highlight_bg`, and `separator` which the
/// current syswatch UI doesn't read yet. Keeping them present means
/// per-theme palettes don't have to change shape when new UI lands.
#[allow(dead_code)]
#[derive(Debug, Clone, Copy)]
pub struct Theme {
    pub name: &'static str,

    // ── Brand / chrome ──────────────────────────────────
    pub brand: Color,
    pub active_tab: Color,
    pub inactive_tab: Color,
    pub border: Color,
    pub separator: Color,

    // ── Text ────────────────────────────────────────────
    pub text_primary: Color,
    pub text_secondary: Color,
    pub text_muted: Color,
    pub text_inverse: Color,

    // ── Status / semantic ───────────────────────────────
    pub status_good: Color,
    pub status_warn: Color,
    pub status_error: Color,
    pub status_info: Color,

    // ── Data ────────────────────────────────────────────
    pub rx_rate: Color,
    pub tx_rate: Color,
    pub key_hint: Color,

    // ── Selection ───────────────────────────────────────
    pub selection_bg: Color,
    pub highlight_bg: Color,

    // ── syswatch extensions ─────────────────────────────
    /// Panel fill. Netwatch leaves bg to the terminal; syswatch paints
    /// every panel for the dense-dashboard look.
    pub bg: Color,
    /// Row tint behind a Warn-severity insight.
    pub warn_bg: Color,
    /// Row tint behind a Crit-severity insight.
    pub err_bg: Color,
}

pub const THEME_NAMES: &[&str] = &[
    "dark",
    "light",
    "ocean",
    "solarized",
    "dracula",
    "nord",
    "terminal",
];

/// Defers entirely to the terminal's own palette: ANSI slots 0–15 for
/// color and `Color::Reset` for foreground and background, so whatever
/// the user's terminal theme defines is what syswatch renders. Nothing
/// here is a fixed RGB value — that is the whole point. Users running a
/// system-wide theming setup (pywal, matugen, a terminal profile) get a
/// syswatch that matches the rest of their desktop without maintaining a
/// separate palette.
///
/// Two deliberate compromises, both forced by the 16-color palette:
///
/// - `warn_bg` / `err_bg` are `Reset` rather than a tint. ANSI has no
///   "slightly red background" — the nearest option is a full-intensity
///   red fill, which is far louder than the subtle row tint these slots
///   exist for. Severity still reads from the foreground
///   (`status_warn` / `status_error`), which is how terminal-native tools
///   convey it anyway.
/// - `selection_bg` / `highlight_bg` use `Indexed(8)` (bright black).
///   It is the only slot conventionally rendered as a neutral mid-grey in
///   both light and dark themes. A terminal theme that maps slot 8 very
///   close to its background will show a faint selection bar; that is a
///   property of the user's theme, and the alternative — a saturated
///   color slot — is worse everywhere else.
///
/// Known gap: the chart dot-grid (`ui::graph::render_grid`) still derives
/// its color by interpolating a fixed grey toward `bg`. With `bg: Reset`
/// the real background is unknown, so it assumes black and renders dark
/// dots — invisible on dark terminals, too heavy on light ones. Fixing it
/// properly means giving `Theme` its own grid slot rather than inferring
/// one from the background.
pub const fn terminal() -> Theme {
    Theme {
        name: "terminal",
        brand: Color::Cyan,
        active_tab: Color::Yellow,
        inactive_tab: Color::DarkGray,
        border: Color::DarkGray,
        separator: Color::DarkGray,
        // Reset = the terminal's configured foreground, exactly.
        text_primary: Color::Reset,
        text_secondary: Color::Gray,
        text_muted: Color::DarkGray,
        text_inverse: Color::Black,
        status_good: Color::Green,
        status_warn: Color::Yellow,
        status_error: Color::Red,
        status_info: Color::Cyan,
        rx_rate: Color::Green,
        tx_rate: Color::Magenta,
        key_hint: Color::Yellow,
        selection_bg: Color::Indexed(8),
        highlight_bg: Color::Indexed(8),
        // The reason this theme exists: never paint over the terminal.
        bg: Color::Reset,
        warn_bg: Color::Reset,
        err_bg: Color::Reset,
    }
}

pub const fn dark() -> Theme {
    // Preserve the original syswatch palette exactly, mapped to the
    // new semantic slots. Brand/active_tab pick up the original cyan;
    // status_info shares brand (as it does in netwatch's dark).
    Theme {
        name: "dark",
        brand: Color::Rgb(0x5f, 0xdc, 0xff),
        active_tab: Color::Rgb(0x5f, 0xdc, 0xff),
        inactive_tab: Color::Rgb(0xc5, 0xd1, 0xd6),
        border: Color::Rgb(0x44, 0x56, 0x60),
        separator: Color::Rgb(0x44, 0x56, 0x60),
        text_primary: Color::Rgb(0xc5, 0xd1, 0xd6),
        text_secondary: Color::Rgb(0xa0, 0xb0, 0xb6),
        text_muted: Color::Rgb(0x6b, 0x80, 0x88),
        text_inverse: Color::Rgb(0x0c, 0x14, 0x18),
        status_good: Color::Rgb(0x5c, 0xd9, 0x89),
        status_warn: Color::Rgb(0xf0, 0xc0, 0x60),
        status_error: Color::Rgb(0xff, 0x78, 0x78),
        status_info: Color::Rgb(0x5f, 0xdc, 0xff),
        rx_rate: Color::Rgb(0x5c, 0xd9, 0x89),
        tx_rate: Color::Rgb(0xd9, 0x7a, 0xff),
        key_hint: Color::Rgb(0x5f, 0xdc, 0xff),
        selection_bg: Color::Rgb(0x1a, 0x33, 0x40),
        highlight_bg: Color::Rgb(0x2a, 0x4a, 0x5a),
        bg: Color::Rgb(0x0c, 0x14, 0x18),
        warn_bg: Color::Rgb(0x3a, 0x2c, 0x14),
        err_bg: Color::Rgb(0x3a, 0x1c, 0x1c),
    }
}

pub const fn light() -> Theme {
    // Contrast budget against the near-white bg (#f5f5f2):
    //   text_primary   #1e1e1e  ≈ 14.5:1  AAA
    //   text_secondary #404040  ≈ 9.3:1   AAA
    //   text_muted     #5a5a5a  ≈ 5.7:1   AA  (was #8c8c8c, ~3.1:1, failed)
    //   inactive_tab   #5a5a64  ≈ 5.6:1   AA  (was #8c8c8c, illegible on bg)
    //   border         #a0a0a0  ≈ 2.5:1   chrome-only, intentionally faint
    Theme {
        name: "light",
        brand: Color::Rgb(0, 100, 160),
        active_tab: Color::Rgb(180, 100, 0),
        inactive_tab: Color::Rgb(90, 90, 100),
        border: Color::Rgb(160, 160, 160),
        separator: Color::Rgb(160, 160, 160),
        text_primary: Color::Rgb(30, 30, 30),
        text_secondary: Color::Rgb(64, 64, 64),
        text_muted: Color::Rgb(90, 90, 90),
        text_inverse: Color::Rgb(255, 255, 255),
        status_good: Color::Rgb(0, 120, 50),
        status_warn: Color::Rgb(170, 110, 0),
        status_error: Color::Rgb(180, 30, 30),
        status_info: Color::Rgb(0, 100, 160),
        rx_rate: Color::Rgb(0, 120, 50),
        tx_rate: Color::Rgb(0, 80, 160),
        key_hint: Color::Rgb(180, 100, 0),
        selection_bg: Color::Rgb(220, 230, 240),
        highlight_bg: Color::Rgb(200, 215, 230),
        bg: Color::Rgb(0xf5, 0xf5, 0xf2),
        warn_bg: Color::Rgb(0xfa, 0xf0, 0xd6),
        err_bg: Color::Rgb(0xfa, 0xdc, 0xdc),
    }
}

/// Apple Terminal.app "Ocean" profile (deep blue bg).
pub const fn ocean() -> Theme {
    let bright_white = Color::Rgb(0xFF, 0xFF, 0xFF);
    let white = Color::Rgb(0xCB, 0xCC, 0xCD);
    let muted_readable = Color::Rgb(0xB5, 0xB6, 0xB7);
    let bright_red = Color::Rgb(0xFC, 0x39, 0x1F);
    let bright_green = Color::Rgb(0x31, 0xE7, 0x22);
    let bright_yellow = Color::Rgb(0xEA, 0xEC, 0x23);
    let bright_cyan = Color::Rgb(0x14, 0xF0, 0xF0);
    Theme {
        name: "ocean",
        brand: bright_cyan,
        active_tab: bright_white,
        inactive_tab: white,
        border: muted_readable,
        separator: muted_readable,
        text_primary: bright_white,
        text_secondary: white,
        text_muted: muted_readable,
        text_inverse: Color::Rgb(0, 0, 0),
        status_good: bright_green,
        status_warn: bright_yellow,
        status_error: bright_red,
        status_info: bright_cyan,
        rx_rate: bright_green,
        tx_rate: bright_cyan,
        key_hint: bright_yellow,
        selection_bg: Color::Rgb(0x21, 0x6D, 0xFF),
        highlight_bg: Color::Rgb(0x3A, 0x6B, 0xE8),
        bg: Color::Rgb(0x22, 0x4F, 0xBC),
        warn_bg: Color::Rgb(0x3A, 0x4A, 0x12),
        err_bg: Color::Rgb(0x4A, 0x1F, 0x1F),
    }
}

pub const fn solarized() -> Theme {
    let base03 = Color::Rgb(0, 43, 54);
    let base02 = Color::Rgb(7, 54, 66);
    let base01 = Color::Rgb(88, 110, 117);
    let base0 = Color::Rgb(131, 148, 150);
    let base1 = Color::Rgb(147, 161, 161);
    let yellow = Color::Rgb(181, 137, 0);
    let orange = Color::Rgb(203, 75, 22);
    let red = Color::Rgb(220, 50, 47);
    let green = Color::Rgb(133, 153, 0);
    let cyan = Color::Rgb(42, 161, 152);
    let blue = Color::Rgb(38, 139, 210);
    let violet = Color::Rgb(108, 113, 196);
    Theme {
        name: "solarized",
        brand: cyan,
        active_tab: yellow,
        inactive_tab: base01,
        border: base01,
        separator: base01,
        text_primary: base0,
        text_secondary: base1,
        text_muted: base01,
        text_inverse: base03,
        status_good: green,
        status_warn: yellow,
        status_error: red,
        status_info: cyan,
        rx_rate: green,
        tx_rate: blue,
        key_hint: orange,
        selection_bg: base02,
        highlight_bg: violet,
        bg: base03,
        warn_bg: Color::Rgb(40, 36, 14),
        err_bg: Color::Rgb(50, 18, 18),
    }
}

pub const fn dracula() -> Theme {
    let bg = Color::Rgb(40, 42, 54);
    let fg = Color::Rgb(248, 248, 242);
    let comment = Color::Rgb(98, 114, 164);
    let cyan = Color::Rgb(139, 233, 253);
    let green = Color::Rgb(80, 250, 123);
    let orange = Color::Rgb(255, 184, 108);
    let pink = Color::Rgb(255, 121, 198);
    let purple = Color::Rgb(189, 147, 249);
    let red = Color::Rgb(255, 85, 85);
    let yellow = Color::Rgb(241, 250, 140);
    Theme {
        name: "dracula",
        brand: purple,
        active_tab: pink,
        inactive_tab: comment,
        border: comment,
        separator: comment,
        text_primary: fg,
        text_secondary: Color::Rgb(200, 200, 210),
        text_muted: comment,
        text_inverse: bg,
        status_good: green,
        status_warn: yellow,
        status_error: red,
        status_info: cyan,
        rx_rate: green,
        tx_rate: cyan,
        key_hint: orange,
        selection_bg: Color::Rgb(68, 71, 90),
        highlight_bg: Color::Rgb(98, 114, 164),
        bg,
        warn_bg: Color::Rgb(60, 50, 30),
        err_bg: Color::Rgb(70, 30, 30),
    }
}

pub const fn nord() -> Theme {
    let polar0 = Color::Rgb(46, 52, 64);
    let polar2 = Color::Rgb(67, 76, 94);
    let snow0 = Color::Rgb(216, 222, 233);
    let snow1 = Color::Rgb(229, 233, 240);
    let frost0 = Color::Rgb(143, 188, 187);
    let frost1 = Color::Rgb(136, 192, 208);
    let frost2 = Color::Rgb(129, 161, 193);
    let frost3 = Color::Rgb(94, 129, 172);
    let aurora_red = Color::Rgb(191, 97, 106);
    let aurora_orange = Color::Rgb(208, 135, 112);
    let aurora_yellow = Color::Rgb(235, 203, 139);
    let aurora_green = Color::Rgb(163, 190, 140);
    Theme {
        name: "nord",
        brand: frost1,
        active_tab: frost0,
        inactive_tab: frost3,
        border: polar2,
        separator: polar2,
        text_primary: snow0,
        text_secondary: snow1,
        text_muted: Color::Rgb(76, 86, 106),
        text_inverse: polar0,
        status_good: aurora_green,
        status_warn: aurora_yellow,
        status_error: aurora_red,
        status_info: frost1,
        rx_rate: aurora_green,
        tx_rate: frost2,
        key_hint: aurora_orange,
        selection_bg: Color::Rgb(59, 66, 82),
        highlight_bg: Color::Rgb(76, 86, 106),
        bg: polar0,
        warn_bg: Color::Rgb(60, 56, 40),
        err_bg: Color::Rgb(60, 36, 38),
    }
}

pub fn by_name(name: &str) -> Theme {
    match name.to_lowercase().as_str() {
        "light" => light(),
        "ocean" => ocean(),
        "solarized" => solarized(),
        "dracula" => dracula(),
        "nord" => nord(),
        // "system" and "ansi" are what users coming from other TUIs tend
        // to reach for; accept both rather than silently falling back to
        // dark and looking like the feature is missing.
        "terminal" | "system" | "ansi" => terminal(),
        _ => dark(),
    }
}

static ACTIVE: RwLock<Theme> = RwLock::new(dark());

pub fn active() -> Theme {
    *ACTIVE.read().expect("theme lock poisoned")
}

pub fn set(theme: Theme) {
    *ACTIVE.write().expect("theme lock poisoned") = theme;
}

pub fn set_by_name(name: &str) {
    set(by_name(name));
}

pub fn name() -> &'static str {
    active().name
}

pub fn cycle() -> &'static str {
    let cur = name();
    let i = THEME_NAMES.iter().position(|n| *n == cur).unwrap_or(0);
    let next = THEME_NAMES[(i + 1) % THEME_NAMES.len()];
    set_by_name(next);
    next
}

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

    /// `ACTIVE` is process-global, and the test harness runs tests on
    /// parallel threads — so the tests that *mutate* the active theme have to
    /// take turns. Without this they interleave and fail intermittently
    /// (roughly 1 run in 20), each one reading a theme the other just set.
    ///
    /// Tests that only read (`by_name`, `dark()`, `terminal()`) touch no
    /// global state and need no lock.
    static ACTIVE_THEME_TESTS: std::sync::Mutex<()> = std::sync::Mutex::new(());

    /// Serialize a test that sets the global theme, and hand back a clean
    /// starting state. The guard holds the lock until the test ends.
    fn exclusive_theme() -> std::sync::MutexGuard<'static, ()> {
        // A panic in one such test poisons the mutex; that shouldn't cascade
        // into failures in the others, so recover the guard either way.
        let guard = ACTIVE_THEME_TESTS
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        restore_dark();
        guard
    }

    fn restore_dark() {
        set_by_name("dark");
    }

    #[test]
    fn all_builtin_themes_load() {
        for name in THEME_NAMES {
            let t = by_name(name);
            assert_eq!(t.name, *name);
        }
    }

    #[test]
    fn unknown_falls_back_to_dark() {
        assert_eq!(by_name("nonsense").name, "dark");
        assert_eq!(by_name("").name, "dark");
    }

    #[test]
    fn cycle_visits_every_theme() {
        let _guard = exclusive_theme();
        let mut seen = Vec::new();
        for _ in 0..THEME_NAMES.len() {
            seen.push(cycle());
        }
        assert_eq!(name(), "dark");
        let mut sorted = seen.clone();
        sorted.sort();
        let mut expected: Vec<&str> = THEME_NAMES.to_vec();
        expected.sort();
        assert_eq!(sorted, expected);
        restore_dark();
    }

    #[test]
    fn set_by_name_changes_active() {
        let _guard = exclusive_theme();
        set_by_name("dracula");
        assert_eq!(name(), "dracula");
        restore_dark();
    }

    #[test]
    fn terminal_theme_pins_no_rgb() {
        // The entire contract of this theme is that every slot resolves
        // through the terminal's own palette. Debug-formatting the whole
        // struct checks every field at once, so a slot added later can't
        // quietly acquire a fixed color without failing here.
        let rendered = format!("{:?}", terminal());
        assert!(
            !rendered.contains("Rgb"),
            "terminal theme must not pin RGB values: {rendered}"
        );
    }

    #[test]
    fn terminal_theme_never_paints_backgrounds() {
        let t = terminal();
        assert_eq!(t.bg, Color::Reset);
        assert_eq!(t.warn_bg, Color::Reset);
        assert_eq!(t.err_bg, Color::Reset);
        assert_eq!(t.text_primary, Color::Reset);
    }

    #[test]
    fn terminal_theme_accepts_common_aliases() {
        for alias in ["terminal", "system", "ansi", "TERMINAL", "System"] {
            assert_eq!(by_name(alias).name, "terminal", "alias {alias} failed");
        }
    }

    #[test]
    fn dark_preserves_legacy_colors() {
        // Sanity: the original syswatch palette is preserved under the new
        // semantic field names. Guards against accidental color drift.
        let t = dark();
        assert_eq!(t.bg, Color::Rgb(0x0c, 0x14, 0x18));
        assert_eq!(t.text_primary, Color::Rgb(0xc5, 0xd1, 0xd6));
        assert_eq!(t.brand, Color::Rgb(0x5f, 0xdc, 0xff));
        assert_eq!(t.status_good, Color::Rgb(0x5c, 0xd9, 0x89));
        assert_eq!(t.status_warn, Color::Rgb(0xf0, 0xc0, 0x60));
        assert_eq!(t.status_error, Color::Rgb(0xff, 0x78, 0x78));
    }
}