chromewright 0.8.0

Browser automation MCP server via Chrome DevTools Protocol (CDP)
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
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
//! Terminal-native TUI theme (ANSI-16 role palette).
//!
//! Defaults use the terminal's 16-color palette and leave base fg/bg unset so
//! light/dark themes inherit correctly. Selection is applied last as reverse
//! video so it stays visible over kind colors.
//!
//! Heading levels and links use a clearer ladder for markdown-like reading
//! (inspired by md-tui's role idea, not a 1:1 color match). Optional
//! `[theme]` keys in `tui.toml` override individual roles.

use crate::semantic::SemanticKind;
use ratatui::style::{Color, Modifier, Style};
use std::collections::HashMap;
use std::fmt;

/// Named content/chrome roles that can be overridden in TOML.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ThemeRole {
    /// Hyperlink text.
    Link,
    /// Heading level 1.
    H1,
    /// Heading level 2.
    H2,
    /// Heading level 3.
    H3,
    /// Heading level 4.
    H4,
    /// Heading level 5.
    H5,
    /// Heading level 6.
    H6,
    /// Landmark region labels.
    Landmark,
    /// Generic group containers.
    Group,
    /// List containers and items.
    List,
    /// Image placeholders / alt text.
    Image,
    /// Form controls (input, select, button, …).
    FormControl,
    /// Two-key hint labels painted over targets.
    HintLabel,
    /// Dim/secondary text (spacers, muted chrome).
    Muted,
    /// Header chrome while lifecycle is Ready.
    ChromeReady,
    /// Header chrome while lifecycle is Loading.
    ChromeLoading,
    /// Header chrome while lifecycle is Error.
    ChromeError,
    /// Mode indicator in chrome (Normal / Input / Hint).
    ChromeMode,
    /// Agent attention foreground paint.
    AttentionFg,
    /// Agent attention background paint.
    AttentionBg,
}

impl ThemeRole {
    /// Config/TOML key for this role (`link`, `h1`, `chrome_ready`, …).
    pub fn name(self) -> &'static str {
        match self {
            Self::Link => "link",
            Self::H1 => "h1",
            Self::H2 => "h2",
            Self::H3 => "h3",
            Self::H4 => "h4",
            Self::H5 => "h5",
            Self::H6 => "h6",
            Self::Landmark => "landmark",
            Self::Group => "group",
            Self::List => "list",
            Self::Image => "image",
            Self::FormControl => "form_control",
            Self::HintLabel => "hint_label",
            Self::Muted => "muted",
            Self::ChromeReady => "chrome_ready",
            Self::ChromeLoading => "chrome_loading",
            Self::ChromeError => "chrome_error",
            Self::ChromeMode => "chrome_mode",
            Self::AttentionFg => "attention_fg",
            Self::AttentionBg => "attention_bg",
        }
    }

    /// Parse a config/TOML role name; unknown names return `None`.
    pub fn from_name(name: &str) -> Option<Self> {
        match name {
            "link" => Some(Self::Link),
            "h1" => Some(Self::H1),
            "h2" => Some(Self::H2),
            "h3" => Some(Self::H3),
            "h4" => Some(Self::H4),
            "h5" => Some(Self::H5),
            "h6" => Some(Self::H6),
            "landmark" => Some(Self::Landmark),
            "group" => Some(Self::Group),
            "list" => Some(Self::List),
            "image" => Some(Self::Image),
            "form_control" | "form" => Some(Self::FormControl),
            "hint_label" | "hint" => Some(Self::HintLabel),
            "muted" => Some(Self::Muted),
            "chrome_ready" => Some(Self::ChromeReady),
            "chrome_loading" => Some(Self::ChromeLoading),
            "chrome_error" => Some(Self::ChromeError),
            "chrome_mode" => Some(Self::ChromeMode),
            "attention_fg" => Some(Self::AttentionFg),
            "attention_bg" => Some(Self::AttentionBg),
            _ => None,
        }
    }

    fn all() -> &'static [ThemeRole] {
        &[
            Self::Link,
            Self::H1,
            Self::H2,
            Self::H3,
            Self::H4,
            Self::H5,
            Self::H6,
            Self::Landmark,
            Self::Group,
            Self::List,
            Self::Image,
            Self::FormControl,
            Self::HintLabel,
            Self::Muted,
            Self::ChromeReady,
            Self::ChromeLoading,
            Self::ChromeError,
            Self::ChromeMode,
            Self::AttentionFg,
            Self::AttentionBg,
        ]
    }
}

/// ANSI-16 role colors. `None` means "use terminal default" for that channel.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ThemePalette {
    colors: HashMap<ThemeRole, Color>,
}

impl Default for ThemePalette {
    fn default() -> Self {
        Self::defaults()
    }
}

impl ThemePalette {
    /// Built-in palette: clearer hierarchy within ANSI-16, not a 1:1 md-tui copy.
    pub fn defaults() -> Self {
        let mut colors = HashMap::new();
        // Content — spread the ladder so prose is scannable.
        colors.insert(ThemeRole::Link, Color::Blue);
        colors.insert(ThemeRole::H1, Color::LightBlue);
        colors.insert(ThemeRole::H2, Color::Green);
        colors.insert(ThemeRole::H3, Color::Magenta);
        colors.insert(ThemeRole::H4, Color::Cyan);
        colors.insert(ThemeRole::H5, Color::Yellow);
        colors.insert(ThemeRole::H6, Color::LightRed);
        colors.insert(ThemeRole::Landmark, Color::Green);
        colors.insert(ThemeRole::Group, Color::DarkGray);
        colors.insert(ThemeRole::List, Color::DarkGray);
        // Images are low-priority chrome in the semantic view — keep them muted.
        colors.insert(ThemeRole::Image, Color::DarkGray);
        // Browser-only roles stay distinct from each other.
        colors.insert(ThemeRole::FormControl, Color::LightCyan);
        colors.insert(ThemeRole::HintLabel, Color::Yellow);
        colors.insert(ThemeRole::Muted, Color::DarkGray);
        // Chrome / status
        colors.insert(ThemeRole::ChromeReady, Color::Green);
        colors.insert(ThemeRole::ChromeLoading, Color::Yellow);
        colors.insert(ThemeRole::ChromeError, Color::Red);
        colors.insert(ThemeRole::ChromeMode, Color::Green);
        colors.insert(ThemeRole::AttentionFg, Color::Black);
        colors.insert(ThemeRole::AttentionBg, Color::Magenta);
        Self { colors }
    }

    /// Color assigned to `role`, if configured.
    pub fn get(&self, role: ThemeRole) -> Option<Color> {
        self.colors.get(&role).copied()
    }

    /// Override the color for one semantic role.
    pub fn set(&mut self, role: ThemeRole, color: Color) {
        self.colors.insert(role, color);
    }

    /// Overlay `role = "color"` entries. Unknown roles or colors fail closed.
    pub fn overlay_from_map(
        &self,
        overrides: &HashMap<String, String>,
    ) -> Result<Self, ThemeError> {
        let mut next = self.clone();
        for (name, spec) in overrides {
            let role =
                ThemeRole::from_name(name).ok_or_else(|| ThemeError::UnknownRole(name.clone()))?;
            let color = parse_color(spec)?;
            next.set(role, color);
        }
        Ok(next)
    }
}

/// Semantic style roles for chrome and content rendering.
#[derive(Debug, Clone)]
pub struct TuiTheme {
    palette: ThemePalette,
}

impl Default for TuiTheme {
    fn default() -> Self {
        Self::new()
    }
}

impl TuiTheme {
    /// Default palette mapped onto chrome and content style helpers.
    pub fn new() -> Self {
        Self {
            palette: ThemePalette::defaults(),
        }
    }

    /// Build a theme from an explicit palette (config overlays, tests).
    pub fn with_palette(palette: ThemePalette) -> Self {
        Self { palette }
    }

    /// Borrow the underlying role → color map.
    pub fn palette(&self) -> &ThemePalette {
        &self.palette
    }

    fn fg(&self, role: ThemeRole) -> Style {
        match self.palette.get(role) {
            Some(c) => self.base().fg(c),
            None => self.base(),
        }
    }

    /// Base style: do not force fg/bg (terminal default).
    pub fn base(&self) -> Style {
        Style::default()
    }

    /// Header mode/status glyph when Ready.
    pub fn chrome_mode(&self) -> Style {
        self.fg(ThemeRole::ChromeMode).add_modifier(Modifier::BOLD)
    }

    /// Header/status style for Ready lifecycle.
    pub fn chrome_ready(&self) -> Style {
        self.fg(ThemeRole::ChromeReady)
    }

    /// Header/status style for Loading lifecycle.
    pub fn chrome_loading(&self) -> Style {
        self.fg(ThemeRole::ChromeLoading)
    }

    /// Header/status style for Error lifecycle.
    pub fn chrome_error(&self) -> Style {
        self.fg(ThemeRole::ChromeError).add_modifier(Modifier::BOLD)
    }

    /// Soft chrome accents (wrap indicators, secondary chrome).
    pub fn chrome_wrap(&self) -> Style {
        self.fg(ThemeRole::Group)
    }

    /// History affordance when back/forward is available.
    pub fn chrome_hist_enabled(&self) -> Style {
        self.fg(ThemeRole::ChromeReady)
    }

    /// History affordance when back/forward is unavailable.
    pub fn chrome_hist_disabled(&self) -> Style {
        self.muted()
    }

    /// Dim text for secondary content (images, disabled chrome).
    pub fn muted(&self) -> Style {
        self.fg(ThemeRole::Muted)
    }

    /// Scrollbar track: solid dim block (background fill, no glyphs).
    pub fn scrollbar_track(&self) -> Style {
        // DarkGray bg reads as a quiet rail on both light and dark terminals.
        Style::default().bg(Color::DarkGray).fg(Color::DarkGray)
    }

    /// Scrollbar thumb: brighter solid block (Amp-style, no │/▐ line art).
    pub fn scrollbar_thumb(&self) -> Style {
        Style::default().bg(Color::Gray).fg(Color::Gray)
    }

    /// Footer/status style for error messages.
    pub fn status_error(&self) -> Style {
        self.chrome_error()
    }

    /// Footer/status style for in-flight actions.
    pub fn status_loading(&self) -> Style {
        self.chrome_loading()
    }

    /// Footer/status style for success/ready feedback.
    pub fn status_ok(&self) -> Style {
        self.chrome_ready()
    }

    /// Agent attention highlight over content lines.
    pub fn attention_overlay(&self) -> Style {
        // Background fill is the readable cue: fg-only was invisible on
        // colored headings (and image lines are muted gray). Selection still
        // wins via reverse when both apply.
        let mut style = Style::default().add_modifier(Modifier::BOLD);
        if let Some(fg) = self.palette.get(ThemeRole::AttentionFg) {
            style = style.fg(fg);
        }
        if let Some(bg) = self.palette.get(ThemeRole::AttentionBg) {
            style = style.bg(bg);
        }
        style
    }

    /// Applied last so selection remains visible over kind colors.
    pub fn selection_overlay(&self) -> Style {
        Style::default().add_modifier(Modifier::REVERSED | Modifier::BOLD)
    }

    /// Jump-label chrome (`[aa]`) in hint mode — distinct from form field content.
    pub fn hint_label(&self) -> Style {
        self.fg(ThemeRole::HintLabel).add_modifier(Modifier::BOLD)
    }

    /// Form controls (input/textarea/select/button) in the content pane.
    pub fn form_control(&self) -> Style {
        self.fg(ThemeRole::FormControl).add_modifier(Modifier::BOLD)
    }

    /// Style for a content line from its semantic kind and optional heading level.
    pub fn content_style(&self, kind: Option<SemanticKind>, heading_level: Option<u8>) -> Style {
        match kind {
            Some(SemanticKind::Heading) => self.heading(heading_level.unwrap_or(2)),
            // Link color only; underline is applied to the URL span in render
            // (`[label](url)`), not the whole line.
            Some(SemanticKind::Link) => self.fg(ThemeRole::Link),
            Some(SemanticKind::Image) => self.fg(ThemeRole::Image),
            Some(SemanticKind::Landmark) => {
                self.fg(ThemeRole::Landmark).add_modifier(Modifier::BOLD)
            }
            Some(SemanticKind::Group) => self.fg(ThemeRole::Group),
            Some(SemanticKind::List) => self.fg(ThemeRole::List),
            Some(SemanticKind::ListItem) => self.base(),
            Some(SemanticKind::Input)
            | Some(SemanticKind::Textarea)
            | Some(SemanticKind::Select)
            | Some(SemanticKind::Button) => self.form_control(),
            Some(SemanticKind::Text) | None => self.base(),
        }
    }

    fn heading(&self, level: u8) -> Style {
        let role = match level {
            1 => ThemeRole::H1,
            2 => ThemeRole::H2,
            3 => ThemeRole::H3,
            4 => ThemeRole::H4,
            5 => ThemeRole::H5,
            _ => ThemeRole::H6,
        };
        let style = self.fg(role);
        // H1–H3 stay bold for scan weight; lower levels are color-only.
        if level <= 3 {
            style.add_modifier(Modifier::BOLD)
        } else {
            style
        }
    }

    /// Layer kind → attention → selection (selection wins).
    pub fn line_style(
        &self,
        kind: Option<SemanticKind>,
        heading_level: Option<u8>,
        selected: bool,
        attention: bool,
    ) -> Style {
        let mut style = self.content_style(kind, heading_level);
        if attention {
            style = style.patch(self.attention_overlay());
        }
        if selected {
            style = style.patch(self.selection_overlay());
        }
        style
    }
}

/// Parse a TOML color name into a ratatui [`Color`].
///
/// Accepts ANSI names (`blue`, `lightcyan`, …), `reset`/`default` (maps to
/// `Reset`), and `#rrggbb` hex. Empty string is rejected (use `reset`).
pub fn parse_color(spec: &str) -> Result<Color, ThemeError> {
    let s = spec.trim();
    if s.is_empty() {
        return Err(ThemeError::InvalidColor(spec.to_string()));
    }
    let lower = s.to_ascii_lowercase();
    match lower.as_str() {
        "reset" | "default" | "none" => Ok(Color::Reset),
        "black" => Ok(Color::Black),
        "red" => Ok(Color::Red),
        "green" => Ok(Color::Green),
        "yellow" => Ok(Color::Yellow),
        "blue" => Ok(Color::Blue),
        "magenta" | "purple" => Ok(Color::Magenta),
        "cyan" => Ok(Color::Cyan),
        "gray" | "grey" | "darkgray" | "darkgrey" => Ok(Color::DarkGray),
        "white" => Ok(Color::White),
        "lightred" => Ok(Color::LightRed),
        "lightgreen" => Ok(Color::LightGreen),
        "lightyellow" => Ok(Color::LightYellow),
        "lightblue" => Ok(Color::LightBlue),
        "lightmagenta" => Ok(Color::LightMagenta),
        "lightcyan" => Ok(Color::LightCyan),
        "lightgray" | "lightgrey" => Ok(Color::Gray),
        hex if hex.starts_with('#') && hex.len() == 7 => {
            let r = u8::from_str_radix(&hex[1..3], 16)
                .map_err(|_| ThemeError::InvalidColor(spec.to_string()))?;
            let g = u8::from_str_radix(&hex[3..5], 16)
                .map_err(|_| ThemeError::InvalidColor(spec.to_string()))?;
            let b = u8::from_str_radix(&hex[5..7], 16)
                .map_err(|_| ThemeError::InvalidColor(spec.to_string()))?;
            Ok(Color::Rgb(r, g, b))
        }
        _ => Err(ThemeError::InvalidColor(spec.to_string())),
    }
}

/// Theme configuration errors (fail startup when loading TOML).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ThemeError {
    /// TOML key is not a known [`ThemeRole`] name.
    UnknownRole(String),
    /// Color token could not be parsed as an ANSI-16 or named color.
    InvalidColor(String),
}

impl fmt::Display for ThemeError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::UnknownRole(name) => {
                write!(
                    f,
                    "unknown theme role '{name}' (valid: {})",
                    ThemeRole::all()
                        .iter()
                        .map(|r| r.name())
                        .collect::<Vec<_>>()
                        .join(", ")
                )
            }
            Self::InvalidColor(spec) => write!(
                f,
                "invalid theme color '{spec}' (use ANSI names like blue/lightcyan, reset, or #rrggbb)"
            ),
        }
    }
}

impl std::error::Error for ThemeError {}

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

    #[test]
    fn selection_overlay_includes_reverse() {
        let theme = TuiTheme::new();
        let style = theme.line_style(Some(SemanticKind::Link), None, true, true);
        assert!(style.add_modifier.contains(Modifier::REVERSED));
        assert!(style.add_modifier.contains(Modifier::BOLD));
    }

    #[test]
    fn attention_overlay_uses_magenta_background() {
        let theme = TuiTheme::new();
        let style = theme.line_style(Some(SemanticKind::Heading), Some(2), false, true);
        assert_eq!(style.bg, Some(Color::Magenta));
        assert_eq!(style.fg, Some(Color::Black));
        assert!(style.add_modifier.contains(Modifier::BOLD));
        let both = theme.line_style(Some(SemanticKind::Heading), Some(2), true, true);
        assert!(both.add_modifier.contains(Modifier::REVERSED));
    }

    #[test]
    fn heading_levels_use_distinct_ladder() {
        let theme = TuiTheme::new();
        let h1 = theme.content_style(Some(SemanticKind::Heading), Some(1));
        let h2 = theme.content_style(Some(SemanticKind::Heading), Some(2));
        let h3 = theme.content_style(Some(SemanticKind::Heading), Some(3));
        let h6 = theme.content_style(Some(SemanticKind::Heading), Some(6));
        assert_eq!(h1.fg, Some(Color::LightBlue));
        assert_eq!(h2.fg, Some(Color::Green));
        assert_eq!(h3.fg, Some(Color::Magenta));
        assert_eq!(h6.fg, Some(Color::LightRed));
        assert_ne!(h1.fg, h2.fg);
        assert_ne!(h2.fg, h3.fg);
        assert_ne!(h3.fg, h6.fg);
    }

    #[test]
    fn link_is_blue_without_full_line_underline() {
        let theme = TuiTheme::new();
        let link = theme.content_style(Some(SemanticKind::Link), None);
        assert_eq!(link.fg, Some(Color::Blue));
        // Underline is applied only to the URL span in render, not the whole line.
        assert!(!link.add_modifier.contains(Modifier::UNDERLINED));
    }

    #[test]
    fn hint_labels_differ_from_form_controls() {
        let theme = TuiTheme::new();
        let hint = theme.hint_label();
        let input = theme.content_style(Some(SemanticKind::Input), None);
        let button = theme.content_style(Some(SemanticKind::Button), None);
        assert_eq!(hint.fg, Some(Color::Yellow));
        assert_eq!(input.fg, Some(Color::LightCyan));
        assert_eq!(button.fg, Some(Color::LightCyan));
        assert_ne!(hint.fg, input.fg);
    }

    #[test]
    fn palette_overlay_replaces_named_roles() {
        let mut overrides = HashMap::new();
        overrides.insert("h2".into(), "yellow".into());
        overrides.insert("link".into(), "lightblue".into());
        let palette = ThemePalette::defaults()
            .overlay_from_map(&overrides)
            .expect("overlay");
        let theme = TuiTheme::with_palette(palette);
        assert_eq!(
            theme.content_style(Some(SemanticKind::Heading), Some(2)).fg,
            Some(Color::Yellow)
        );
        assert_eq!(
            theme.content_style(Some(SemanticKind::Link), None).fg,
            Some(Color::LightBlue)
        );
        // Untouched roles stay default.
        assert_eq!(
            theme.content_style(Some(SemanticKind::Heading), Some(3)).fg,
            Some(Color::Magenta)
        );
    }

    #[test]
    fn unknown_role_and_color_fail() {
        let mut bad_role = HashMap::new();
        bad_role.insert("not_a_role".into(), "blue".into());
        assert!(matches!(
            ThemePalette::defaults().overlay_from_map(&bad_role),
            Err(ThemeError::UnknownRole(_))
        ));
        let mut bad_color = HashMap::new();
        bad_color.insert("h1".into(), "blurple".into());
        assert!(matches!(
            ThemePalette::defaults().overlay_from_map(&bad_color),
            Err(ThemeError::InvalidColor(_))
        ));
    }

    #[test]
    fn parse_color_accepts_ansi_and_hex() {
        assert_eq!(parse_color("blue").unwrap(), Color::Blue);
        assert_eq!(parse_color("LightCyan").unwrap(), Color::LightCyan);
        assert_eq!(parse_color("reset").unwrap(), Color::Reset);
        assert_eq!(parse_color("#ff00aa").unwrap(), Color::Rgb(255, 0, 170));
    }
}