Merge pull request 'tui: extract pub ColorMode/TokenStyles/tokens_for for testable colour tokens' (#80) from wt/t_49b7c2b1 into main
Reviewed-on: #80
This commit is contained in:
commit
3eee27fd09
2 changed files with 182 additions and 36 deletions
150
src/tui.rs
150
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_HL_BG: Color = Color::Rgb(48, 42, 28);
|
||||||
const C_BORDER: Color = Color::Rgb(72, 84, 64);
|
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.
|
/// ASCII chrome when `ONIONWIRE_ASCII` or `NO_COLOR` is set. No extra crate.
|
||||||
pub fn ascii_chrome() -> bool {
|
pub fn ascii_chrome() -> bool {
|
||||||
|
|
@ -340,53 +324,121 @@ fn truncate_chars(s: &str, max: usize) -> String {
|
||||||
format!("{head}{e}")
|
format!("{head}{e}")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn style_fg(rgb: Color, ansi: Color) -> Style {
|
/// Terminal colour capability, detected from the environment.
|
||||||
match color_mode() {
|
///
|
||||||
ColorMode::None => Style::default(),
|
/// `ColorMode::None` means “strip all colour” (e.g. `NO_COLOR`); the TUI then
|
||||||
ColorMode::Ansi16 => Style::default().fg(ansi),
|
/// falls back to ASCII chrome and a `! ` prefix on alerts so signal survives.
|
||||||
ColorMode::Rgb => Style::default().fg(rgb),
|
/// `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 {
|
/// All UI styles resolved for a single `ColorMode`. Tests construct this
|
||||||
match color_mode() {
|
/// 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::None => Style::default().add_modifier(Modifier::BOLD),
|
||||||
ColorMode::Ansi16 => Style::default().fg(Color::Yellow),
|
ColorMode::Ansi16 => Style::default().fg(Color::Yellow),
|
||||||
ColorMode::Rgb => Style::default().fg(C_ACCENT),
|
ColorMode::Rgb => Style::default().fg(C_ACCENT),
|
||||||
}
|
};
|
||||||
}
|
let highlight = match mode {
|
||||||
|
|
||||||
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() {
|
|
||||||
ColorMode::None => Style::default().add_modifier(Modifier::BOLD | Modifier::REVERSED),
|
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()
|
ColorMode::Rgb => Style::default()
|
||||||
.bg(C_HL_BG)
|
.bg(C_HL_BG)
|
||||||
.fg(C_ACCENT)
|
.fg(C_ACCENT)
|
||||||
.add_modifier(Modifier::BOLD),
|
.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).
|
/// 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);
|
pub const TUI_POLL: Duration = Duration::from_millis(100);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -796,3 +796,97 @@ async fn renders_a_queued_row_from_a_real_outbox_send() {
|
||||||
assert!(delivered[0].contains('✓'), "{:?}", delivered[0]);
|
assert!(delivered[0].contains('✓'), "{:?}", delivered[0]);
|
||||||
assert_eq!(summary.note(), None);
|
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 {
|
||||||
|
match _guard {
|
||||||
|
Some(v) => std::env::set_var("NO_COLOR", v),
|
||||||
|
None => std::env::remove_var("NO_COLOR"),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue