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.
This commit is contained in:
parent
e3a051a980
commit
6ce97a0e3c
2 changed files with 183 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_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);
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue