zagens-cli 0.8.2

Zagens headless CLI + HTTP/SSE runtime sidecar (`zagens`, `zagens-runtime` binaries)
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
//! Panel background presets and theme registry (extensible style switching).

use ratatui::style::Color;

/// Per-theme accent colors for the four transcript roles shown in the center column.
/// Body text, dim hints, checklist marks, etc. remain shared across all themes.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ThemePalette {
    /// "you>" tag and user message body.
    pub user_prompt: Color,
    /// "AI>" tag and assistant reply body.
    pub agent_reply: Color,
    /// "THK>" tag and live/expanded thinking body.
    pub thinking: Color,
    /// "tool " tag and tool-chain body.
    pub tool_call: Color,
}

impl ThemePalette {
    /// Dracula original — used by Classic and as the fallback.
    pub const fn dracula() -> Self {
        Self {
            user_prompt: Color::Rgb(0x8b, 0xe9, 0xfd), // cyan
            agent_reply: Color::Rgb(0x50, 0xfa, 0x7b), // green
            thinking: Color::Rgb(0xf1, 0xfa, 0x8c),    // yellow
            tool_call: Color::Rgb(0xbd, 0x93, 0xf9),   // purple
        }
    }

    /// CoolBlue — GitHub-style blues + amber warmth to counter the cool bg.
    pub const fn cool_blue() -> Self {
        Self {
            user_prompt: Color::Rgb(0x79, 0xc0, 0xff), // GitHub blue
            agent_reply: Color::Rgb(0x7e, 0xe7, 0x87), // GitHub green
            thinking: Color::Rgb(0xe3, 0xb3, 0x41),    // amber — warms up the blue
            tool_call: Color::Rgb(0xd2, 0xa8, 0xff),   // soft lavender
        }
    }

    /// GrayScale — Nord-inspired muted pastels, easy on the eye.
    pub const fn gray_scale() -> Self {
        Self {
            user_prompt: Color::Rgb(0x88, 0xc0, 0xd0), // Nord frost
            agent_reply: Color::Rgb(0xa3, 0xbe, 0x8c), // Nord sage
            thinking: Color::Rgb(0xeb, 0xcb, 0x8b),    // Nord amber
            tool_call: Color::Rgb(0xb4, 0x8e, 0xad),   // Nord mauve
        }
    }

    /// Charcoal — Catppuccin Mocha palette, warm mid-tones.
    pub const fn charcoal() -> Self {
        Self {
            user_prompt: Color::Rgb(0x89, 0xb4, 0xfa), // Catppuccin blue
            agent_reply: Color::Rgb(0xa6, 0xe3, 0xa1), // Catppuccin green
            thinking: Color::Rgb(0xf9, 0xe2, 0xaf),    // Catppuccin yellow
            tool_call: Color::Rgb(0xcb, 0xa6, 0xf7),   // Catppuccin mauve
        }
    }

    /// DraculaTint — purple-gray bg calls for pink + orange twist on the classic.
    pub const fn dracula_tint() -> Self {
        Self {
            user_prompt: Color::Rgb(0x8b, 0xe9, 0xfd), // keep cyan — complements purple
            agent_reply: Color::Rgb(0x50, 0xfa, 0x7b), // keep green
            thinking: Color::Rgb(0xff, 0xb8, 0x6c), // Dracula orange — more vivid than yellow on purple
            tool_call: Color::Rgb(0xff, 0x79, 0xc6), // Dracula pink — suits purple bg
        }
    }

    /// HighContrast — maximum punch, one shade brighter than Dracula on each hue.
    pub const fn high_contrast() -> Self {
        Self {
            user_prompt: Color::Rgb(0x00, 0xd7, 0xff), // bright cyan
            agent_reply: Color::Rgb(0x00, 0xff, 0x87), // bright mint
            thinking: Color::Rgb(0xff, 0xff, 0x5f),    // bright yellow
            tool_call: Color::Rgb(0xaf, 0x87, 0xff),   // bright violet
        }
    }
}

/// Persisted / selectable TUI color layout. Semantic Dracula tokens stay shared in [`super::palette`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TuiThemeId {
    /// Legacy bordered shell — all panels `#000000`.
    Classic,
    /// Scheme B — cool blue-gray sidebars, pure-black transcript (default).
    CoolBlue,
    /// Scheme A — minimal gray scale, low contrast.
    GrayScale,
    /// Scheme C — layered charcoal.
    Charcoal,
    /// Scheme D — faint purple-gray sidebars.
    DraculaTint,
    /// Scheme E — higher contrast sidebars.
    HighContrast,
}

impl TuiThemeId {
    pub const DEFAULT: Self = Self::CoolBlue;

    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Classic => "classic",
            Self::CoolBlue => "cool-blue",
            Self::GrayScale => "gray-scale",
            Self::Charcoal => "charcoal",
            Self::DraculaTint => "dracula-tint",
            Self::HighContrast => "high-contrast",
        }
    }

    #[must_use]
    pub fn label(self) -> &'static str {
        match self {
            Self::Classic => "Classic (bordered)",
            Self::CoolBlue => "Cool blue-gray",
            Self::GrayScale => "Gray scale",
            Self::Charcoal => "Charcoal",
            Self::DraculaTint => "Dracula tint",
            Self::HighContrast => "High contrast",
        }
    }

    /// All ids in UI / settings order.
    pub const ALL: [Self; 6] = [
        Self::CoolBlue,
        Self::GrayScale,
        Self::Charcoal,
        Self::DraculaTint,
        Self::HighContrast,
        Self::Classic,
    ];

    #[must_use]
    pub fn from_storage(raw: Option<&str>) -> Self {
        match raw.map(str::trim).map(str::to_ascii_lowercase).as_deref() {
            None | Some("") | Some("cool-blue") | Some("cool_blue") | Some("b") => Self::CoolBlue,
            Some("classic") | Some("bordered") => Self::Classic,
            Some("gray-scale") | Some("gray_scale") | Some("a") => Self::GrayScale,
            Some("charcoal") | Some("c") => Self::Charcoal,
            Some("dracula-tint") | Some("dracula_tint") | Some("d") => Self::DraculaTint,
            Some("high-contrast") | Some("high_contrast") | Some("e") => Self::HighContrast,
            _ => Self::DEFAULT,
        }
    }

    #[must_use]
    pub fn cycle(self) -> Self {
        let all = Self::ALL;
        let idx = all.iter().position(|id| *id == self).unwrap_or(0);
        all[(idx + 1) % all.len()]
    }
}

/// Whether panes draw ratatui border lines or rely on background color alone.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ThemeLayout {
    Borderless,
    Bordered,
}

/// Per-panel background colors (24-bit RGB).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PanelSurfaces {
    pub title: Color,
    pub left: Color,
    pub left_active: Color,
    pub transcript: Color,
    pub activity: Color,
    pub composer: Color,
    pub status: Color,
    pub inspector: Color,
    pub inspector_active: Color,
    pub lht: Color,
    pub lht_active: Color,
}

impl PanelSurfaces {
    #[must_use]
    pub fn surface_for(self, panel: super::TuiPanel) -> Color {
        match panel {
            super::TuiPanel::Title => self.title,
            super::TuiPanel::Left => self.left,
            super::TuiPanel::Transcript => self.transcript,
            super::TuiPanel::Activity => self.activity,
            super::TuiPanel::Composer => self.composer,
            super::TuiPanel::Status => self.status,
            super::TuiPanel::Inspector => self.inspector,
            super::TuiPanel::Lht => self.lht,
        }
    }

    #[must_use]
    pub fn active_for(self, panel: super::TuiPanel) -> Color {
        match panel {
            super::TuiPanel::Left => self.left_active,
            super::TuiPanel::Inspector => self.inspector_active,
            super::TuiPanel::Lht => self.lht_active,
            other => tint_color(self.surface_for(other), 0x0a),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TuiTheme {
    pub id: TuiThemeId,
    pub layout: ThemeLayout,
    pub surfaces: PanelSurfaces,
    /// Per-theme accent colors for the four transcript roles.
    pub palette: ThemePalette,
}

impl TuiTheme {
    #[must_use]
    pub fn default_theme() -> Self {
        Self::resolve(TuiThemeId::DEFAULT)
    }

    #[must_use]
    pub fn resolve(id: TuiThemeId) -> Self {
        let (layout, surfaces, palette) = match id {
            TuiThemeId::Classic => (
                ThemeLayout::Bordered,
                classic_surfaces(),
                ThemePalette::dracula(),
            ),
            TuiThemeId::CoolBlue => (
                ThemeLayout::Borderless,
                cool_blue_surfaces(),
                ThemePalette::cool_blue(),
            ),
            TuiThemeId::GrayScale => (
                ThemeLayout::Borderless,
                gray_scale_surfaces(),
                ThemePalette::gray_scale(),
            ),
            TuiThemeId::Charcoal => (
                ThemeLayout::Borderless,
                charcoal_surfaces(),
                ThemePalette::charcoal(),
            ),
            TuiThemeId::DraculaTint => (
                ThemeLayout::Borderless,
                dracula_tint_surfaces(),
                ThemePalette::dracula_tint(),
            ),
            TuiThemeId::HighContrast => (
                ThemeLayout::Borderless,
                high_contrast_surfaces(),
                ThemePalette::high_contrast(),
            ),
        };
        Self {
            id,
            layout,
            surfaces,
            palette,
        }
    }

    #[must_use]
    pub fn pane_chrome_rows(self) -> u16 {
        match self.layout {
            ThemeLayout::Borderless => 1,
            ThemeLayout::Bordered => 2,
        }
    }

    #[must_use]
    pub fn borderless(self) -> bool {
        self.layout == ThemeLayout::Borderless
    }
}

const fn rgb(r: u8, g: u8, b: u8) -> Color {
    Color::Rgb(r, g, b)
}

#[must_use]
pub fn tint_color(base: Color, amount: u8) -> Color {
    match base {
        Color::Rgb(r, g, b) => Color::Rgb(
            r.saturating_add(amount),
            g.saturating_add(amount),
            b.saturating_add(amount),
        ),
        other => other,
    }
}

fn classic_surfaces() -> PanelSurfaces {
    let black = rgb(0, 0, 0);
    let active = rgb(0x22, 0x22, 0x22);
    PanelSurfaces {
        title: black,
        left: black,
        left_active: active,
        transcript: black,
        activity: black,
        composer: black,
        status: black,
        inspector: black,
        inspector_active: active,
        lht: black,
        lht_active: active,
    }
}

fn cool_blue_surfaces() -> PanelSurfaces {
    PanelSurfaces {
        title: rgb(0, 0, 0),
        left: rgb(0x12, 0x18, 0x20),
        left_active: rgb(0x1a, 0x22, 0x30),
        transcript: rgb(0, 0, 0),
        activity: rgb(0, 0, 0),
        composer: rgb(0x08, 0x0b, 0x0f),
        status: rgb(0x05, 0x06, 0x08),
        inspector: rgb(0x14, 0x18, 0x20),
        inspector_active: rgb(0x1c, 0x24, 0x30),
        lht: rgb(0x0c, 0x10, 0x18),
        lht_active: rgb(0x14, 0x1c, 0x28),
    }
}

fn gray_scale_surfaces() -> PanelSurfaces {
    PanelSurfaces {
        title: rgb(0, 0, 0),
        left: rgb(0x11, 0x11, 0x11),
        left_active: rgb(0x1a, 0x1a, 0x1a),
        transcript: rgb(0, 0, 0),
        activity: rgb(0, 0, 0),
        composer: rgb(0x0a, 0x0a, 0x0a),
        status: rgb(0x05, 0x05, 0x05),
        inspector: rgb(0x12, 0x12, 0x12),
        inspector_active: rgb(0x1a, 0x1a, 0x1a),
        lht: rgb(0x0a, 0x0a, 0x0a),
        lht_active: rgb(0x12, 0x12, 0x12),
    }
}

fn charcoal_surfaces() -> PanelSurfaces {
    PanelSurfaces {
        title: rgb(0, 0, 0),
        left: rgb(0x16, 0x16, 0x16),
        left_active: rgb(0x20, 0x20, 0x20),
        transcript: rgb(0, 0, 0),
        activity: rgb(0, 0, 0),
        composer: rgb(0x12, 0x12, 0x12),
        status: rgb(0x08, 0x08, 0x08),
        inspector: rgb(0x18, 0x18, 0x18),
        inspector_active: rgb(0x22, 0x22, 0x22),
        lht: rgb(0x10, 0x10, 0x10),
        lht_active: rgb(0x18, 0x18, 0x18),
    }
}

fn dracula_tint_surfaces() -> PanelSurfaces {
    PanelSurfaces {
        title: rgb(0, 0, 0),
        left: rgb(0x15, 0x13, 0x1a),
        left_active: rgb(0x1e, 0x1b, 0x26),
        transcript: rgb(0, 0, 0),
        activity: rgb(0, 0, 0),
        composer: rgb(0x0e, 0x0c, 0x12),
        status: rgb(0x08, 0x07, 0x0a),
        inspector: rgb(0x17, 0x14, 0x1f),
        inspector_active: rgb(0x21, 0x1d, 0x2a),
        lht: rgb(0x0f, 0x0d, 0x14),
        lht_active: rgb(0x17, 0x15, 0x1e),
    }
}

fn high_contrast_surfaces() -> PanelSurfaces {
    PanelSurfaces {
        title: rgb(0, 0, 0),
        left: rgb(0x1e, 0x1e, 0x1e),
        left_active: rgb(0x28, 0x28, 0x28),
        transcript: rgb(0, 0, 0),
        activity: rgb(0, 0, 0),
        composer: rgb(0x14, 0x14, 0x14),
        status: rgb(0x0a, 0x0a, 0x0a),
        inspector: rgb(0x22, 0x22, 0x22),
        inspector_active: rgb(0x2c, 0x2c, 0x2c),
        lht: rgb(0x16, 0x16, 0x16),
        lht_active: rgb(0x20, 0x20, 0x20),
    }
}

static THEME: std::sync::OnceLock<std::sync::RwLock<TuiTheme>> = std::sync::OnceLock::new();

/// Install the active theme for the process (call at TUI startup and on theme switch).
pub fn install(theme: TuiTheme) {
    match THEME.get() {
        Some(lock) => *lock.write().expect("theme lock") = theme,
        None => {
            let _ = THEME.set(std::sync::RwLock::new(theme));
        }
    }
}

#[must_use]
pub fn current() -> TuiTheme {
    THEME
        .get()
        .map(|lock| *lock.read().expect("theme lock"))
        .unwrap_or_else(TuiTheme::default_theme)
}

#[must_use]
pub fn current_id() -> TuiThemeId {
    current().id
}

#[must_use]
pub fn pane_chrome_rows() -> u16 {
    current().pane_chrome_rows()
}

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

    #[test]
    fn cool_blue_matches_fixture_preview() {
        let s = cool_blue_surfaces();
        assert_eq!(s.left, rgb(0x12, 0x18, 0x20));
        assert_eq!(s.transcript, rgb(0, 0, 0));
        assert_eq!(s.composer, rgb(0x08, 0x0b, 0x0f));
        assert_eq!(s.lht, rgb(0x0c, 0x10, 0x18));
    }

    #[test]
    fn theme_id_round_trip() {
        assert_eq!(
            TuiThemeId::from_storage(Some("cool-blue")),
            TuiThemeId::CoolBlue
        );
        assert_eq!(TuiThemeId::CoolBlue.cycle(), TuiThemeId::GrayScale);
        assert!(TuiTheme::resolve(TuiThemeId::CoolBlue).borderless());
        assert!(TuiTheme::resolve(TuiThemeId::Classic).layout == ThemeLayout::Bordered);
    }
}