From 6ce97a0e3c4dc203da715acd8bf784d0a3ec4673 Mon Sep 17 00:00:00 2001 From: agent Date: Tue, 15 Sep 2026 17:29:26 -0400 Subject: [PATCH 1/2] tui: extract pub ColorMode/TokenStyles/tokens_for for testable colour tokens Make ColorMode a pub enum with pub color_mode() fn, add TokenStyles struct and tokens_for(mode) as the single source of truth for the style_* helpers. Tests assert RGB/None/Ansi16 fg/bg/modifier resolution without depending on process environment. --- src/tui.rs | 150 +++++++++++++++++++++++++++++++--------------- tests/tui_spec.rs | 95 +++++++++++++++++++++++++++++ 2 files changed, 196 insertions(+), 49 deletions(-) diff --git a/src/tui.rs b/src/tui.rs index 8622ca5..d07b88c 100644 --- a/src/tui.rs +++ b/src/tui.rs @@ -26,22 +26,6 @@ const C_STALE: Color = Color::Rgb(210, 180, 80); const C_HL_BG: Color = Color::Rgb(48, 42, 28); const C_BORDER: Color = Color::Rgb(72, 84, 64); -#[derive(Clone, Copy, PartialEq, Eq)] -enum ColorMode { - Rgb, - Ansi16, - None, -} - -fn color_mode() -> ColorMode { - if std::env::var_os("NO_COLOR").is_some() { - ColorMode::None - } else if std::env::var("TERM").ok().as_deref() == Some("dumb") || !io::stdout().is_terminal() { - ColorMode::Ansi16 - } else { - ColorMode::Rgb - } -} /// ASCII chrome when `ONIONWIRE_ASCII` or `NO_COLOR` is set. No extra crate. pub fn ascii_chrome() -> bool { @@ -340,53 +324,121 @@ fn truncate_chars(s: &str, max: usize) -> String { format!("{head}{e}") } -fn style_fg(rgb: Color, ansi: Color) -> Style { - match color_mode() { - ColorMode::None => Style::default(), - ColorMode::Ansi16 => Style::default().fg(ansi), - ColorMode::Rgb => Style::default().fg(rgb), +/// Terminal colour capability, detected from the environment. +/// +/// `ColorMode::None` means “strip all colour” (e.g. `NO_COLOR`); the TUI then +/// falls back to ASCII chrome and a `! ` prefix on alerts so signal survives. +/// `Ansi16` is the 16-colour fallback for dumb / non-TTY output. `Rgb` is the +/// full-fidelity path. +/// +/// The palette (`C_*`) is always defined. `tokens_for(mode)` projects it into a +/// concrete `TokenStyles` so that tests can assert on the resolved `Style` +/// without depending on the process environment. +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub enum ColorMode { + Rgb, + Ansi16, + None, +} + +/// Detect the terminal's colour capability from the environment. +/// +/// - `NO_COLOR` is set → `ColorMode::None`. +/// - `TERM=dumb` or stdout is not a TTY → `ColorMode::Ansi16`. +/// - Otherwise → `ColorMode::Rgb`. +pub fn color_mode() -> ColorMode { + if std::env::var_os("NO_COLOR").is_some() { + ColorMode::None + } else if std::env::var("TERM").ok().as_deref() == Some("dumb") || !io::stdout().is_terminal() { + ColorMode::Ansi16 + } else { + ColorMode::Rgb } } -fn style_accent() -> Style { - match color_mode() { +/// All UI styles resolved for a single `ColorMode`. Tests construct this +/// directly instead of calling `color_mode()`, because the latter reads the +/// process environment (which is non-deterministic in `cargo test`). +pub struct TokenStyles { + /// Primary text (names, message bodies). + pub text: Style, + /// Muted / secondary text (timestamps, hints). + pub dim: Style, + /// Accent: headers, focus ring, selected friend. + pub accent: Style, + /// Alert: failure / validation errors. + pub alert: Style, + /// Stale data (unconfirmed, timed out). + pub stale: Style, + /// Border / chrome lines. + pub border: Style, + /// Highlight block (selected list row). + pub highlight: Style, +} + +/// Project the RGB palette constants into a concrete [`TokenStyles`] for the +/// given `ColorMode`. This is the single source of truth the `style_*` helpers +/// delegate to, and the only surface tests assert against. +pub fn tokens_for(mode: ColorMode) -> TokenStyles { + let rgb = |fg: Color, ansi: Color| match mode { + ColorMode::None => Style::default(), + ColorMode::Ansi16 => Style::default().fg(ansi), + ColorMode::Rgb => Style::default().fg(fg), + }; + let accent = match mode { ColorMode::None => Style::default().add_modifier(Modifier::BOLD), ColorMode::Ansi16 => Style::default().fg(Color::Yellow), ColorMode::Rgb => Style::default().fg(C_ACCENT), - } -} - -fn style_text() -> Style { - style_fg(C_TEXT, Color::White) -} - -fn style_dim() -> Style { - style_fg(C_DIM, Color::DarkGray) -} - -fn style_alert() -> Style { - style_fg(C_ALERT, Color::Red) -} - -fn style_stale() -> Style { - style_fg(C_STALE, Color::Yellow) -} - -fn style_border() -> Style { - style_fg(C_BORDER, Color::DarkGray) -} - -fn style_highlight() -> Style { - match color_mode() { + }; + let highlight = match mode { ColorMode::None => Style::default().add_modifier(Modifier::BOLD | Modifier::REVERSED), - ColorMode::Ansi16 => Style::default().add_modifier(Modifier::REVERSED | Modifier::BOLD), + ColorMode::Ansi16 => Style::default() + .add_modifier(Modifier::REVERSED | Modifier::BOLD), ColorMode::Rgb => Style::default() .bg(C_HL_BG) .fg(C_ACCENT) .add_modifier(Modifier::BOLD), + }; + TokenStyles { + text: rgb(C_TEXT, Color::White), + dim: rgb(C_DIM, Color::DarkGray), + accent, + alert: rgb(C_ALERT, Color::Red), + stale: rgb(C_STALE, Color::Yellow), + border: rgb(C_BORDER, Color::DarkGray), + highlight, } } + +fn style_accent() -> Style { + tokens_for(color_mode()).accent +} + +fn style_text() -> Style { + tokens_for(color_mode()).text +} + +fn style_dim() -> Style { + tokens_for(color_mode()).dim +} + +fn style_alert() -> Style { + tokens_for(color_mode()).alert +} + +fn style_stale() -> Style { + tokens_for(color_mode()).stale +} + +fn style_border() -> Style { + tokens_for(color_mode()).border +} + +fn style_highlight() -> Style { + tokens_for(color_mode()).highlight +} + /// How long the TUI waits for a key before redrawing (so background send can update status). pub const TUI_POLL: Duration = Duration::from_millis(100); diff --git a/tests/tui_spec.rs b/tests/tui_spec.rs index a04517d..2cec5c3 100644 --- a/tests/tui_spec.rs +++ b/tests/tui_spec.rs @@ -796,3 +796,98 @@ async fn renders_a_queued_row_from_a_real_outbox_send() { assert!(delivered[0].contains('✓'), "{:?}", delivered[0]); assert_eq!(summary.note(), None); } + +// --------------------------------------------------------------------------- +// Colour token set (card t_49b7c2b1) +// --------------------------------------------------------------------------- + +use onionwire::tui::{ColorMode, tokens_for}; +use ratatui::style::{Color, Modifier}; + +#[test] +fn rgb_tokens_resolve_to_palette_fg() { + let t = tokens_for(ColorMode::Rgb); + assert_eq!(t.text.fg, Some(Color::Rgb(196, 214, 176))); + assert_eq!(t.dim.fg, Some(Color::Rgb(98, 110, 92))); + assert_eq!(t.accent.fg, Some(Color::Rgb(232, 165, 75))); + assert_eq!(t.alert.fg, Some(Color::Rgb(220, 80, 70))); + assert_eq!(t.stale.fg, Some(Color::Rgb(210, 180, 80))); + assert_eq!(t.border.fg, Some(Color::Rgb(72, 84, 64))); + // highlight uses bg + fg + bold in RGB mode + assert_eq!(t.highlight.fg, Some(Color::Rgb(232, 165, 75))); + assert_eq!(t.highlight.bg, Some(Color::Rgb(48, 42, 28))); + assert!( + t.highlight + .add_modifier + .contains(Modifier::BOLD), + "RGB highlight must be bold" + ); +} + +#[test] +fn none_tokens_strip_colour_fg_and_alert_keeps_bold() { + let t = tokens_for(ColorMode::None); + assert_eq!(t.text.fg, None, "None mode must strip all colour"); + assert_eq!(t.dim.fg, None); + assert_eq!(t.alert.fg, None); + assert_eq!(t.border.fg, None); + assert_eq!(t.stale.fg, None); + // accent falls back to bold (no colour) in None mode + assert_eq!( + t.accent.add_modifier, + Modifier::BOLD, + "None accent must be bold-only" + ); + // highlight falls back to bold + reversed + assert_eq!( + t.highlight.add_modifier, + Modifier::BOLD | Modifier::REVERSED, + "None highlight must be bold+reversed" + ); + assert_eq!(t.highlight.fg, None); + assert_eq!(t.highlight.bg, None); +} + +#[test] +fn ansi16_tokens_use_ansi_colours() { + let t = tokens_for(ColorMode::Ansi16); + assert_eq!(t.text.fg, Some(Color::White)); + assert_eq!(t.dim.fg, Some(Color::DarkGray)); + assert_eq!(t.alert.fg, Some(Color::Red)); + assert_eq!(t.border.fg, Some(Color::DarkGray)); + assert_eq!(t.stale.fg, Some(Color::Yellow)); + assert_eq!(t.accent.fg, Some(Color::Yellow)); + // highlight in Ansi16 has no colour, just modifiers + assert_eq!(t.highlight.fg, None); + assert_eq!(t.highlight.bg, None); + assert!( + t.highlight + .add_modifier + .contains(Modifier::REVERSED | Modifier::BOLD) + ); +} + +#[test] +fn tokens_for_is_deterministic_across_calls() { + let a = tokens_for(ColorMode::Rgb); + let b = tokens_for(ColorMode::Rgb); + assert_eq!(a.text.fg, b.text.fg); + assert_eq!(a.accent.fg, b.accent.fg); + assert_eq!(a.highlight.fg, b.highlight.fg); +} + +#[test] +fn alert_line_prefixes_when_no_color() { + // NO_COLOR → ColorMode::None → alert_line prepends "! " + let _guard = std::env::var_os("NO_COLOR"); + unsafe { std::env::set_var("NO_COLOR", "1") }; + let line = alert_line("peer down", 80); + assert!(line.starts_with("! "), "expected '! ' prefix in None mode: {line:?}"); + unsafe { + if _guard.is_none() { + std::env::remove_var("NO_COLOR"); + } else { + std::env::set_var("NO_COLOR", _guard.unwrap()); + } + }; +} -- 2.52.0 From d4b0071d1ac26027e320c6d5ee85965a5eb2d413 Mon Sep 17 00:00:00 2001 From: agent Date: Tue, 15 Sep 2026 17:34:59 -0400 Subject: [PATCH 2/2] tui: fix clippy unnecessary_unwrap in NO_COLOR cleanup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The alert_line_prefixes_when_no_color test stashed the prior NO_COLOR value and restored it with is_none() + unwrap() — a clippy lint failure under -D warnings. Replace with a match that takes ownership. No behavior change; the restored env var is identical. Co-authored-by: openhands --- tests/tui_spec.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tests/tui_spec.rs b/tests/tui_spec.rs index 2cec5c3..549b63b 100644 --- a/tests/tui_spec.rs +++ b/tests/tui_spec.rs @@ -884,10 +884,9 @@ fn alert_line_prefixes_when_no_color() { let line = alert_line("peer down", 80); assert!(line.starts_with("! "), "expected '! ' prefix in None mode: {line:?}"); unsafe { - if _guard.is_none() { - std::env::remove_var("NO_COLOR"); - } else { - std::env::set_var("NO_COLOR", _guard.unwrap()); + match _guard { + Some(v) => std::env::set_var("NO_COLOR", v), + None => std::env::remove_var("NO_COLOR"), } }; } -- 2.52.0