cordy 0.2.0

A cross-platform TUI coding agent in Rust — workspace tabs, PTY terminals, direct-key panels, hot-swap any model/provider mid-conversation, MCP, skills, sub-agents and background jobs.
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
//! Color themes.
//!
//! A theme is a small set of semantic slots (surfaces, text, accents, state colors) rather than a
//! per-widget palette, so a new theme is one row in [`BUILTIN`] and every panel picks it up. Custom
//! themes are JSON files in `~/.cordy/themes/` (or `<project>/.cordy/themes/`) and hot-reload on
//! save — see [`ThemeRegistry::load`].

use ratatui::style::Color;
use std::path::Path;

/// The palette used to render the UI.
///
/// The first block is the legacy role set the transcript renders with; the second is the surface /
/// state set the chrome (header, tab strip, panels, meters) is built from.
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct Theme {
    // --- conversation roles ---
    pub user: Color,
    pub assistant: Color,
    pub tool: Color,
    pub system: Color,
    pub dim: Color,
    pub accent: Color,
    /// Subtle border/rule color.
    pub border: Color,
    /// Subtle raised surface (user message + input backgrounds).
    pub surface: Color,
    /// The app background, filled behind everything.
    pub base: Color,

    // --- chrome ---
    /// A second surface layer above `surface` — header bar, tab strip, active panel headers.
    pub elevated: Color,
    /// Secondary accent; pairs with `accent` for gradients, meters and the brand mark.
    pub accent2: Color,
    /// State colors for badges, diffs, meters and the git panel.
    pub success: Color,
    pub warning: Color,
    pub danger: Color,
    pub info: Color,
    /// Foreground to use on top of an `accent`-filled chip.
    pub on_accent: Color,
    /// Whether this is a dark theme (drives a few contrast decisions).
    pub dark: bool,
}

impl Default for Theme {
    fn default() -> Self {
        theme_by_name("cordy")
    }
}

impl Theme {
    /// Build a theme from the raw slots of a [`BUILTIN`] row (or a custom JSON theme).
    fn from_slots(s: &Slots) -> Theme {
        let text = s.text;
        Theme {
            user: text,
            // The assistant reads as the "quieter" voice: the same text color pulled a step
            // toward the background so user turns stay the visual anchor.
            assistant: blend(text, s.base, 0.16),
            tool: s.tool,
            system: s.muted,
            dim: s.muted,
            accent: s.accent,
            border: s.border,
            surface: s.surface,
            base: s.base,
            elevated: s.elevated,
            accent2: s.accent2,
            success: s.success,
            warning: s.warning,
            danger: s.danger,
            info: s.accent2,
            on_accent: if luma(s.accent) > 0.55 {
                blend(s.base, Color::Rgb(0, 0, 0), 0.35)
            } else {
                Color::Rgb(255, 255, 255)
            },
            dark: s.dark,
        }
    }

    /// Override individual role colors from `#rrggbb` strings (used for `[colors]` in config).
    pub fn with_overrides(mut self, c: &ColorOverrides) -> Self {
        let set = |slot: &mut Color, hex: &Option<String>| {
            if let Some(h) = hex
                && let Some(col) = parse_hex(h)
            {
                *slot = col;
            }
        };
        set(&mut self.user, &c.user);
        set(&mut self.assistant, &c.assistant);
        set(&mut self.tool, &c.tool);
        set(&mut self.system, &c.system);
        set(&mut self.dim, &c.dim);
        set(&mut self.accent, &c.accent);
        set(&mut self.border, &c.border);
        set(&mut self.surface, &c.surface);
        set(&mut self.base, &c.base);
        set(&mut self.elevated, &c.elevated);
        set(&mut self.accent2, &c.accent2);
        set(&mut self.success, &c.success);
        set(&mut self.warning, &c.warning);
        set(&mut self.danger, &c.danger);
        self
    }

    /// `n` steps of the accent gradient, for meters and the brand mark.
    pub fn ramp(&self, n: usize) -> Vec<Color> {
        (0..n.max(1))
            .map(|i| {
                let t = if n <= 1 {
                    0.0
                } else {
                    i as f32 / (n - 1) as f32
                };
                blend(self.accent, self.accent2, t)
            })
            .collect()
    }

    /// A context meter color: accent while there's room, warning past 75%, danger past 90%.
    pub fn gauge(&self, pct: u64) -> Color {
        match pct {
            0..=74 => self.accent,
            75..=89 => self.warning,
            _ => self.danger,
        }
    }
}

/// Per-role hex color overrides from `[colors]` in config; any unset field keeps the theme's.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct ColorOverrides {
    pub user: Option<String>,
    pub assistant: Option<String>,
    pub tool: Option<String>,
    pub system: Option<String>,
    pub dim: Option<String>,
    pub accent: Option<String>,
    pub border: Option<String>,
    pub surface: Option<String>,
    pub base: Option<String>,
    pub elevated: Option<String>,
    pub accent2: Option<String>,
    pub success: Option<String>,
    pub warning: Option<String>,
    pub danger: Option<String>,
}

/// The raw color slots a theme is defined by.
struct Slots {
    base: Color,
    surface: Color,
    elevated: Color,
    border: Color,
    text: Color,
    muted: Color,
    accent: Color,
    accent2: Color,
    success: Color,
    warning: Color,
    danger: Color,
    tool: Color,
    dark: bool,
}

/// One builtin theme: `(name, dark, base, surface, elevated, border, text, muted, accent, accent2,
/// success, warning, danger, tool)`.
type Row = (
    &'static str,
    bool,
    &'static str,
    &'static str,
    &'static str,
    &'static str,
    &'static str,
    &'static str,
    &'static str,
    &'static str,
    &'static str,
    &'static str,
    &'static str,
    &'static str,
);

/// Every builtin theme. `cordy` is the signature palette and the default.
#[rustfmt::skip]
const BUILTIN: &[Row] = &[
    // name                base      surface   elevated  border    text      muted     accent    accent2   success   warning   danger    tool
    ("cordy",        true,  "#0e0f16","#171926","#1e2133","#2b2f45","#e6e8f2","#767c99","#7c6cff","#3fd8c7","#4ade80","#fbbf24","#f87171","#a78bfa"),
    ("cordy-light",  false, "#fbfbfe","#f1f2f8","#e7e9f3","#d6d9e6","#1b1d29","#6c7288","#5b46e8","#0f9e8f","#15803d","#b45309","#dc2626","#7c3aed"),
    ("ember",        true,  "#120e0c","#1d1715","#26201d","#3a302b","#f0e6df","#9a8a80","#ff7a45","#ffc55c","#7cc47f","#ffb454","#ff5f56","#ffa657"),
    ("abyss",        true,  "#080b10","#101620","#161f2c","#22303f","#dce6f0","#6c8199","#38bdf8","#22d3ee","#34d399","#fbbf24","#fb7185","#60a5fa"),
    ("dark",         true,  "#14161c","#20232b","#272b35","#3a404e","#e1e6f0","#697282","#6e9bf5","#63d2c0","#68d391","#e8b339","#f0736a","#b4becd"),
    ("light",        false, "#fafafc","#eef0f4","#e4e7ed","#d2d6de","#1e222a","#828a96","#1e64d2","#0e8f86","#137a3a","#a2600a","#c62b2b","#5a606c"),
    ("mono",         true,  "#0c0c0c","#1c1c1c","#242424","#3a3a3a","#e8e8e8","#7a7a7a","#e8e8e8","#a8a8a8","#c8c8c8","#c8c8c8","#e8e8e8","#a8a8a8"),
    ("mono-light",   false, "#fcfcfc","#f0f0f0","#e6e6e6","#cccccc","#141414","#6a6a6a","#141414","#4a4a4a","#3a3a3a","#3a3a3a","#141414","#4a4a4a"),
    ("tokyonight",   true,  "#1a1b26","#24283b","#2f334d","#2f337d","#c0caf5","#565f89","#7aa2f7","#7dcfff","#9ece6a","#e0af68","#f7768e","#e0af68"),
    ("tokyonight-storm", true, "#24283b","#2f334d","#343a52","#3b4261","#c0caf5","#565f89","#7aa2f7","#7dcfff","#9ece6a","#e0af68","#f7768e","#bb9af7"),
    ("tokyonight-day",   false,"#e1e2e7","#d0d5e3","#c4c8da","#a8aecb","#3760bf","#8990b3","#2e7de9","#007197","#587539","#8c6c3e","#f52a65","#b15c00"),
    ("catppuccin-mocha", true, "#1e1e2e","#313244","#45475a","#585b70","#cdd6f4","#6c7086","#89b4fa","#94e2d5","#a6e3a1","#f9e2af","#f38ba8","#fab387"),
    ("catppuccin-macchiato", true,"#24273a","#363a4f","#494d64","#5b6078","#cad3f5","#6e738d","#8aadf4","#8bd5ca","#a6da95","#eed49f","#ed8796","#f5a97f"),
    ("catppuccin-frappe",true, "#303446","#414559","#51576d","#626880","#c6d0f5","#737994","#8caaee","#81c8be","#a6d189","#e5c890","#e78284","#ef9f76"),
    ("catppuccin-latte", false,"#eff1f5","#e6e9ef","#dce0e8","#bcc0cc","#4c4f69","#8c8fa1","#1e66f5","#179299","#40a02b","#df8e1d","#d20f39","#fe640b"),
    ("dracula",      true,  "#282a36","#343746","#414458","#4d5066","#f8f8f2","#6272a4","#bd93f9","#8be9fd","#50fa7b","#f1fa8c","#ff5555","#ffb86c"),
    ("gruvbox-dark", true,  "#282828","#32302f","#3c3836","#504945","#ebdbb2","#928374","#83a598","#8ec07c","#b8bb26","#fabd2f","#fb4934","#d3869b"),
    ("gruvbox-light",false, "#fbf1c7","#f2e5bc","#ebdbb2","#d5c4a1","#3c3836","#7c6f64","#076678","#427b58","#79740e","#b57614","#9d0006","#8f3f71"),
    ("nord",         true,  "#2e3440","#3b4252","#434c5e","#4c566a","#eceff4","#616e88","#88c0d0","#8fbcbb","#a3be8c","#ebcb8b","#bf616a","#d08770"),
    ("rose-pine",    true,  "#191724","#1f1d2e","#26233a","#403d52","#e0def4","#6e6a86","#c4a7e7","#9ccfd8","#31748f","#f6c177","#eb6f92","#f6c177"),
    ("rose-pine-moon",true, "#232136","#2a273f","#393552","#44415a","#e0def4","#6e6a86","#c4a7e7","#9ccfd8","#3e8fb0","#f6c177","#eb6f92","#ea9a97"),
    ("rose-pine-dawn",false,"#faf4ed","#fffaf3","#f2e9e1","#dfdad9","#575279","#9893a5","#907aa9","#56949f","#286983","#ea9d34","#b4637a","#d7827e"),
    ("everforest",   true,  "#2d353b","#343f44","#3d484d","#4f585e","#d3c6aa","#859289","#a7c080","#83c092","#a7c080","#dbbc7f","#e67e80","#d699b6"),
    ("everforest-light",false,"#fdf6e3","#f4f0d9","#efebd4","#ddd8be","#5c6a72","#939f91","#8da101","#35a77c","#8da101","#dfa000","#f85552","#df69ba"),
    ("kanagawa",     true,  "#1f1f28","#2a2a37","#363646","#54546d","#dcd7ba","#727169","#7e9cd8","#7aa89f","#98bb6c","#e6c384","#e82424","#ffa066"),
    ("kanagawa-dragon",true,"#181616","#282727","#393836","#54546d","#c5c9c5","#737c73","#8ba4b0","#8ea4a2","#87a987","#c4b28a","#c4746e","#b6927b"),
    ("solarized-dark",true, "#002b36","#073642","#0a4451","#0f5666","#eee8d5","#657b83","#268bd2","#2aa198","#859900","#b58900","#dc322f","#cb4b16"),
    ("solarized-light",false,"#fdf6e3","#eee8d5","#e4ddc8","#d3cbb7","#073642","#93a1a1","#268bd2","#2aa198","#859900","#b58900","#dc322f","#cb4b16"),
    ("monokai",      true,  "#272822","#31322c","#3b3c35","#4a4b44","#f8f8f2","#75715e","#66d9ef","#a6e22e","#a6e22e","#e6db74","#f92672","#fd971f"),
    ("one-dark",     true,  "#282c34","#31363f","#3a3f4b","#4b5263","#abb2bf","#5c6370","#61afef","#56b6c2","#98c379","#e5c07b","#e06c75","#c678dd"),
    ("one-light",    false, "#fafafa","#eaeaeb","#e0e0e1","#c8c8c9","#383a42","#a0a1a7","#4078f2","#0184bc","#50a14f","#c18401","#e45649","#a626a4"),
    ("ayu-dark",     true,  "#0b0e14","#131721","#1b1f2b","#2d3444","#bfbdb6","#565b66","#e6b450","#59c2ff","#7fd962","#ffb454","#f26d78","#ffb454"),
    ("ayu-mirage",   true,  "#1f2430","#242936","#2b3140","#3d4657","#cccac2","#707a8c","#ffcc66","#73d0ff","#87d96c","#ffd173","#f28779","#ffad66"),
    ("ayu-light",    false, "#fcfcfc","#f3f4f5","#eaebec","#d4d6d8","#5c6166","#8a9199","#ff9940","#399ee6","#6cbf43","#f2ae49","#f07171","#fa8d3e"),
    ("material",     true,  "#263238","#2e3c43","#37474f","#425b67","#eeffff","#546e7a","#82aaff","#89ddff","#c3e88d","#ffcb6b","#f07178","#f78c6c"),
    ("night-owl",    true,  "#011627","#0b2942","#0e3049","#1d3b53","#d6deeb","#637777","#82aaff","#7fdbca","#addb67","#ecc48d","#ef5350","#c792ea"),
    ("synthwave",    true,  "#241b2f","#2a2139","#34294f","#495495","#f8f8f2","#848bbd","#ff7edb","#36f9f6","#72f1b8","#fede5d","#fe4450","#ff8b39"),
    ("nightfox",     true,  "#192330","#212e3f","#29394f","#39506d","#cdcecf","#71839b","#719cd6","#63cdcf","#81b29a","#dbc074","#c94f6d","#f4a261"),
    ("oxocarbon",    true,  "#161616","#262626","#303030","#393939","#f2f4f8","#6f6f6f","#78a9ff","#3ddbd9","#42be65","#ff7eb6","#ee5396","#be95ff"),
    ("zenburn",      true,  "#3f3f3f","#4a4a4a","#545454","#6f6f6f","#dcdccc","#8f8f8f","#8cd0d3","#93e0e3","#7f9f7f","#f0dfaf","#dca3a3","#dfaf8f"),
    ("github-dark",  true,  "#0d1117","#161b22","#1c2128","#30363d","#c9d1d9","#8b949e","#58a6ff","#39c5cf","#3fb950","#d29922","#f85149","#a5d6ff"),
    ("github-light", false, "#ffffff","#f6f8fa","#eaeef2","#d0d7de","#24292f","#6e7781","#0969da","#1b7c83","#1a7f37","#9a6700","#cf222e","#0550ae"),
];

/// Older names kept working after the palette was split into variants.
const ALIASES: &[(&str, &str)] = &[
    ("catppuccin", "catppuccin-mocha"),
    ("rosepine", "rose-pine"),
    ("gruvbox", "gruvbox-dark"),
    ("onedark", "one-dark"),
    ("ayu", "ayu-dark"),
    ("nightowl", "night-owl"),
];

fn row_to_theme(r: &Row) -> Theme {
    let hex = |s: &str| parse_hex(s).unwrap_or(Color::Rgb(255, 0, 255));
    Theme::from_slots(&Slots {
        dark: r.1,
        base: hex(r.2),
        surface: hex(r.3),
        elevated: hex(r.4),
        border: hex(r.5),
        text: hex(r.6),
        muted: hex(r.7),
        accent: hex(r.8),
        accent2: hex(r.9),
        success: hex(r.10),
        warning: hex(r.11),
        danger: hex(r.12),
        tool: hex(r.13),
    })
}

/// Resolve a builtin theme by name (aliases included); unknown names fall back to `cordy`.
pub fn theme_by_name(name: &str) -> Theme {
    let lower = name.to_ascii_lowercase();
    let canonical = ALIASES
        .iter()
        .find(|(from, _)| *from == lower)
        .map(|(_, to)| *to)
        .unwrap_or(lower.as_str());
    let row = BUILTIN
        .iter()
        .find(|r| r.0 == canonical)
        .unwrap_or(&BUILTIN[0]);
    row_to_theme(row)
}

/// Every theme available to the picker: the builtins plus any custom JSON themes found on disk.
///
/// Custom themes shadow a builtin of the same name, so a user can retune `cordy` without renaming
/// it. [`signature`] changes whenever a theme file is added, removed or saved, which is what drives
/// hot-reload.
#[derive(Clone, Default)]
pub struct ThemeRegistry {
    entries: Vec<(String, Theme)>,
    sig: u64,
}

impl ThemeRegistry {
    /// Builtins only — used when no theme directories exist.
    pub fn builtin() -> Self {
        ThemeRegistry {
            entries: BUILTIN
                .iter()
                .map(|r| (r.0.to_string(), row_to_theme(r)))
                .collect(),
            sig: 0,
        }
    }

    /// Builtins plus `*.json` themes from each directory in `dirs` (later dirs win on a name clash,
    /// so a project theme overrides a user one).
    pub fn load(dirs: &[std::path::PathBuf]) -> Self {
        let mut reg = ThemeRegistry::builtin();
        for dir in dirs {
            for (name, theme) in load_dir(dir) {
                match reg.entries.iter_mut().find(|(n, _)| *n == name) {
                    Some(slot) => slot.1 = theme,
                    None => reg.entries.push((name, theme)),
                }
            }
        }
        reg.sig = signature(dirs);
        reg
    }

    /// Re-read `dirs` when their contents changed; returns whether anything was reloaded.
    pub fn reload_if_changed(&mut self, dirs: &[std::path::PathBuf]) -> bool {
        let sig = signature(dirs);
        if sig == self.sig {
            return false;
        }
        *self = ThemeRegistry::load(dirs);
        true
    }

    pub fn names(&self) -> Vec<String> {
        self.entries.iter().map(|(n, _)| n.clone()).collect()
    }

    pub fn len(&self) -> usize {
        self.entries.len()
    }

    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// The theme at `i` in picker order, clamped to the last row.
    pub fn at(&self, i: usize) -> Theme {
        self.entries
            .get(i.min(self.entries.len().saturating_sub(1)))
            .map(|(_, t)| *t)
            .unwrap_or_default()
    }

    pub fn name_at(&self, i: usize) -> &str {
        self.entries
            .get(i.min(self.entries.len().saturating_sub(1)))
            .map(|(n, _)| n.as_str())
            .unwrap_or("cordy")
    }

    /// Picker index of `name`, resolving aliases.
    pub fn index_of(&self, name: &str) -> Option<usize> {
        let lower = name.to_ascii_lowercase();
        let canonical = ALIASES
            .iter()
            .find(|(from, _)| *from == lower)
            .map(|(_, to)| *to)
            .unwrap_or(lower.as_str());
        self.entries.iter().position(|(n, _)| n == canonical)
    }

    pub fn get(&self, name: &str) -> Option<Theme> {
        self.index_of(name).map(|i| self.at(i))
    }
}

/// Read every `*.json` theme in `dir`. Malformed files are skipped rather than failing the load.
fn load_dir(dir: &Path) -> Vec<(String, Theme)> {
    let Ok(rd) = std::fs::read_dir(dir) else {
        return Vec::new();
    };
    let mut out = Vec::new();
    for entry in rd.flatten() {
        let path = entry.path();
        if path.extension().and_then(|e| e.to_str()) != Some("json") {
            continue;
        }
        let Ok(text) = std::fs::read_to_string(&path) else {
            continue;
        };
        let Ok(json) = serde_json::from_str::<serde_json::Value>(&text) else {
            continue;
        };
        let stem = path
            .file_stem()
            .and_then(|s| s.to_str())
            .unwrap_or("custom")
            .to_string();
        let name = json
            .get("name")
            .and_then(|v| v.as_str())
            .unwrap_or(&stem)
            .to_ascii_lowercase();
        out.push((name, theme_from_json(&json)));
    }
    out.sort_by(|a, b| a.0.cmp(&b.0));
    out
}

/// Build a theme from a JSON object; any missing slot falls back to the base theme named by
/// `"extends"` (default `cordy`), so a two-line theme file is valid.
fn theme_from_json(json: &serde_json::Value) -> Theme {
    let base_name = json
        .get("extends")
        .and_then(|v| v.as_str())
        .unwrap_or("cordy");
    let mut t = theme_by_name(base_name);
    if let Some(dark) = json.get("dark").and_then(|v| v.as_bool()) {
        t.dark = dark;
    }
    let pick = |key: &str| json.get(key).and_then(|v| v.as_str()).and_then(parse_hex);
    // Slot-level fields first, then the derived role fields so an explicit role always wins.
    if let Some(c) = pick("base") {
        t.base = c;
    }
    if let Some(c) = pick("surface") {
        t.surface = c;
    }
    if let Some(c) = pick("elevated") {
        t.elevated = c;
    }
    if let Some(c) = pick("border") {
        t.border = c;
    }
    if let Some(c) = pick("text") {
        t.user = c;
        t.assistant = blend(c, t.base, 0.16);
    }
    if let Some(c) = pick("muted") {
        t.system = c;
        t.dim = c;
    }
    if let Some(c) = pick("accent") {
        t.accent = c;
        t.on_accent = if luma(c) > 0.55 {
            blend(t.base, Color::Rgb(0, 0, 0), 0.35)
        } else {
            Color::Rgb(255, 255, 255)
        };
    }
    if let Some(c) = pick("accent2") {
        t.accent2 = c;
        t.info = c;
    }
    for (key, slot) in [
        ("success", &mut t.success),
        ("warning", &mut t.warning),
        ("danger", &mut t.danger),
        ("tool", &mut t.tool),
        ("user", &mut t.user),
        ("assistant", &mut t.assistant),
        ("system", &mut t.system),
        ("dim", &mut t.dim),
        ("info", &mut t.info),
    ] {
        if let Some(c) = pick(key) {
            *slot = c;
        }
    }
    t
}

/// A cheap fingerprint of every theme file across `dirs` (name + mtime + size), for hot-reload.
fn signature(dirs: &[std::path::PathBuf]) -> u64 {
    let mut h: u64 = 0xcbf2_9ce4_8422_2325;
    let mut mix = |b: &[u8]| {
        for byte in b {
            h ^= *byte as u64;
            h = h.wrapping_mul(0x1000_0000_01b3);
        }
    };
    for dir in dirs {
        let Ok(rd) = std::fs::read_dir(dir) else {
            continue;
        };
        let mut rows: Vec<(String, u64, u64)> = Vec::new();
        for e in rd.flatten() {
            let p = e.path();
            if p.extension().and_then(|x| x.to_str()) != Some("json") {
                continue;
            }
            let meta = e.metadata().ok();
            let mtime = meta
                .as_ref()
                .and_then(|m| m.modified().ok())
                .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
                .map(|d| d.as_secs())
                .unwrap_or(0);
            let size = meta.as_ref().map(|m| m.len()).unwrap_or(0);
            rows.push((p.to_string_lossy().to_string(), mtime, size));
        }
        rows.sort();
        for (name, mtime, size) in rows {
            mix(name.as_bytes());
            mix(&mtime.to_le_bytes());
            mix(&size.to_le_bytes());
        }
    }
    h
}

/// Parse a `#rrggbb` (or `rrggbb`) hex string into a color.
pub fn parse_hex(s: &str) -> Option<Color> {
    let h = s.trim().trim_start_matches('#');
    if h.len() != 6 {
        return None;
    }
    let r = u8::from_str_radix(&h[0..2], 16).ok()?;
    let g = u8::from_str_radix(&h[2..4], 16).ok()?;
    let b = u8::from_str_radix(&h[4..6], 16).ok()?;
    Some(Color::Rgb(r, g, b))
}

/// Linear mix of two colors; `t = 0` is `a`, `t = 1` is `b`. Non-RGB colors pass through as `a`.
pub fn blend(a: Color, b: Color, t: f32) -> Color {
    let (Color::Rgb(ar, ag, ab), Color::Rgb(br, bg, bb)) = (a, b) else {
        return a;
    };
    let t = t.clamp(0.0, 1.0);
    let mix = |x: u8, y: u8| (x as f32 + (y as f32 - x as f32) * t).round() as u8;
    Color::Rgb(mix(ar, br), mix(ag, bg), mix(ab, bb))
}

/// Perceptual-ish brightness in `0.0..=1.0`; non-RGB colors report mid-gray.
pub fn luma(c: Color) -> f32 {
    match c {
        Color::Rgb(r, g, b) => (0.2126 * r as f32 + 0.7152 * g as f32 + 0.0722 * b as f32) / 255.0,
        _ => 0.5,
    }
}

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

    #[test]
    fn resolves_named_themes_and_aliases() {
        assert_eq!(theme_by_name("mono").user, Color::Rgb(0xe8, 0xe8, 0xe8));
        // unknown falls back to the signature theme
        assert_eq!(theme_by_name("nope").accent, theme_by_name("cordy").accent);
        assert_ne!(theme_by_name("light").user, theme_by_name("mono").user);
        assert_eq!(
            theme_by_name("tokyonight").accent,
            Color::Rgb(122, 162, 247)
        );
        assert_eq!(theme_by_name("nord").accent, Color::Rgb(136, 192, 208));
        // aliases keep older config values working
        assert_eq!(
            theme_by_name("catppuccin").accent,
            theme_by_name("catppuccin-mocha").accent
        );
        assert_eq!(
            theme_by_name("rosepine").base,
            theme_by_name("rose-pine").base
        );
    }

    #[test]
    fn every_builtin_parses_and_names_are_unique() {
        let reg = ThemeRegistry::builtin();
        assert!(reg.len() >= 36, "expected 36+ themes, got {}", reg.len());
        let mut names = reg.names();
        names.sort();
        let before = names.len();
        names.dedup();
        assert_eq!(before, names.len(), "duplicate theme name");
        // A bad hex would render as magenta; nothing should have slipped through.
        for i in 0..reg.len() {
            let t = reg.at(i);
            assert_ne!(t.base, Color::Rgb(255, 0, 255), "{}", reg.name_at(i));
            assert_ne!(t.accent, Color::Rgb(255, 0, 255), "{}", reg.name_at(i));
        }
    }

    #[test]
    fn hex_parsing_and_overrides() {
        assert_eq!(parse_hex("#ff8800"), Some(Color::Rgb(255, 136, 0)));
        assert_eq!(parse_hex("00ff00"), Some(Color::Rgb(0, 255, 0)));
        assert_eq!(parse_hex("bad"), None);
        let over = ColorOverrides {
            accent: Some("#010203".into()),
            ..Default::default()
        };
        let t = theme_by_name("dark").with_overrides(&over);
        assert_eq!(t.accent, Color::Rgb(1, 2, 3));
        assert_eq!(t.user, theme_by_name("dark").user); // untouched
    }

    #[test]
    fn custom_json_theme_extends_a_builtin() {
        let json: serde_json::Value = serde_json::from_str(
            r##"{"name":"mine","extends":"nord","accent":"#ff0000","text":"#ffffff"}"##,
        )
        .unwrap();
        let t = theme_from_json(&json);
        assert_eq!(t.accent, Color::Rgb(255, 0, 0));
        assert_eq!(t.user, Color::Rgb(255, 255, 255));
        assert_eq!(t.base, theme_by_name("nord").base); // inherited
    }

    #[test]
    fn custom_themes_shadow_builtins_and_hot_reload() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("nord.json");
        std::fs::write(&path, r##"{"accent":"#123456"}"##).unwrap();
        let dirs = vec![dir.path().to_path_buf()];
        let mut reg = ThemeRegistry::load(&dirs);
        assert_eq!(
            reg.get("nord").unwrap().accent,
            Color::Rgb(0x12, 0x34, 0x56)
        );
        assert_eq!(reg.len(), ThemeRegistry::builtin().len()); // shadowed, not appended
        // An unchanged directory is not reloaded.
        assert!(!reg.reload_if_changed(&dirs));
        // A new file is picked up.
        std::fs::write(dir.path().join("zzz.json"), r##"{"accent":"#00ff00"}"##).unwrap();
        assert!(reg.reload_if_changed(&dirs));
        assert_eq!(reg.get("zzz").unwrap().accent, Color::Rgb(0, 255, 0));
    }

    #[test]
    fn gauge_escalates_and_ramp_spans_both_accents() {
        let t = theme_by_name("cordy");
        assert_eq!(t.gauge(10), t.accent);
        assert_eq!(t.gauge(80), t.warning);
        assert_eq!(t.gauge(95), t.danger);
        let ramp = t.ramp(5);
        assert_eq!(ramp.first(), Some(&t.accent));
        assert_eq!(ramp.last(), Some(&t.accent2));
    }
}