feat(tui): keep fail-closed footer, flatten main chrome
All checks were successful
ci / test (pull_request) Successful in 4m48s
All checks were successful
ci / test (pull_request) Successful in 4m48s
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).
This commit is contained in:
parent
4457c7330c
commit
0749d2300a
2 changed files with 508 additions and 112 deletions
415
src/tui.rs
415
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<ListItem> = 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<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::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::<Vec<_>>().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<String> = 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::<Vec<_>>()
|
||||
.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 {
|
||||
|
|
|
|||
Loading…
Reference in a new issue