mermaid-cli 0.30.0

Open-source AI pair programmer with agentic capabilities. Local-first with Ollama, native tool calling, and beautiful TUI.
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
use ratatui::style::Color;
use serde::{Deserialize, Serialize};

/// Theme configuration for the TUI
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Theme {
    pub name: String,
    pub colors: ThemeColors,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ThemeColors {
    // Primary colors
    pub background: ColorValue,
    pub foreground: ColorValue,

    // UI elements
    pub border: ColorValue,
    pub header: ColorValue,

    // Text colors
    pub text_primary: ColorValue,
    pub text_secondary: ColorValue,
    pub text_disabled: ColorValue,
    pub text_highlight: ColorValue,

    /// Full-width background band behind the user's submitted prompt
    /// (Claude-Code style). A clearly-visible neutral gray, a step above the
    /// main background — not a blue tint. The `/load` and rewind pickers lay
    /// their highlighted row on it too.
    pub user_message_background: ColorValue,

    // Code highlighting
    pub code_background: ColorValue,
    pub code_foreground: ColorValue,
    pub code_keyword: ColorValue,
    pub code_string: ColorValue,
    pub code_comment: ColorValue,

    // Status colors
    pub success: ColorValue,
    pub warning: ColorValue,
    pub error: ColorValue,
    pub info: ColorValue,

    /// Brand accent (aqua #22D3EE; the light theme carries a deeper aqua that
    /// reads on white) — the `ask_user_question` modal's header chip and
    /// border, the checklist marks, the model-picker cursor. Serde-defaulted
    /// so themes/configs predating it still load.
    #[serde(default = "default_brand")]
    pub brand: ColorValue,

    /// Muted meta text (tool headers, byline timestamps) — deliberately a
    /// specific mid-gray rather than the terminal's ANSI gray, which most
    /// palettes render much brighter. Serde-defaulted like `brand`.
    #[serde(default = "default_text_meta")]
    pub text_meta: ColorValue,
    /// Background band behind added diff lines.
    #[serde(default = "default_diff_added_bg")]
    pub diff_added_bg: ColorValue,
    /// Background band behind removed diff lines.
    #[serde(default = "default_diff_removed_bg")]
    pub diff_removed_bg: ColorValue,
    /// Highlight band behind queued (mid-run steering) messages in the
    /// status area.
    #[serde(default = "default_queued_bg")]
    pub queued_bg: ColorValue,
}

/// Mermaid's aqua brand accent, used when a theme omits `brand`.
fn default_brand() -> ColorValue {
    ColorValue::Rgb {
        r: 34,
        g: 211,
        b: 238,
    }
}

/// Dark-theme values double as serde defaults so themes/configs predating
/// these fields keep today's exact colors.
fn default_text_meta() -> ColorValue {
    ColorValue::Rgb {
        r: 136,
        g: 136,
        b: 136,
    }
}

fn default_diff_added_bg() -> ColorValue {
    ColorValue::Rgb {
        r: 20,
        g: 50,
        b: 20,
    }
}

fn default_diff_removed_bg() -> ColorValue {
    ColorValue::Rgb {
        r: 60,
        g: 20,
        b: 20,
    }
}

fn default_queued_bg() -> ColorValue {
    ColorValue::Rgb {
        r: 60,
        g: 60,
        b: 80,
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ColorValue {
    Rgb { r: u8, g: u8, b: u8 },
    Named(String),
}

impl ColorValue {
    #[must_use]
    pub fn to_color(&self) -> Color {
        match self {
            Self::Rgb { r, g, b } => Color::Rgb(*r, *g, *b),
            Self::Named(name) => match name.as_str() {
                // The terminal's own default fg/bg — what `Theme::plain()`
                // (NO_COLOR) is built from.
                "default" => Color::Reset,
                "black" => Color::Black,
                "red" => Color::Red,
                "green" => Color::Green,
                "yellow" => Color::Yellow,
                "blue" => Color::Blue,
                "magenta" => Color::Magenta,
                "cyan" => Color::Cyan,
                "white" => Color::White,
                "gray" | "grey" => Color::Gray,
                "dark_gray" | "dark_grey" => Color::DarkGray,
                _ => Color::White,
            },
        }
    }
}

/// A fixed 24-bit colour, which renders the same on every terminal palette.
const fn rgb(r: u8, g: u8, b: u8) -> ColorValue {
    ColorValue::Rgb { r, g, b }
}

impl Theme {
    /// The palette a frame draws with. `NO_COLOR` beats the theme choice
    /// (colours off entirely); otherwise `ui.theme` / `/theme` picks dark or
    /// light. The TUI and the `--resume` picker both resolve through here, so
    /// the picker can never disagree with the session it opens.
    #[must_use]
    pub fn resolve(choice: mermaid_domain::ThemeChoice, no_color: bool) -> Self {
        if no_color {
            return Self::plain();
        }
        match choice {
            mermaid_domain::ThemeChoice::Dark => Self::dark(),
            mermaid_domain::ThemeChoice::Light => Self::light(),
        }
    }

    /// Create a light theme. Selected by `ui.theme = "light"` in config.toml
    /// or `/theme light` (see `render()`'s theme memo).
    ///
    /// The greys, the green, the yellow and the brand accent are fixed RGB
    /// here, not ANSI names: a terminal palette tunes its gray, green and
    /// yellow for a dark background, and on a light one they wash out (on
    /// Windows Terminal's default palette, gray reads at 1.5:1 against this
    /// background and yellow at 2.5:1). Each fixed value clears WCAG AA on
    /// the ground it is drawn on, 4.5:1 for text and 3:1 for a border, which
    /// `light_theme_fixed_inks_meet_contrast_on_their_grounds` pins.
    #[must_use]
    pub fn light() -> Self {
        Self {
            name: "Light".to_string(),
            colors: ThemeColors {
                background: rgb(250, 250, 250),
                foreground: rgb(30, 30, 30),

                border: rgb(112, 112, 112),
                header: ColorValue::Named("blue".to_string()),

                text_primary: ColorValue::Named("black".to_string()),
                text_secondary: rgb(85, 85, 85),
                text_disabled: rgb(112, 112, 112),
                text_highlight: ColorValue::Named("magenta".to_string()),

                user_message_background: rgb(230, 230, 230),

                code_background: rgb(240, 240, 240),
                code_foreground: rgb(85, 85, 85),
                code_keyword: ColorValue::Named("magenta".to_string()),
                code_string: rgb(20, 117, 54),
                code_comment: rgb(105, 105, 105),

                success: rgb(20, 117, 54),
                warning: rgb(148, 98, 0),
                error: ColorValue::Named("red".to_string()),
                info: ColorValue::Named("blue".to_string()),

                // The dark theme's aqua is 1.7:1 on this background; the same
                // hue, deepened until it reads as text and under the chip label.
                brand: rgb(14, 116, 144),
                text_meta: rgb(110, 110, 110),
                diff_added_bg: rgb(220, 245, 220),
                diff_removed_bg: rgb(250, 225, 225),
                queued_bg: rgb(225, 225, 240),
            },
        }
    }

    /// Create the default dark theme
    #[must_use]
    pub fn dark() -> Self {
        Self {
            name: "Dark".to_string(),
            colors: ThemeColors {
                background: rgb(20, 20, 20),
                foreground: rgb(230, 230, 230),

                border: ColorValue::Named("dark_gray".to_string()),
                header: ColorValue::Named("cyan".to_string()),

                text_primary: ColorValue::Named("white".to_string()),
                text_secondary: ColorValue::Named("gray".to_string()),
                text_disabled: ColorValue::Named("dark_gray".to_string()),
                text_highlight: ColorValue::Named("yellow".to_string()),

                user_message_background: rgb(54, 54, 54),

                code_background: rgb(40, 40, 40),
                code_foreground: ColorValue::Named("gray".to_string()),
                code_keyword: ColorValue::Named("magenta".to_string()),
                code_string: ColorValue::Named("green".to_string()),
                code_comment: ColorValue::Named("dark_gray".to_string()),

                success: ColorValue::Named("green".to_string()),
                warning: ColorValue::Named("yellow".to_string()),
                error: ColorValue::Named("red".to_string()),
                info: ColorValue::Named("cyan".to_string()),

                brand: default_brand(),
                text_meta: default_text_meta(),
                diff_added_bg: default_diff_added_bg(),
                diff_removed_bg: default_diff_removed_bg(),
                queued_bg: default_queued_bg(),
            },
        }
    }

    /// Colorless theme for `NO_COLOR`: every slot is the terminal's own
    /// default fg/bg (`Color::Reset`), so nothing emits a color at all.
    /// Structure (glyphs, layout, bold/dim) is untouched — diffs still read
    /// via their `+`/`-` prefixes.
    #[must_use]
    pub fn plain() -> Self {
        fn d() -> ColorValue {
            ColorValue::Named("default".to_string())
        }
        Self {
            name: "Plain".to_string(),
            colors: ThemeColors {
                background: d(),
                foreground: d(),
                border: d(),
                header: d(),
                text_primary: d(),
                text_secondary: d(),
                text_disabled: d(),
                text_highlight: d(),
                user_message_background: d(),
                code_background: d(),
                code_foreground: d(),
                code_keyword: d(),
                code_string: d(),
                code_comment: d(),
                success: d(),
                warning: d(),
                error: d(),
                info: d(),
                brand: d(),
                text_meta: d(),
                diff_added_bg: d(),
                diff_removed_bg: d(),
                queued_bg: d(),
            },
        }
    }
}

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

    #[test]
    fn named_default_maps_to_reset() {
        assert_eq!(
            ColorValue::Named("default".to_string()).to_color(),
            Color::Reset
        );
    }

    #[test]
    fn plain_theme_is_entirely_reset() {
        // Every slot must resolve to the terminal's own default — a single
        // colored slot would defeat NO_COLOR. Serializing the palette and
        // scanning for any non-"default" value covers all fields without
        // enumerating them (new fields are covered automatically).
        let theme = Theme::plain();
        let json = serde_json::to_value(&theme.colors).unwrap();
        let obj = json.as_object().unwrap();
        assert!(!obj.is_empty());
        for (field, value) in obj {
            assert_eq!(
                value.as_str(),
                Some("default"),
                "plain theme leaks color through `{field}`: {value}"
            );
        }
    }

    #[test]
    fn dark_and_light_populate_the_new_slots() {
        for theme in [Theme::dark(), Theme::light()] {
            assert_ne!(theme.colors.diff_added_bg.to_color(), Color::Reset);
            assert_ne!(theme.colors.diff_removed_bg.to_color(), Color::Reset);
            assert_ne!(theme.colors.queued_bg.to_color(), Color::Reset);
            assert_ne!(theme.colors.text_meta.to_color(), Color::Reset);
        }
    }

    /// WCAG 2 relative luminance of a fixed RGB slot, or `None` for a named
    /// ANSI slot, whose shade belongs to the terminal's palette.
    fn luminance(value: &ColorValue) -> Option<f64> {
        let ColorValue::Rgb { r, g, b } = value else {
            return None;
        };
        let linear = |channel: u8| {
            let srgb = f64::from(channel) / 255.0;
            if srgb <= 0.04045 {
                srgb / 12.92
            } else {
                ((srgb + 0.055) / 1.055).powf(2.4)
            }
        };
        Some(
            [(0.2126, *r), (0.7152, *g), (0.0722, *b)]
                .into_iter()
                .map(|(weight, channel)| weight * linear(channel))
                .sum(),
        )
    }

    #[test]
    fn light_theme_fixed_inks_meet_contrast_on_their_grounds() {
        // Each light slot the theme pins to RGB, on the ground it is drawn
        // on: 4.5:1 for text, 3:1 for a border (WCAG AA). A slot switched
        // back to an ANSI name fails here too, since its contrast is then the
        // terminal's to decide.
        let c = Theme::light().colors;
        let pairs = [
            ("text_secondary", &c.text_secondary, &c.background, 4.5),
            ("text_disabled", &c.text_disabled, &c.background, 4.5),
            ("text_meta", &c.text_meta, &c.background, 4.5),
            ("brand", &c.brand, &c.background, 4.5),
            ("the question chip label", &c.background, &c.brand, 4.5),
            ("success", &c.success, &c.background, 4.5),
            ("success on its diff row", &c.success, &c.diff_added_bg, 4.5),
            ("warning", &c.warning, &c.background, 4.5),
            (
                "code_foreground",
                &c.code_foreground,
                &c.code_background,
                4.5,
            ),
            ("code_string", &c.code_string, &c.code_background, 4.5),
            ("code_comment", &c.code_comment, &c.code_background, 4.5),
            ("border", &c.border, &c.background, 3.0),
        ];
        for (slot, ink, ground, floor) in pairs {
            let ratio = luminance(ink)
                .zip(luminance(ground))
                .map_or(0.0, |(ink, ground)| {
                    (ink.max(ground) + 0.05) / (ink.min(ground) + 0.05)
                });
            assert!(
                ratio >= floor,
                "light {slot} is {ratio:.2}:1, under the {floor}:1 floor \
                 (0 means an ANSI name, whose contrast the terminal decides)"
            );
        }
    }

    #[test]
    fn resolve_lets_no_color_beat_the_theme_choice() {
        use mermaid_domain::ThemeChoice;
        assert_eq!(Theme::resolve(ThemeChoice::Dark, false).name, "Dark");
        assert_eq!(Theme::resolve(ThemeChoice::Light, false).name, "Light");
        assert_eq!(Theme::resolve(ThemeChoice::Light, true).name, "Plain");
        assert_eq!(Theme::resolve(ThemeChoice::Dark, true).name, "Plain");
    }

    #[test]
    fn theme_colors_deserialize_defaults_new_fields() {
        // A theme serialized before the new slots existed still loads, with
        // the dark values as defaults.
        let dark = Theme::dark();
        let mut json = serde_json::to_value(&dark.colors).unwrap();
        let obj = json.as_object_mut().unwrap();
        for field in ["text_meta", "diff_added_bg", "diff_removed_bg", "queued_bg"] {
            obj.remove(field);
        }
        let colors: ThemeColors = serde_json::from_value(json).unwrap();
        assert_eq!(
            colors.text_meta.to_color(),
            dark.colors.text_meta.to_color()
        );
        assert_eq!(
            colors.diff_added_bg.to_color(),
            dark.colors.diff_added_bg.to_color()
        );
    }
}