Merge origin/main into the peer-payments branch
main moved under this PR when #120 (clean-look chrome redesign) landed, which touched the same three files (src/tui.rs, tests/tui_spec.rs, docs/TUI.md) and left PR #121 unmergeable. - docs/TUI.md: both sections kept — the peer-payment-history section from this branch, then #120's chrome-redesign section. - tests/tui_spec.rs: #120's `fit_cells` flake relaxation kept (it is a superset of the one on this branch: same cut point plus the ellipsis assertion). - src/tui.rs merged cleanly; `cargo test --locked` 383 passed / 0 failed and `cargo clippy --locked --all-targets -- -D warnings` clean afterwards. The peer-payments frames are byte-identical after the merge, so the dumps in this PR body still hold.
This commit is contained in:
commit
cbe889024b
5 changed files with 1343 additions and 475 deletions
759
src/tui.rs
759
src/tui.rs
|
|
@ -12,6 +12,7 @@ use ratatui::widgets::{Block, Borders, Clear, List, ListItem, ListState, Paragra
|
|||
use ratatui::{DefaultTerminal, Frame};
|
||||
use zeroize::{Zeroize, Zeroizing};
|
||||
|
||||
use crate::hs::TorState;
|
||||
use crate::node::Node;
|
||||
use crate::outbox::DEFAULT_MAX_ATTEMPTS;
|
||||
use crate::qr::{self, QrPayload};
|
||||
|
|
@ -49,12 +50,15 @@ fn ellipsis() -> &'static str {
|
|||
}
|
||||
|
||||
/// Truncate or keep an alert so it cannot spill into the roster row.
|
||||
///
|
||||
/// Cells, not chars: the message can carry a peer-supplied transport error, and
|
||||
/// a CJK/emoji byte there must not push the alert into the next pane.
|
||||
pub fn alert_line(msg: &str, width: u16) -> String {
|
||||
let mut text = msg.to_string();
|
||||
if color_mode() == ColorMode::None && !text.starts_with("! ") {
|
||||
text = format!("! {text}");
|
||||
}
|
||||
truncate_chars(&text, width as usize)
|
||||
fit_cells(&text, width as usize)
|
||||
}
|
||||
|
||||
/// Wrap an invite at `:` so a mouse-copy cannot lose a mid-hex continuation.
|
||||
|
|
@ -172,7 +176,7 @@ pub fn short_send_reason(why: &str, budget: usize) -> String {
|
|||
let clean = sanitize::display_text(why.as_bytes());
|
||||
let tail = clean.rsplit(": ").next().unwrap_or("").trim();
|
||||
let text = if tail.is_empty() { clean.trim() } else { tail };
|
||||
truncate_chars(text, budget)
|
||||
fit_cells(text, budget)
|
||||
}
|
||||
|
||||
/// One row's delivery marker, straight from the outbox state.
|
||||
|
|
@ -340,19 +344,6 @@ 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}")
|
||||
}
|
||||
|
||||
/// Terminal colour capability, detected from the environment.
|
||||
///
|
||||
/// `ColorMode::None` means “strip all colour” (e.g. `NO_COLOR`); the TUI then
|
||||
|
|
@ -606,14 +597,67 @@ 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 {
|
||||
main_header_line_phased(width, me_fp, me_onion, None)
|
||||
}
|
||||
|
||||
/// One-line main-screen header carrying the Tor phase. `None` keeps the
|
||||
/// historic string; `Some` puts the phase word and glyph between the app name
|
||||
/// and the identity.
|
||||
pub fn main_header_line_phased(
|
||||
width: u16,
|
||||
me_fp: &str,
|
||||
me_onion: &str,
|
||||
phase: Option<TorPhase>,
|
||||
) -> String {
|
||||
fit_cells(&header_plain(me_fp, me_onion, phase), width as usize)
|
||||
}
|
||||
|
||||
/// The header text without a width: one place builds the string, so the styled
|
||||
/// spans and the plain helper can never drift apart.
|
||||
fn header_plain(me_fp: &str, me_onion: &str, phase: Option<TorPhase>) -> 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)
|
||||
let tor = match phase {
|
||||
None => "tor".to_string(),
|
||||
Some(p) => format!("{} tor {}", p.glyph(), p.word()),
|
||||
};
|
||||
format!("onionwire {tor} me:{fp} onion:{onion}")
|
||||
}
|
||||
|
||||
/// The header as spans. The app name is plain text (not accent — the accent is
|
||||
/// reserved for focus), the identity is muted, and only the phase carries a
|
||||
/// semantic colour. Too narrow to lay out in parts, the line degrades to one
|
||||
/// truncated muted span rather than clipping mid-field.
|
||||
pub fn main_header_spans(
|
||||
width: u16,
|
||||
me_fp: &str,
|
||||
me_onion: &str,
|
||||
phase: Option<TorPhase>,
|
||||
) -> Vec<Span<'static>> {
|
||||
let plain = header_plain(me_fp, me_onion, phase);
|
||||
if cell_width(&plain) > width as usize {
|
||||
return vec![Span::raw(fit_cells(&plain, width as usize)).style(style_dim())];
|
||||
}
|
||||
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 (tor_text, tor_style) = match phase {
|
||||
None => ("tor".to_string(), style_dim()),
|
||||
Some(p) => (format!("{} tor {}", p.glyph(), p.word()), p.style()),
|
||||
};
|
||||
vec![
|
||||
Span::raw("onionwire").style(style_text().add_modifier(Modifier::BOLD)),
|
||||
Span::raw(" "),
|
||||
Span::raw(tor_text).style(tor_style),
|
||||
Span::raw(format!(" me:{fp} onion:{onion}")).style(style_dim()),
|
||||
]
|
||||
}
|
||||
|
||||
/// Roster column in cells: 22 at 80-wide, 28 at 120+.
|
||||
|
|
@ -2476,11 +2520,12 @@ impl App {
|
|||
Screen::WalletsSettings { state } => {
|
||||
// The one status line the screen carries: same `status_footer`
|
||||
// shape as Main, so TOR / identity / the copy confirmation read
|
||||
// in the same place on every screen.
|
||||
let footer = status_footer(
|
||||
// in the same place on every screen — including the phase.
|
||||
let footer = status_footer_phased(
|
||||
f.area().width,
|
||||
&self.me_fp,
|
||||
&self.me_onion,
|
||||
Some(TorPhase::of(self.node.tor_state())),
|
||||
self.status_note.as_deref(),
|
||||
footer_hints(&self.screen),
|
||||
);
|
||||
|
|
@ -2505,70 +2550,9 @@ impl App {
|
|||
}
|
||||
|
||||
fn draw_main(&self, f: &mut Frame) {
|
||||
let area = f.area();
|
||||
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(1),
|
||||
Constraint::Length(alert_h),
|
||||
Constraint::Min(3),
|
||||
Constraint::Length(3),
|
||||
Constraint::Length(1),
|
||||
])
|
||||
.split(area);
|
||||
|
||||
f.render_widget(Paragraph::new(header).style(style_accent()), cols[0]);
|
||||
if let Some(msg) = &self.alert {
|
||||
let text = alert_line(msg, area.width);
|
||||
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::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 {
|
||||
format!("roster {} {stale_n} stale", self.friends.len())
|
||||
} else {
|
||||
format!("roster {}", self.friends.len())
|
||||
};
|
||||
|
||||
let items: Vec<ListItem> = 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_highlight())
|
||||
.highlight_symbol("> ")
|
||||
.block(self.pane_block(&roster_title, Pane::Roster));
|
||||
let mut roster_state = ListState::default();
|
||||
if !self.friends.is_empty() {
|
||||
roster_state.select(Some(self.selected));
|
||||
}
|
||||
f.render_stateful_widget(roster, panes[0], &mut roster_state);
|
||||
|
||||
let (chat_lines, summary) = self.chat_view();
|
||||
let phase = Some(TorPhase::of(self.node.tor_state()));
|
||||
let (chat_lines, summary, chat_style) = self.chat_view(phase);
|
||||
let rows: Vec<FriendRow> = self.friends.iter().map(friend_row).collect();
|
||||
let chat_name = self.friends.get(self.selected).map(|friend| {
|
||||
friend
|
||||
.petname
|
||||
|
|
@ -2576,80 +2560,210 @@ impl App {
|
|||
.unwrap_or(&friend.fingerprint)
|
||||
.to_string()
|
||||
});
|
||||
draw_chat(
|
||||
f,
|
||||
panes[1],
|
||||
&chat_lines,
|
||||
chat_name.as_deref(),
|
||||
self.focus == Pane::Chat,
|
||||
self.chat_scroll,
|
||||
);
|
||||
|
||||
let mark = if self.focus == Pane::Composer {
|
||||
"> "
|
||||
} else {
|
||||
" "
|
||||
};
|
||||
let (cmd, cmd_style) = if self.composer.is_empty() {
|
||||
(format!("{mark}message"), style_dim())
|
||||
} else {
|
||||
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)
|
||||
.wrap(Wrap { trim: false }),
|
||||
cols[3],
|
||||
);
|
||||
|
||||
let footer = self.footer_line(area.width, &summary);
|
||||
f.render_widget(Paragraph::new(footer).style(style_dim()), cols[4]);
|
||||
}
|
||||
|
||||
fn footer_line(&self, width: u16, summary: &OutboxSummary) -> String {
|
||||
// The outbox is the source of truth while anything is outstanding; the
|
||||
// transient note still covers "sending", "sent file x", "invoice sent".
|
||||
let note = summary.note().or_else(|| self.status_note.clone());
|
||||
status_footer(
|
||||
width,
|
||||
&self.me_fp,
|
||||
&self.me_onion,
|
||||
note.as_deref(),
|
||||
footer_hints(&self.screen),
|
||||
)
|
||||
let mut note = summary.note().or_else(|| self.status_note.clone());
|
||||
// The alert row above the panes is the error channel. When it already
|
||||
// carries the same sentence, drop it from the footer: one statement of a
|
||||
// failure, in the place the eye goes for errors.
|
||||
if note.as_deref() == self.alert.as_deref() {
|
||||
note = None;
|
||||
}
|
||||
let view = MainView {
|
||||
me_fp: &self.me_fp,
|
||||
me_onion: &self.me_onion,
|
||||
phase,
|
||||
friends: &rows,
|
||||
selected: self.selected,
|
||||
focus: self.focus,
|
||||
chat_lines: &chat_lines,
|
||||
chat_style,
|
||||
chat_name: chat_name.as_deref(),
|
||||
chat_scroll: self.chat_scroll,
|
||||
composer: &self.composer,
|
||||
alert: self.alert.as_deref(),
|
||||
note: note.as_deref(),
|
||||
};
|
||||
draw_main_chrome(f, &view);
|
||||
}
|
||||
|
||||
fn chat_view(&self) -> (Vec<String>, OutboxSummary) {
|
||||
/// Transcript, outbox summary, and how the rows should be styled. State copy
|
||||
/// is flagged here — the render path never guesses from the text.
|
||||
fn chat_view(&self, phase: Option<TorPhase>) -> (Vec<String>, OutboxSummary, ChatRowStyle) {
|
||||
let Some(friend) = self.friends.get(self.selected) else {
|
||||
return (
|
||||
vec![
|
||||
"no friends yet".into(),
|
||||
"F3 paste an invite to add one".into(),
|
||||
],
|
||||
chat_no_friends_lines(phase),
|
||||
OutboxSummary::default(),
|
||||
ChatRowStyle::State,
|
||||
);
|
||||
};
|
||||
match self.node.list_messages(&friend.pubkey) {
|
||||
Ok(msgs) => {
|
||||
let pending = self.node.outbox_pending(&friend.pubkey).unwrap_or_default();
|
||||
chat_with_delivery(&msgs, &pending, unix_now_secs())
|
||||
let (lines, summary) = chat_with_delivery(&msgs, &pending, unix_now_secs());
|
||||
let style = if msgs.is_empty() {
|
||||
ChatRowStyle::State
|
||||
} else {
|
||||
ChatRowStyle::Text
|
||||
};
|
||||
(lines, summary, style)
|
||||
}
|
||||
Err(e) => (vec![e], OutboxSummary::default()),
|
||||
Err(e) => (vec![e], OutboxSummary::default(), ChatRowStyle::Error),
|
||||
}
|
||||
}
|
||||
|
||||
fn pane_block<'a>(&self, title: &'a str, pane: Pane) -> Block<'a> {
|
||||
pane_block_for(title, self.focus == pane)
|
||||
}
|
||||
}
|
||||
|
||||
/// Pane chrome without an `App`: focus is accent border + bold title, never
|
||||
/// color alone (the `> ` marks come from the callers).
|
||||
/// One roster row: the label, the fingerprint, and the word `stale` when the
|
||||
/// last dial did not answer. Colour is what the row is (text vs stale), never a
|
||||
/// decoration — the word carries the state on a monochrome terminal.
|
||||
pub fn roster_line(row: &FriendRow, inner_width: u16) -> Line<'static> {
|
||||
let text = roster_row_text(row, inner_width);
|
||||
let style = if row.stale { style_stale() } else { style_text() };
|
||||
Line::from(Span::raw(text).style(style))
|
||||
}
|
||||
|
||||
/// How one chat row is styled.
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||
pub enum ChatRowStyle {
|
||||
/// Transcript rows: our own messages and the peer's.
|
||||
Text,
|
||||
/// Empty / loading copy that stands in for a transcript.
|
||||
State,
|
||||
/// The pane could not read the transcript at all.
|
||||
Error,
|
||||
}
|
||||
|
||||
/// The main screen: one header line, one primary pane, one status line, one
|
||||
/// footer hint line.
|
||||
///
|
||||
/// Every field is plain data, so the render path has no `Node` in it and
|
||||
/// `tests/tui_frames.rs` dumps the real chrome at 80x24, 120x40 and 200x60.
|
||||
pub struct MainView<'a> {
|
||||
pub me_fp: &'a str,
|
||||
pub me_onion: &'a str,
|
||||
/// Tor phase, or `None` to keep the phase-free `TOR` token.
|
||||
pub phase: Option<TorPhase>,
|
||||
pub friends: &'a [FriendRow],
|
||||
pub selected: usize,
|
||||
/// Which pane owns the keyboard. Drives the marker, the bold title, the
|
||||
/// accented border, and the footer hints.
|
||||
pub focus: Pane,
|
||||
pub chat_lines: &'a [String],
|
||||
pub chat_style: ChatRowStyle,
|
||||
pub chat_name: Option<&'a str>,
|
||||
pub chat_scroll: u16,
|
||||
pub composer: &'a str,
|
||||
/// Fail-closed one-liner: `send failed: …`, `fingerprint mismatch`, or a
|
||||
/// validation message. Rendered above the panes, never truncated mid-word.
|
||||
pub alert: Option<&'a str>,
|
||||
/// Footer note: outbox summary or the last status message.
|
||||
pub note: Option<&'a str>,
|
||||
}
|
||||
|
||||
/// Draw the main screen. The only chrome that reads colour is `pane_block_for`
|
||||
/// (focus), `roster_line` (state), and the header's phase token.
|
||||
pub fn draw_main_chrome(f: &mut Frame, view: &MainView<'_>) {
|
||||
let area = f.area();
|
||||
let alert_h = u16::from(view.alert.is_some());
|
||||
let cols = Layout::vertical([
|
||||
Constraint::Length(1),
|
||||
Constraint::Length(alert_h),
|
||||
Constraint::Min(3),
|
||||
Constraint::Length(3),
|
||||
Constraint::Length(1),
|
||||
])
|
||||
.split(area);
|
||||
|
||||
f.render_widget(
|
||||
Paragraph::new(Line::from(main_header_spans(
|
||||
area.width,
|
||||
view.me_fp,
|
||||
view.me_onion,
|
||||
view.phase,
|
||||
))),
|
||||
cols[0],
|
||||
);
|
||||
if let Some(msg) = view.alert {
|
||||
let text = alert_line(msg, area.width);
|
||||
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::Length(roster_w), Constraint::Min(10)]).split(cols[2]);
|
||||
let inner_roster = panes[0].width.saturating_sub(2);
|
||||
|
||||
let stale_n = view.friends.iter().filter(|fr| fr.stale).count();
|
||||
let roster_title = if view.friends.is_empty() {
|
||||
"roster 0".to_string()
|
||||
} else if stale_n > 0 {
|
||||
format!("roster {} {stale_n} stale", view.friends.len())
|
||||
} else {
|
||||
format!("roster {}", view.friends.len())
|
||||
};
|
||||
|
||||
let items: Vec<ListItem> = if view.friends.is_empty() {
|
||||
empty_roster_lines()
|
||||
.iter()
|
||||
.map(|l| ListItem::new(*l).style(style_dim()))
|
||||
.collect()
|
||||
} else {
|
||||
view.friends
|
||||
.iter()
|
||||
.map(|row| ListItem::new(roster_line(row, inner_roster)))
|
||||
.collect()
|
||||
};
|
||||
let roster = List::new(items)
|
||||
.highlight_style(style_highlight())
|
||||
.highlight_symbol("> ")
|
||||
.block(pane_block_for(&roster_title, view.focus == Pane::Roster));
|
||||
let mut roster_state = ListState::default();
|
||||
if !view.friends.is_empty() {
|
||||
roster_state.select(Some(view.selected));
|
||||
}
|
||||
f.render_stateful_widget(roster, panes[0], &mut roster_state);
|
||||
|
||||
let chat = chat_lines_for(view.chat_lines, view.chat_style);
|
||||
draw_chat_lines(
|
||||
f,
|
||||
panes[1],
|
||||
&chat,
|
||||
view.chat_name,
|
||||
view.focus == Pane::Chat,
|
||||
view.chat_scroll,
|
||||
);
|
||||
|
||||
// Composer: no frame. `> ` plus bold while it owns the keyboard, `message`
|
||||
// as a muted placeholder when it is empty. The 3-row slot stays so a
|
||||
// wrapped line is not clipped.
|
||||
let focused = view.focus == Pane::Composer;
|
||||
let mark = if focused { "> " } else { " " };
|
||||
let (cmd, cmd_style) = if view.composer.is_empty() {
|
||||
(format!("{mark}message"), style_dim())
|
||||
} else if focused {
|
||||
(
|
||||
format!("{mark}{}", view.composer),
|
||||
style_text().add_modifier(Modifier::BOLD),
|
||||
)
|
||||
} else {
|
||||
(format!("{mark}{}", view.composer), style_text())
|
||||
};
|
||||
f.render_widget(
|
||||
Paragraph::new(cmd)
|
||||
.style(cmd_style)
|
||||
.wrap(Wrap { trim: false }),
|
||||
cols[3],
|
||||
);
|
||||
|
||||
let footer = status_footer_compact(
|
||||
area.width,
|
||||
view.me_fp,
|
||||
view.phase,
|
||||
view.note,
|
||||
pane_footer_hints(view.focus),
|
||||
);
|
||||
f.render_widget(Paragraph::new(footer).style(style_dim()), cols[4]);
|
||||
}
|
||||
fn pane_block_for(title: &str, focused: bool) -> Block<'_> {
|
||||
let border = if focused {
|
||||
style_accent()
|
||||
|
|
@ -2683,6 +2797,35 @@ pub fn draw_chat(
|
|||
name: Option<&str>,
|
||||
focused: bool,
|
||||
scroll: u16,
|
||||
) {
|
||||
let styled = chat_lines_for(lines, ChatRowStyle::Text);
|
||||
draw_chat_lines(f, area, &styled, name, focused, scroll);
|
||||
}
|
||||
|
||||
/// Transcript rows, styled. The style is a caller decision, never inferred from
|
||||
/// the text: a peer message body must not be able to pick its own colour by
|
||||
/// starting with the word `no`.
|
||||
pub fn chat_lines_for(lines: &[String], style: ChatRowStyle) -> Vec<Line<'static>> {
|
||||
let style = match style {
|
||||
ChatRowStyle::Text => style_text(),
|
||||
ChatRowStyle::State => style_dim(),
|
||||
ChatRowStyle::Error => style_alert(),
|
||||
};
|
||||
lines
|
||||
.iter()
|
||||
.map(|l| Line::from(Span::raw(l.clone()).style(style)))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The chat pane itself. Same renderer for transcript rows and state copy; the
|
||||
/// only difference is the style the caller baked into each `Line`.
|
||||
pub fn draw_chat_lines(
|
||||
f: &mut Frame,
|
||||
area: Rect,
|
||||
lines: &[Line<'static>],
|
||||
name: Option<&str>,
|
||||
focused: bool,
|
||||
scroll: u16,
|
||||
) {
|
||||
let inner_h = area.height.saturating_sub(2);
|
||||
let nlines = lines.len();
|
||||
|
|
@ -2700,8 +2843,7 @@ pub fn draw_chat(
|
|||
let mark = if focused { "> " } else { " " };
|
||||
let title = format!("{mark}{title}");
|
||||
f.render_widget(
|
||||
Paragraph::new(lines.join("\n"))
|
||||
.style(style_text())
|
||||
Paragraph::new(lines.to_vec())
|
||||
.wrap(Wrap { trim: false })
|
||||
.scroll((scroll, 0))
|
||||
.block(pane_block_for(&title, focused)),
|
||||
|
|
@ -2709,18 +2851,199 @@ pub fn draw_chat(
|
|||
);
|
||||
}
|
||||
|
||||
/// Chat-pane copy when no friend is selected. States after the base two rows
|
||||
/// say which phase the node is actually in, so an empty roster during first
|
||||
/// publish does not read as a broken app.
|
||||
pub fn chat_no_friends_lines(phase: Option<TorPhase>) -> Vec<String> {
|
||||
let mut lines = vec![
|
||||
"no friends yet".to_string(),
|
||||
"F3 paste an invite to add one".to_string(),
|
||||
];
|
||||
let state = match phase {
|
||||
None => None,
|
||||
Some(TorPhase::Connecting) => {
|
||||
Some("tor connecting — first publish takes a few minutes")
|
||||
}
|
||||
Some(TorPhase::Degraded) => Some("tor degraded — reachable, still settling"),
|
||||
Some(TorPhase::Off) => Some("tor off — no onion service is running"),
|
||||
Some(TorPhase::Ready) => None,
|
||||
};
|
||||
if let Some(note) = state {
|
||||
lines.push(String::new());
|
||||
lines.push(note.to_string());
|
||||
}
|
||||
lines
|
||||
}
|
||||
|
||||
/// Roster-pane copy when the friend list is empty. Two rows, so the pane reads
|
||||
/// as an empty state instead of a blank box.
|
||||
pub fn empty_roster_lines() -> [&'static str; 2] {
|
||||
["no friends", "F3 adds one"]
|
||||
}
|
||||
|
||||
/// One friend, as the roster draws it.
|
||||
///
|
||||
/// Built by `App` from `store::Friend`; the render path never touches the store
|
||||
/// type, which is what lets `tests/tui_frames.rs` draw the real chrome.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct FriendRow {
|
||||
/// Display name: petname when set, fingerprint otherwise, already sanitized.
|
||||
pub label: String,
|
||||
/// Full fingerprint hex; the row fits it to the pane.
|
||||
pub fingerprint: String,
|
||||
/// Last dial to this peer's onion did not answer.
|
||||
pub stale: bool,
|
||||
}
|
||||
|
||||
/// The roster row for one friend, fitted to the pane's inner width in cells.
|
||||
pub fn friend_row(friend: &Friend) -> FriendRow {
|
||||
let raw = friend.petname.as_deref().unwrap_or(&friend.fingerprint);
|
||||
FriendRow {
|
||||
label: sanitize::display_text(raw.as_bytes()).to_string(),
|
||||
fingerprint: friend.fingerprint.clone(),
|
||||
stale: !friend.last_connect_ok,
|
||||
}
|
||||
}
|
||||
|
||||
/// Tor lifecycle as the chrome shows it: one word plus one glyph, so the phase
|
||||
/// survives `NO_COLOR`, a 16-colour terminal, and a monochrome dump.
|
||||
///
|
||||
/// Read from `Node::tor_state()`. `Connecting` is the long first publish (HsDir
|
||||
/// descriptor upload), `Ready` is a published descriptor, `Degraded` is
|
||||
/// reachable while Arti still reports the service broken — usable, and not the
|
||||
/// same statement as ready — and `Off` is a stopped node.
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||
pub enum TorPhase {
|
||||
Connecting,
|
||||
Ready,
|
||||
Degraded,
|
||||
Off,
|
||||
}
|
||||
|
||||
impl TorPhase {
|
||||
/// Project `hs::TorState`. This is the only place that knows the mapping.
|
||||
pub fn of(state: TorState) -> Self {
|
||||
match state {
|
||||
TorState::Published => Self::Ready,
|
||||
TorState::DegradedReachable => Self::Degraded,
|
||||
TorState::Stopped => Self::Off,
|
||||
TorState::Bootstrapping => Self::Connecting,
|
||||
}
|
||||
}
|
||||
|
||||
/// The word a reader sees. Never abbreviated: `ok`/`up` would be ambiguous
|
||||
/// with the delivery markers.
|
||||
pub fn word(self) -> &'static str {
|
||||
match self {
|
||||
Self::Connecting => "connecting",
|
||||
Self::Ready => "ready",
|
||||
Self::Degraded => "degraded",
|
||||
Self::Off => "off",
|
||||
}
|
||||
}
|
||||
|
||||
/// Marker glyph, paired with [`Self::word`]. ASCII chrome falls back to
|
||||
/// `...` / `ok` rather than dropping the marker.
|
||||
pub fn glyph(self) -> &'static str {
|
||||
let ascii = ascii_chrome();
|
||||
match self {
|
||||
Self::Connecting => {
|
||||
if ascii {
|
||||
"..."
|
||||
} else {
|
||||
"…"
|
||||
}
|
||||
}
|
||||
Self::Ready => {
|
||||
if ascii {
|
||||
"ok"
|
||||
} else {
|
||||
"✓"
|
||||
}
|
||||
}
|
||||
Self::Degraded => "!",
|
||||
Self::Off => "x",
|
||||
}
|
||||
}
|
||||
|
||||
/// Semantic style: colour answers "what is this", never "what mood".
|
||||
/// Ready is the unremarkable state, so it takes the plain text token.
|
||||
pub fn style(self) -> Style {
|
||||
match self {
|
||||
Self::Connecting => style_dim(),
|
||||
Self::Ready => style_text(),
|
||||
Self::Degraded => style_stale(),
|
||||
Self::Off => style_alert(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The footer's `TOR` cluster. `None` keeps the bare `TOR` the chrome tests
|
||||
/// lock; `Some` adds the phase glyph and word.
|
||||
pub fn tor_token(phase: Option<TorPhase>) -> String {
|
||||
match phase {
|
||||
None => "TOR".to_string(),
|
||||
Some(p) => format!("{} TOR {}", p.glyph(), p.word()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Status bar: never drop a non-empty `status_note`. Always keep `TOR`, `me:`, `? help`.
|
||||
///
|
||||
/// Phase-free twin of [`status_footer_phased`], kept because the chrome tests
|
||||
/// and the reconstructed dumps call it directly.
|
||||
pub fn status_footer(
|
||||
width: u16,
|
||||
me_fp: &str,
|
||||
me_onion: &str,
|
||||
status_note: Option<&str>,
|
||||
hints: &str,
|
||||
) -> String {
|
||||
status_footer_phased(width, me_fp, me_onion, None, status_note, hints)
|
||||
}
|
||||
|
||||
/// Status bar with the Tor phase folded into the `TOR` cluster.
|
||||
///
|
||||
/// Every width here is in terminal **cells** (`fit_cells` / `cell_width`), not
|
||||
/// `chars().count()`: the phase glyph and any peer-supplied note text must not
|
||||
/// be able to push the line past the frame.
|
||||
pub fn status_footer_phased(
|
||||
width: u16,
|
||||
me_fp: &str,
|
||||
me_onion: &str,
|
||||
phase: Option<TorPhase>,
|
||||
status_note: Option<&str>,
|
||||
hints: &str,
|
||||
) -> String {
|
||||
status_footer_inner(width, me_fp, Some(me_onion), phase, status_note, hints)
|
||||
}
|
||||
|
||||
/// Footer for a screen whose header already prints the identity.
|
||||
///
|
||||
/// The main screen's header carries `me:` and `onion:`, so repeating the onion
|
||||
/// in the footer costs 20+ cells and buys nothing; those cells go to the hints
|
||||
/// instead. `TOR` and `me:` stay — `tests/tui_chrome.rs` locks both.
|
||||
pub fn status_footer_compact(
|
||||
width: u16,
|
||||
me_fp: &str,
|
||||
phase: Option<TorPhase>,
|
||||
status_note: Option<&str>,
|
||||
hints: &str,
|
||||
) -> String {
|
||||
status_footer_inner(width, me_fp, None, phase, status_note, hints)
|
||||
}
|
||||
|
||||
fn status_footer_inner(
|
||||
width: u16,
|
||||
me_fp: &str,
|
||||
me_onion: Option<&str>,
|
||||
phase: Option<TorPhase>,
|
||||
status_note: Option<&str>,
|
||||
hints: &str,
|
||||
) -> String {
|
||||
let w = width as usize;
|
||||
let gap = if w >= 120 { " " } else { " " };
|
||||
let help = "? help";
|
||||
let me = format!("TOR me:{}", truncate_fp(me_fp));
|
||||
let me = format!("{} me:{}", tor_token(phase), truncate_fp(me_fp));
|
||||
let mut note = status_note.unwrap_or("").trim().to_string();
|
||||
if !note.is_empty()
|
||||
&& color_mode() == ColorMode::None
|
||||
|
|
@ -2730,22 +3053,11 @@ pub fn status_footer(
|
|||
note = format!("! {note}");
|
||||
}
|
||||
|
||||
let onion_full = if me_onion.is_empty() {
|
||||
"unset".to_string()
|
||||
} else {
|
||||
me_onion.to_string()
|
||||
};
|
||||
let onion_short = if me_onion.is_empty() {
|
||||
"unset".to_string()
|
||||
} else {
|
||||
truncate_fp(me_onion)
|
||||
};
|
||||
|
||||
if !note.is_empty() {
|
||||
let fixed = char_len(&me) + char_len(help) + 2 * gap.len();
|
||||
let fixed = cell_width(&me) + cell_width(help) + 2 * cell_width(gap);
|
||||
let budget = w.saturating_sub(fixed);
|
||||
if char_len(¬e) > budget {
|
||||
note = truncate_chars(¬e, budget.max(12));
|
||||
if cell_width(¬e) > budget {
|
||||
note = fit_cells(¬e, budget.max(12));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -2755,13 +3067,25 @@ pub fn status_footer(
|
|||
}
|
||||
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);
|
||||
if let Some(onion) = me_onion {
|
||||
let onion_full = if onion.is_empty() {
|
||||
"unset".to_string()
|
||||
} else {
|
||||
onion.to_string()
|
||||
};
|
||||
let onion_short = if onion.is_empty() {
|
||||
"unset".to_string()
|
||||
} else {
|
||||
truncate_fp(onion)
|
||||
};
|
||||
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", "");
|
||||
|
|
@ -2777,10 +3101,10 @@ pub fn status_footer(
|
|||
|
||||
parts.push(help.to_string());
|
||||
let line = parts.join(gap);
|
||||
if char_len(&line) <= w {
|
||||
if cell_width(&line) <= w {
|
||||
line
|
||||
} else {
|
||||
truncate_chars(&line, w)
|
||||
fit_cells(&line, w)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -2788,7 +3112,7 @@ fn footer_fits(parts: &[String], extra: &[&str], help: &str, gap: &str, w: usize
|
|||
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
|
||||
cell_width(&all.join(gap)) <= w
|
||||
}
|
||||
|
||||
/// Main-screen key hints. `F6` (wallet) and `F9` (wallets & settings) are named
|
||||
|
|
@ -2799,6 +3123,20 @@ pub fn main_footer_hints() -> &'static str {
|
|||
"Tab F2 F3 F4 F5 F6 F9 ? help Ctrl-Q"
|
||||
}
|
||||
|
||||
/// Footer hints for one focused main pane.
|
||||
///
|
||||
/// The footer is the only place the keys are visible without opening `?`, so it
|
||||
/// changes with the pane you are actually in — and every variant keeps `F9`
|
||||
/// (the discoverable menu) and `? help`, which `tests/tui_chrome.rs` asserts are
|
||||
/// reachable from the footer at 80, 120 and 200.
|
||||
pub fn pane_footer_hints(pane: Pane) -> &'static str {
|
||||
match pane {
|
||||
Pane::Roster => "j k select F3 add F6 wallet F9 settings ? help",
|
||||
Pane::Chat => "j k scroll g G ends F4 rotate F9 settings ? help",
|
||||
Pane::Composer => "Enter send / commands F9 settings ? help",
|
||||
}
|
||||
}
|
||||
|
||||
fn footer_hints(screen: &Screen) -> &'static str {
|
||||
match screen {
|
||||
Screen::Main => main_footer_hints(),
|
||||
|
|
@ -3006,26 +3344,35 @@ fn draw_passphrase(f: &mut Frame, kind: BackupKind, second: bool, buf: &str) {
|
|||
);
|
||||
}
|
||||
|
||||
/// A centered popup over a cleared screen: title, body, wrap, scroll to the
|
||||
/// tail. Every modal goes through this one, so a popup can never punch through
|
||||
/// the chrome underneath it and can never be wider than the frame.
|
||||
///
|
||||
/// Width always keeps a one-cell margin on each side. Height keeps the margin
|
||||
/// only when the frame can afford it: a 22-line body on a 24-row terminal needs
|
||||
/// the rows more than it needs symmetry, and the scroll below still lands the
|
||||
/// last line on screen.
|
||||
pub fn draw_text_overlay(f: &mut Frame, title: &str, text: &str) {
|
||||
let area = f.area();
|
||||
let popup = if area.height < 28 {
|
||||
area
|
||||
let wanted_w = text
|
||||
.lines()
|
||||
.map(cell_width)
|
||||
.max()
|
||||
.unwrap_or(40)
|
||||
.saturating_add(4);
|
||||
let wanted_h = text.lines().count() as u16 + 2;
|
||||
let max_w = area.width.saturating_sub(2).max(10);
|
||||
let width = (wanted_w as u16).clamp(10, max_w.max(10)).min(area.width);
|
||||
let height = if wanted_h >= area.height.saturating_sub(1) {
|
||||
area.height
|
||||
} 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,
|
||||
}
|
||||
wanted_h.clamp(3, area.height)
|
||||
};
|
||||
let popup = 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);
|
||||
|
|
@ -3034,8 +3381,12 @@ pub fn draw_text_overlay(f: &mut Frame, title: &str, text: &str) {
|
|||
let wrapped: u16 = text
|
||||
.lines()
|
||||
.map(|l| {
|
||||
let n = char_len(l) as u16;
|
||||
if n == 0 { 1 } else { n.div_ceil(inner_w) }
|
||||
let n = cell_width(l) as u16;
|
||||
if n == 0 {
|
||||
1
|
||||
} else {
|
||||
n.div_ceil(inner_w)
|
||||
}
|
||||
})
|
||||
.sum();
|
||||
let scroll = wrapped.saturating_sub(inner_h);
|
||||
|
|
@ -3944,28 +4295,26 @@ fn truncate_fp(fp: &str) -> String {
|
|||
format!("{s}{}", ellipsis())
|
||||
}
|
||||
|
||||
fn roster_row_text(name: &str, fingerprint: &str, stale: bool, inner_width: u16) -> String {
|
||||
fn roster_row_text(row: &FriendRow, 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()
|
||||
let stale_s = if row.stale { " stale" } else { "" };
|
||||
let avail = budget.saturating_sub(cell_width(stale_s));
|
||||
// The name is what the reader is looking for. The fingerprint only
|
||||
// disambiguates two friends with the same petname, so it gets what is left
|
||||
// and is dropped entirely when the pane is too narrow to show a usable
|
||||
// prefix — a row that reads `bob-the-sta… stale` beats one that reads
|
||||
// `… bbbbbbbb… stale`.
|
||||
let name_share = cell_width(&row.label).min((avail / 2).max(6)).min(avail);
|
||||
let fp_share = avail.saturating_sub(name_share);
|
||||
let (shown, fp_part) = if fp_share >= 8 {
|
||||
(
|
||||
fit_cells(&row.label, name_share),
|
||||
format!(" {}", fit_cells(&truncate_fp(&row.fingerprint), fp_share - 2)),
|
||||
)
|
||||
} else {
|
||||
truncate_chars(name, name_budget)
|
||||
(fit_cells(&row.label, avail), String::new())
|
||||
};
|
||||
let line = format!("{shown}{fp_part}{stale_s}");
|
||||
if char_len(&line) > budget {
|
||||
truncate_chars(&line, budget)
|
||||
} else {
|
||||
line
|
||||
}
|
||||
fit_cells(&format!("{shown}{fp_part}{stale_s}"), budget)
|
||||
}
|
||||
|
||||
fn to_hex(bytes: &[u8]) -> String {
|
||||
|
|
|
|||
Loading…
Reference in a new issue