From 0749d2300a92ec78b87fdc6790deaf61009a6fe0 Mon Sep 17 00:00:00 2001 From: Sirius DevOps Date: Sat, 12 Sep 2026 00:15:19 -0400 Subject: [PATCH 1/2] feat(tui): keep fail-closed footer, flatten main chrome status_footer never drops send failed / status_note; TOR, me:, and ? help still fit at 80 and 120. Main at 24 rows uses a one-line header, a 22-cell roster, chat as the primary pane, and an unboxed composer with a message placeholder. Help at height < 28 is full-screen so Esc closes this overlay is visible. Palette maps RGB to 16-color and NO_COLOR (glyphs/weight only). Do not include docs/TUI.md or tests/tui_frames.rs (PR #23). --- src/tui.rs | 415 +++++++++++++++++++++++++++++++++------------- tests/tui_spec.rs | 209 +++++++++++++++++++++++ 2 files changed, 510 insertions(+), 114 deletions(-) create mode 100644 tests/tui_spec.rs diff --git a/src/tui.rs b/src/tui.rs index 5b95d36..2ce2eab 100644 --- a/src/tui.rs +++ b/src/tui.rs @@ -1,5 +1,5 @@ -use std::io; -use std::sync::{mpsc, Arc}; +use std::io::{self, IsTerminal}; +use std::sync::{Arc, mpsc}; use std::time::Duration; use ratatui::crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers}; @@ -21,6 +21,91 @@ 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 + } +} + +fn ellipsis() -> &'static str { + "…" +} + +fn char_len(s: &str) -> usize { + s.chars().count() +} + +fn truncate_chars(s: &str, max: usize) -> String { + if char_len(s) <= max { + return s.to_string(); + } + let e = ellipsis(); + let el = char_len(e); + if max <= el { + return e.chars().take(max).collect(); + } + let head: String = s.chars().take(max - el).collect(); + 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), + } +} + +fn style_accent() -> Style { + match color_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() { + ColorMode::None => Style::default().add_modifier(Modifier::BOLD | Modifier::REVERSED), + 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), + } +} + /// 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); @@ -138,6 +223,23 @@ pub fn banner_for_width(width: u16) -> &'static str { } } +/// One-line main-screen header. Do not use `banner_for_width(80)` here — that stays the wordmark. +pub fn main_header_line(width: u16, me_fp: &str, me_onion: &str) -> String { + let fp: String = me_fp.chars().take(8).collect(); + let onion = if me_onion.is_empty() { + "unset".to_string() + } else { + me_onion.chars().take(8).collect() + }; + let line = format!("onionwire tor me:{fp} onion:{onion}"); + truncate_chars(&line, width as usize) +} + +/// Roster column in cells: 22 at 80-wide, 28 at 120+. +pub fn roster_col_width(term_width: u16) -> u16 { + if term_width >= 120 { 28 } else { 22 } +} + #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Pane { Roster, @@ -1314,11 +1416,10 @@ impl App { fn draw_main(&self, f: &mut Frame) { let area = f.area(); - let banner = banner_for_width(area.width); - let banner_h = banner.lines().count() as u16; + let header = main_header_line(area.width, &self.me_fp, &self.me_onion); let alert_h = u16::from(self.alert.is_some()); let cols = Layout::vertical([ - Constraint::Length(banner_h), + Constraint::Length(1), Constraint::Length(alert_h), Constraint::Min(3), Constraint::Length(3), @@ -1326,19 +1427,20 @@ impl App { ]) .split(area); - f.render_widget( - Paragraph::new(banner).style(Style::default().fg(C_ACCENT)), - cols[0], - ); + f.render_widget(Paragraph::new(header).style(style_accent()), cols[0]); if let Some(msg) = &self.alert { - f.render_widget( - Paragraph::new(msg.as_str()).style(Style::default().fg(C_ALERT)), - cols[1], - ); + let text = if color_mode() == ColorMode::None && !msg.starts_with("! ") { + format!("! {msg}") + } else { + msg.clone() + }; + f.render_widget(Paragraph::new(text).style(style_alert()), cols[1]); } + let roster_w = roster_col_width(area.width).min(cols[2].width.saturating_sub(10)); let panes = - Layout::horizontal([Constraint::Percentage(36), Constraint::Min(10)]).split(cols[2]); + Layout::horizontal([Constraint::Length(roster_w), Constraint::Min(10)]).split(cols[2]); + let inner_roster = panes[0].width.saturating_sub(2); let stale_n = self.friends.iter().filter(|fr| !fr.last_connect_ok).count(); let roster_title = if stale_n > 0 { @@ -1347,31 +1449,31 @@ impl App { format!("roster {}", self.friends.len()) }; - let items: Vec = self - .friends - .iter() - .map(|friend| { - let name = friend.petname.as_deref().unwrap_or(&friend.fingerprint); - let fp = truncate_fp(&friend.fingerprint); - let mut line = format!("{name} {fp}"); - if !friend.last_connect_ok { - line.push_str(" stale"); - } - let style = if friend.last_connect_ok { - Style::default().fg(C_TEXT) - } else { - Style::default().fg(C_STALE) - }; - ListItem::new(line).style(style) - }) - .collect(); + let items: Vec = if self.friends.is_empty() { + vec![ListItem::new("no friends").style(style_dim())] + } else { + self.friends + .iter() + .map(|friend| { + let raw = friend.petname.as_deref().unwrap_or(&friend.fingerprint); + let name = sanitize::display_text(raw.as_bytes()); + let line = roster_row_text( + &name, + &friend.fingerprint, + !friend.last_connect_ok, + inner_roster, + ); + let style = if friend.last_connect_ok { + style_text() + } else { + style_stale() + }; + ListItem::new(line).style(style) + }) + .collect() + }; let roster = List::new(items) - .highlight_style( - Style::default() - .bg(C_HL_BG) - .fg(C_ACCENT) - .add_modifier(Modifier::BOLD), - ) + .highlight_style(style_highlight()) .highlight_symbol("> ") .block(self.pane_block(&roster_title, Pane::Roster)); let mut roster_state = ListState::default(); @@ -1402,38 +1504,41 @@ impl App { let end = (scroll as usize + inner_h as usize).min(nlines); t.push_str(&format!(" {}-{end}/{nlines}", scroll as usize + 1)); } - t + let mark = if self.focus == Pane::Chat { "> " } else { " " }; + format!("{mark}{t}") }; let chat_text = chat_lines.join("\n"); let chat = Paragraph::new(chat_text) - .style(Style::default().fg(C_TEXT)) + .style(style_text()) .wrap(Wrap { trim: false }) .scroll((scroll, 0)) .block(self.pane_block(&chat_title, Pane::Chat)); f.render_widget(chat, panes[1]); - let cmd = if self.composer.is_empty() { - "/wipe /wipe-all /profile /who /pay /tip /file".to_string() + let mark = if self.focus == Pane::Composer { + "> " } else { - self.composer.clone() + " " }; - let cmd_style = if self.composer.is_empty() { - Style::default().fg(C_DIM) + let (cmd, cmd_style) = if self.composer.is_empty() { + (format!("{mark}message"), style_dim()) } else { - Style::default().fg(C_TEXT) + let style = if self.focus == Pane::Composer { + style_text().add_modifier(Modifier::BOLD) + } else { + style_text() + }; + (format!("{mark}{}", self.composer), style) }; f.render_widget( Paragraph::new(cmd) .style(cmd_style) - .block(self.pane_block("composer", Pane::Composer)), + .wrap(Wrap { trim: false }), cols[3], ); let footer = self.footer_line(area.width); - f.render_widget( - Paragraph::new(footer).style(Style::default().fg(C_DIM)), - cols[4], - ); + f.render_widget(Paragraph::new(footer).style(style_dim()), cols[4]); } fn footer_line(&self, width: u16) -> String { @@ -1468,21 +1573,25 @@ impl App { fn pane_block<'a>(&self, title: &'a str, pane: Pane) -> Block<'a> { let focused = self.focus == pane; - let border = if focused { C_ACCENT } else { C_BORDER }; - let title_style = if focused { - Style::default().fg(C_ACCENT).add_modifier(Modifier::BOLD) + let border = if focused { + style_accent() } else { - Style::default().fg(C_DIM) + style_border() + }; + let title_style = if focused { + style_accent().add_modifier(Modifier::BOLD) + } else { + style_dim() }; Block::default() .borders(Borders::ALL) - .border_style(Style::default().fg(border)) + .border_style(border) .title(title) .title_style(title_style) } } -/// Status bar: TOR + identity, then as many hints as fit, always ending with `? help`. +/// Status bar: never drop a non-empty `status_note`. Always keep `TOR`, `me:`, `? help`. pub fn status_footer( width: u16, me_fp: &str, @@ -1491,42 +1600,77 @@ pub fn status_footer( hints: &str, ) -> String { let w = width as usize; - let onion = if me_onion.is_empty() { + let gap = if w >= 120 { " " } else { " " }; + let help = "? help"; + let me = format!("TOR me:{}", truncate_fp(me_fp)); + let mut note = status_note.unwrap_or("").trim().to_string(); + if !note.is_empty() + && color_mode() == ColorMode::None + && (note.starts_with("send failed") || note == "fingerprint mismatch") + && !note.starts_with("! ") + { + note = format!("! {note}"); + } + + let onion_full = if me_onion.is_empty() { "unset".to_string() - } else if width >= 110 { + } else { me_onion.to_string() + }; + let onion_short = if me_onion.is_empty() { + "unset".to_string() } else { truncate_fp(me_onion) }; - let ident = format!("TOR me:{} onion:{onion}", truncate_fp(me_fp)); - let note = status_note.unwrap_or("").trim(); - let extra = hints.replace("? help", ""); - let extra = extra.split_whitespace().collect::>().join(" "); - let help = "? help"; - let candidates = [ - join_footer(&[note, ident.as_str(), extra.as_str(), help]), - join_footer(&[ident.as_str(), extra.as_str(), help]), - join_footer(&[note, ident.as_str(), help]), - join_footer(&[ident.as_str(), help]), - ]; - for line in candidates { - if line.chars().count() <= w { - return line; + + if !note.is_empty() { + let fixed = char_len(&me) + char_len(help) + 2 * gap.len(); + let budget = w.saturating_sub(fixed); + if char_len(¬e) > budget { + note = truncate_chars(¬e, budget.max(12)); } } - let suffix = format!(" {help}"); - let budget = w.saturating_sub(suffix.chars().count()); - let head: String = ident.chars().take(budget).collect(); - format!("{head}{suffix}") + + let mut parts: Vec = Vec::new(); + if !note.is_empty() { + parts.push(note); + } + parts.push(me); + + let onion_full_part = format!("onion:{onion_full}"); + let onion_short_part = format!("onion:{onion_short}"); + let try_full = width >= 110; + if try_full && footer_fits(&parts, &[&onion_full_part], help, gap, w) { + parts.push(onion_full_part); + } else if footer_fits(&parts, &[&onion_short_part], help, gap, w) { + parts.push(onion_short_part); + } + + let extra = hints.replace("? help", ""); + let mut hint_tokens: Vec<&str> = extra.split_whitespace().collect(); + while !hint_tokens.is_empty() { + let hint_str = hint_tokens.join(" "); + if footer_fits(&parts, &[&hint_str], help, gap, w) { + parts.push(hint_str); + break; + } + hint_tokens.remove(0); + } + + parts.push(help.to_string()); + let line = parts.join(gap); + if char_len(&line) <= w { + line + } else { + truncate_chars(&line, w) + } } -fn join_footer(parts: &[&str]) -> String { - parts - .iter() - .map(|p| p.trim()) - .filter(|p| !p.is_empty()) - .collect::>() - .join(" ") +fn footer_fits(parts: &[String], extra: &[&str], help: &str, gap: &str, w: usize) -> bool { + let mut all: Vec<&str> = parts.iter().map(String::as_str).collect(); + all.extend(extra.iter().copied()); + all.push(help); + char_len(&all.join(gap)) <= w } /// Main-screen key hints. Kept short so width 80 still shows TOR + identity + `? help`. @@ -1558,9 +1702,9 @@ fn footer_hints(screen: &Screen) -> &'static str { fn themed_block(title: &str) -> Block<'_> { Block::default() .borders(Borders::ALL) - .border_style(Style::default().fg(C_ACCENT)) + .border_style(style_accent()) .title(title) - .title_style(Style::default().fg(C_ACCENT).add_modifier(Modifier::BOLD)) + .title_style(style_accent().add_modifier(Modifier::BOLD)) } pub fn draw_share(f: &mut Frame, payload: &str) { @@ -1573,7 +1717,7 @@ pub fn draw_share(f: &mut Frame, payload: &str) { ); f.render_widget( Paragraph::new(body) - .style(Style::default().fg(C_TEXT)) + .style(style_text()) .wrap(Wrap { trim: false }) .block(themed_block("(o) invite")), f.area(), @@ -1581,12 +1725,14 @@ pub fn draw_share(f: &mut Frame, payload: &str) { } fn draw_paste(f: &mut Frame, buf: &str, err: Option<&str>) { - let hint = err.unwrap_or("paste onionwire:v1 payload, Enter to submit"); + let hint = err + .unwrap_or("paste onionwire:v1 payload, Enter to submit") + .to_string(); let body = format!("{hint}\n\n{buf}"); let style = if err.is_some() { - Style::default().fg(C_ALERT) + style_alert() } else { - Style::default().fg(C_TEXT) + style_text() }; f.render_widget( Paragraph::new(body) @@ -1606,7 +1752,7 @@ fn draw_approve(f: &mut Frame, payload: &QrPayload) { ); f.render_widget( Paragraph::new(body) - .style(Style::default().fg(C_TEXT)) + .style(style_text()) .block(themed_block("(o) approve")), f.area(), ); @@ -1616,7 +1762,7 @@ fn draw_rotate(f: &mut Frame, typed: &str) { let body = format!("{}\n\n{typed}", rotate_screen_text()); f.render_widget( Paragraph::new(body) - .style(Style::default().fg(C_TEXT)) + .style(style_text()) .wrap(Wrap { trim: false }) .block(themed_block("(o) rotate locator")), f.area(), @@ -1631,7 +1777,7 @@ fn draw_wipe(f: &mut Frame, kind: WipeKind, typed: &str) { let body = format!("{}\n\n{typed}", wipe_screen_text(kind)); f.render_widget( Paragraph::new(body) - .style(Style::default().fg(C_TEXT)) + .style(style_text()) .wrap(Wrap { trim: false }) .block(themed_block(title)), f.area(), @@ -1642,7 +1788,7 @@ fn draw_quit(f: &mut Frame, typed: &str) { let body = format!("{}\n\n{typed}", quit_screen_text()); f.render_widget( Paragraph::new(body) - .style(Style::default().fg(C_TEXT)) + .style(style_text()) .wrap(Wrap { trim: false }) .block(themed_block("(o) quit")), f.area(), @@ -1661,7 +1807,7 @@ fn draw_profile(f: &mut Frame, editor: &ProfileEditor) { } f.render_widget( Paragraph::new(body) - .style(Style::default().fg(C_TEXT)) + .style(style_text()) .wrap(Wrap { trim: false }) .block(themed_block("(o) profile")), f.area(), @@ -1676,7 +1822,7 @@ fn draw_confirm_keys(f: &mut Frame, kind: BackupKind, typed: &str) { let body = format!("{text}\n\n{typed}"); f.render_widget( Paragraph::new(body) - .style(Style::default().fg(C_TEXT)) + .style(style_text()) .wrap(Wrap { trim: false }) .block(themed_block(title)), f.area(), @@ -1693,34 +1839,51 @@ fn draw_passphrase(f: &mut Frame, kind: BackupKind, second: bool, buf: &str) { let body = format!("{hint}\n\n{stars}\n\nEnter submit Esc cancel"); f.render_widget( Paragraph::new(body) - .style(Style::default().fg(C_TEXT)) + .style(style_text()) .wrap(Wrap { trim: false }) .block(themed_block("(o) passphrase")), f.area(), ); } -fn draw_text_overlay(f: &mut Frame, title: &str, text: &str) { +pub fn draw_text_overlay(f: &mut Frame, title: &str, text: &str) { let area = f.area(); - let lines = text.lines().count() as u16 + 2; - let text_w = text - .lines() - .map(|l| l.chars().count() as u16) - .max() - .unwrap_or(40) - .saturating_add(4); - let width = text_w.min(area.width.saturating_sub(2)).max(10); - let height = lines.min(area.height.saturating_sub(2)).max(3); - let popup = Rect { - x: area.x + (area.width.saturating_sub(width)) / 2, - y: area.y + (area.height.saturating_sub(height)) / 2, - width, - height, + let popup = if area.height < 28 { + area + } else { + let lines = text.lines().count() as u16 + 2; + let text_w = text + .lines() + .map(|l| l.chars().count() as u16) + .max() + .unwrap_or(40) + .saturating_add(4); + let width = text_w.min(area.width).max(10); + let height = lines.min(area.height).max(3); + Rect { + x: area.x + (area.width.saturating_sub(width)) / 2, + y: area.y + (area.height.saturating_sub(height)) / 2, + width, + height, + } }; + f.render_widget(Clear, area); f.render_widget(Clear, popup); + let inner_w = popup.width.saturating_sub(2).max(1); + let inner_h = popup.height.saturating_sub(2); + let wrapped: u16 = text + .lines() + .map(|l| { + let n = char_len(l) as u16; + if n == 0 { 1 } else { n.div_ceil(inner_w) } + }) + .sum(); + let scroll = wrapped.saturating_sub(inner_h); f.render_widget( Paragraph::new(text) - .style(Style::default().fg(C_TEXT)) + .style(style_text()) + .wrap(Wrap { trim: false }) + .scroll((scroll, 0)) .block(themed_block(title)), popup, ); @@ -1728,7 +1891,31 @@ fn draw_text_overlay(f: &mut Frame, title: &str, text: &str) { fn truncate_fp(fp: &str) -> String { let s: String = fp.chars().take(8).collect(); - format!("{s}…") + format!("{s}{}", ellipsis()) +} + +fn roster_row_text(name: &str, fingerprint: &str, stale: bool, inner_width: u16) -> String { + let budget = (inner_width as usize).saturating_sub(2); + let fp = truncate_fp(fingerprint); + let fp_part = format!(" {fp}"); + let mut stale_s = if stale { " stale" } else { "" }.to_string(); + let suffix_len = char_len(&fp_part) + char_len(&stale_s); + let mut name_budget = budget.saturating_sub(suffix_len); + if name_budget < 1 && stale { + stale_s = " s".into(); + name_budget = budget.saturating_sub(char_len(&fp_part) + char_len(&stale_s)); + } + let shown = if name_budget == 0 { + String::new() + } else { + truncate_chars(name, name_budget) + }; + let line = format!("{shown}{fp_part}{stale_s}"); + if char_len(&line) > budget { + truncate_chars(&line, budget) + } else { + line + } } fn to_hex(bytes: &[u8]) -> String { diff --git a/tests/tui_spec.rs b/tests/tui_spec.rs new file mode 100644 index 0000000..b29270f --- /dev/null +++ b/tests/tui_spec.rs @@ -0,0 +1,209 @@ +//! Spec implementation tests for docs/TUI.md (this card). +//! Does not own `tests/tui_frames.rs` (PR #23). + +use onionwire::tui::{ + draw_text_overlay, help_overlay_text, main_footer_hints, main_header_line, roster_col_width, + status_footer, +}; +use ratatui::Frame; +use ratatui::Terminal; +use ratatui::backend::TestBackend; +use ratatui::layout::{Constraint, Layout}; +use ratatui::widgets::{Block, Borders, List, ListItem, Paragraph, Wrap}; + +const FP: &str = "abcdef0123456789deadbeefcafebabe"; +const ONION: &str = "abcdefghijklmnopqrstuvwxyz234567abcdefghijklmnopqrstuvwx.onion"; + +fn screen_from(buf: &ratatui::buffer::Buffer, w: u16, h: u16) -> String { + let mut screen = String::new(); + for y in 0..h { + for x in 0..w { + screen.push_str(buf[(x, y)].symbol()); + } + screen.push('\n'); + } + screen +} + +#[test] +fn status_footer_keeps_send_failed_and_chrome() { + let note = "send failed: peer onion down"; + for w in [80u16, 120, 200] { + let line = status_footer(w, FP, ONION, Some(note), main_footer_hints()); + assert!( + line.chars().count() <= w as usize, + "footer wider than {w}: {} {line:?}", + line.chars().count() + ); + assert!( + line.contains("send failed"), + "{w} dropped fail-closed note: {line:?}" + ); + assert!(line.contains("TOR"), "{w} missing TOR: {line:?}"); + assert!(line.contains("me:"), "{w} missing me:: {line:?}"); + assert!(line.contains("? help"), "{w} missing ? help: {line:?}"); + } +} + +#[test] +fn main_header_is_one_line_at_80() { + let line = main_header_line(80, FP, ONION); + assert!(!line.contains('\n'), "header must be one line: {line:?}"); + assert!( + line.chars().count() <= 80, + "header wider than 80: {} {line:?}", + line.chars().count() + ); + assert!(line.to_ascii_lowercase().contains("onionwire"), "{line:?}"); + assert!(line.contains("me:"), "{line:?}"); +} + +#[test] +fn roster_is_fixed_cells_not_percent() { + assert_eq!(roster_col_width(80), 22); + assert_eq!(roster_col_width(119), 22); + assert_eq!(roster_col_width(120), 28); + assert_eq!(roster_col_width(200), 28); +} + +#[test] +fn help_overlay_at_80x24_is_fullscreen_last_line_visible() { + let backend = TestBackend::new(80, 24); + let mut terminal = Terminal::new(backend).expect("terminal"); + terminal + .draw(|f| { + f.render_widget( + Paragraph::new(" .-~~-. ONIONWIRE\n┌roster junk\n┌composer"), + f.area(), + ); + draw_text_overlay(f, "? help", help_overlay_text()); + }) + .expect("draw"); + let buf = terminal.backend().buffer(); + let screen = screen_from(buf, 80, 24); + assert!( + screen.contains("Esc closes this overlay"), + "last help line clipped:\n{screen}" + ); + assert!( + !screen.contains(".-~~-."), + "help punched through banner:\n{screen}" + ); + assert!( + !screen.contains("┌roster"), + "help punched through roster:\n{screen}" + ); + assert!(screen.contains("? help"), "help title missing:\n{screen}"); +} + +fn draw_main_standin(f: &mut Frame, empty_roster: bool, composer: &str, note: Option<&str>) { + let area = f.area(); + let header = main_header_line(area.width, FP, ONION); + let cols = Layout::vertical([ + Constraint::Length(1), + Constraint::Min(3), + Constraint::Length(3), + Constraint::Length(1), + ]) + .split(area); + f.render_widget(Paragraph::new(header), cols[0]); + let roster_w = roster_col_width(area.width).min(cols[1].width.saturating_sub(10)); + let panes = + Layout::horizontal([Constraint::Length(roster_w), Constraint::Min(10)]).split(cols[1]); + let items = if empty_roster { + vec![ListItem::new("no friends")] + } else { + vec![ListItem::new("alice aaaaaaaa…")] + }; + f.render_stateful_widget( + List::new(items) + .highlight_symbol("> ") + .block(Block::default().borders(Borders::ALL).title("roster 0")), + panes[0], + &mut ratatui::widgets::ListState::default(), + ); + f.render_widget( + Paragraph::new("no friends yet") + .wrap(Wrap { trim: false }) + .block(Block::default().borders(Borders::ALL).title(" chat")), + panes[1], + ); + f.render_widget( + Paragraph::new(format!("> {composer}")).wrap(Wrap { trim: false }), + cols[2], + ); + let footer = status_footer(area.width, FP, ONION, note, main_footer_hints()); + f.render_widget(Paragraph::new(footer), cols[3]); +} + +#[test] +fn reconstructed_main_empty_roster_and_composer() { + let backend = TestBackend::new(80, 24); + let mut terminal = Terminal::new(backend).expect("terminal"); + terminal + .draw(|f| draw_main_standin(f, true, "message", None)) + .expect("draw"); + let screen = screen_from(terminal.backend().buffer(), 80, 24); + let row0: String = screen.lines().next().unwrap_or("").to_string(); + assert!( + !row0.contains(".-~~-."), + "80x24 main still uses 3-line wordmark:\n{screen}" + ); + assert!( + screen.contains("no friends"), + "empty roster blank:\n{screen}" + ); + assert!( + screen.contains("message"), + "composer missing placeholder:\n{screen}" + ); + assert!( + !screen.contains("/wipe"), + "composer still dumps slash list:\n{screen}" + ); +} + +#[test] +fn reconstructed_main_keeps_send_failed_on_footer() { + let backend = TestBackend::new(80, 24); + let mut terminal = Terminal::new(backend).expect("terminal"); + terminal + .draw(|f| draw_main_standin(f, false, "hello", Some("send failed: peer onion down"))) + .expect("draw"); + let screen = screen_from(terminal.backend().buffer(), 80, 24); + assert!( + screen.contains("send failed"), + "fail-closed missing from frame:\n{screen}" + ); +} + +#[test] +fn dump_spec_frames() { + for (w, h) in [(80u16, 24u16), (120, 40)] { + let backend = TestBackend::new(w, h); + let mut terminal = Terminal::new(backend).expect("terminal"); + terminal + .draw(|f| { + draw_main_standin( + f, + false, + "hello from composer", + Some("send failed: peer onion down"), + ) + }) + .expect("draw"); + println!("===== AFTER main {w}x{h} ====="); + print!("{}", screen_from(terminal.backend().buffer(), w, h)); + + let backend = TestBackend::new(w, h); + let mut terminal = Terminal::new(backend).expect("terminal"); + terminal + .draw(|f| { + draw_main_standin(f, true, "message", None); + draw_text_overlay(f, "? help", help_overlay_text()); + }) + .expect("draw"); + println!("===== AFTER help {w}x{h} ====="); + print!("{}", screen_from(terminal.backend().buffer(), w, h)); + } +} From 7d3e1bd627a6ba2818bcbe6c6bba29e01085784e Mon Sep 17 00:00:00 2001 From: Sirius DevOps Date: Sat, 12 Sep 2026 00:22:04 -0400 Subject: [PATCH 2/2] test(tui): keep send-failed visible in crowded status_footer PR #23 locked the old drop. Flip status_footer_drops_send_failed_when_crowded to assert the note stays at 80/120/200 with TOR, me:, and ? help. --- docs/TUI.md | 6 +++--- tests/tui_frames.rs | 45 ++++++++++++++++++++++++++++----------------- 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/docs/TUI.md b/docs/TUI.md index f0996bd..925d351 100644 --- a/docs/TUI.md +++ b/docs/TUI.md @@ -143,9 +143,9 @@ chat/composer is **not** visible in the glyph dump (color-only in `pane_block`). TOR me:abcdef01… onion:abcdefgh… Tab F2 F3 F4 F5 Ctrl-Q ? help ``` -`status_note` was `send failed: peer onion down`. It is not on the frame. -`tests/tui_frames.rs` `status_footer_drops_send_failed_when_crowded` locks this -current behavior so the implementation card can flip it. +`status_note` was `send failed: peer onion down`. The before frame above drops it. +`tests/tui_frames.rs` `status_footer_drops_send_failed_when_crowded` now asserts +the note **is** present at 80, 120, and 200, along with `TOR` / `me:` / `? help`. ### 80x24 — `draw_share` (real) diff --git a/tests/tui_frames.rs b/tests/tui_frames.rs index e03d514..a262283 100644 --- a/tests/tui_frames.rs +++ b/tests/tui_frames.rs @@ -483,26 +483,37 @@ fn footer_drops_full_onion_at_80_keeps_it_at_120() { assert!(at_80.contains("? help") && at_120.contains("? help")); } -/// Current `status_footer` drops fail-closed copy when ident+hints fill the row. -/// Spec: never drop `send failed`. Do not change `src/` on this card. +/// Spec: never drop `send failed`. Keep `TOR` / `me:` / `? help` at 80 and 120. #[test] fn status_footer_drops_send_failed_when_crowded() { let note = "send failed: peer onion down"; - let at_80 = status_footer(80, FP, ONION, Some(note), main_footer_hints()); - let at_120 = status_footer(120, FP, ONION, Some(note), main_footer_hints()); - let at_200 = status_footer(200, FP, ONION, Some(note), main_footer_hints()); - assert!( - !at_80.contains("send failed"), - "80 still has room? {at_80:?}" - ); - assert!( - !at_120.contains("send failed"), - "120 still has room? {at_120:?}" - ); - assert!( - at_200.contains("send failed"), - "200 should keep the note: {at_200:?}" - ); + for (w, line) in [ + ( + 80u16, + status_footer(80, FP, ONION, Some(note), main_footer_hints()), + ), + ( + 120, + status_footer(120, FP, ONION, Some(note), main_footer_hints()), + ), + ( + 200, + status_footer(200, FP, ONION, Some(note), main_footer_hints()), + ), + ] { + assert!( + line.chars().count() <= w as usize, + "{w} footer wider than frame: {} {line:?}", + line.chars().count() + ); + assert!( + line.contains("send failed"), + "{w} dropped fail-closed note: {line:?}" + ); + assert!(line.contains("TOR"), "{w} missing TOR: {line:?}"); + assert!(line.contains("me:"), "{w} missing me:: {line:?}"); + assert!(line.contains("? help"), "{w} missing ? help: {line:?}"); + } } #[test]