feat(tui): ASCII chrome, invite wrap, alert truncate, remaining copy
Some checks failed
ci / test (pull_request) Failing after 4m39s
Some checks failed
ci / test (pull_request) Failing after 4m39s
ONIONWIRE_ASCII (and NO_COLOR) switches truncation to ... and ASCII borders. Share/paste wrap at : so a mouse-copy cannot lose a mid-hex line. Alerts truncate to the frame; empty threads and paste errors keep the next-step hint. Skip the optional 40-row wordmark.
This commit is contained in:
parent
33beeded92
commit
35b8caa716
3 changed files with 337 additions and 18 deletions
122
src/tui.rs
122
src/tui.rs
|
|
@ -5,6 +5,7 @@ use std::time::Duration;
|
|||
use ratatui::crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
|
||||
use ratatui::layout::{Constraint, Layout, Rect};
|
||||
use ratatui::style::{Color, Modifier, Style};
|
||||
use ratatui::symbols::border;
|
||||
use ratatui::widgets::{Block, Borders, Clear, List, ListItem, ListState, Paragraph, Wrap};
|
||||
use ratatui::{DefaultTerminal, Frame};
|
||||
|
||||
|
|
@ -38,8 +39,90 @@ fn color_mode() -> ColorMode {
|
|||
}
|
||||
}
|
||||
|
||||
/// ASCII chrome when `ONIONWIRE_ASCII` or `NO_COLOR` is set. No extra crate.
|
||||
pub fn ascii_chrome() -> bool {
|
||||
std::env::var_os("ONIONWIRE_ASCII").is_some() || std::env::var_os("NO_COLOR").is_some()
|
||||
}
|
||||
|
||||
const ASCII_BORDERS: border::Set = border::Set {
|
||||
top_left: "+",
|
||||
top_right: "+",
|
||||
bottom_left: "+",
|
||||
bottom_right: "+",
|
||||
vertical_left: "|",
|
||||
vertical_right: "|",
|
||||
horizontal_top: "-",
|
||||
horizontal_bottom: "-",
|
||||
};
|
||||
|
||||
fn ellipsis() -> &'static str {
|
||||
"…"
|
||||
if ascii_chrome() { "..." } else { "…" }
|
||||
}
|
||||
|
||||
/// Truncate or keep an alert so it cannot spill into the roster row.
|
||||
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)
|
||||
}
|
||||
|
||||
/// Wrap an invite at `:` so a mouse-copy cannot lose a mid-hex continuation.
|
||||
/// Joining the lines (no extra spaces) reconstructs `payload`.
|
||||
pub fn wrap_invite(payload: &str, width: usize) -> String {
|
||||
if width == 0 {
|
||||
return String::new();
|
||||
}
|
||||
let mut lines: Vec<String> = Vec::new();
|
||||
let mut cur = String::new();
|
||||
let parts: Vec<&str> = payload.split(':').collect();
|
||||
for (i, part) in parts.iter().enumerate() {
|
||||
let piece = if i + 1 < parts.len() {
|
||||
format!("{part}:")
|
||||
} else {
|
||||
(*part).to_string()
|
||||
};
|
||||
for chunk in char_chunks(&piece, width) {
|
||||
if cur.is_empty() {
|
||||
cur = chunk;
|
||||
} else if char_len(&cur) + char_len(&chunk) <= width {
|
||||
cur.push_str(&chunk);
|
||||
} else {
|
||||
lines.push(std::mem::take(&mut cur));
|
||||
cur = chunk;
|
||||
}
|
||||
}
|
||||
}
|
||||
if !cur.is_empty() {
|
||||
lines.push(cur);
|
||||
}
|
||||
lines.join("\n")
|
||||
}
|
||||
|
||||
fn char_chunks(s: &str, width: usize) -> Vec<String> {
|
||||
if width == 0 {
|
||||
return Vec::new();
|
||||
}
|
||||
if char_len(s) <= width {
|
||||
return vec![s.to_string()];
|
||||
}
|
||||
let chars: Vec<char> = s.chars().collect();
|
||||
chars.chunks(width).map(|c| c.iter().collect()).collect()
|
||||
}
|
||||
|
||||
/// Friend selected, no messages yet.
|
||||
pub fn empty_friend_chat_text() -> &'static str {
|
||||
"no messages\ntype below, Enter sends"
|
||||
}
|
||||
|
||||
/// Paste overlay copy. Error keeps the resubmit hint.
|
||||
pub fn paste_screen_text(buf: &str, err: Option<&str>) -> String {
|
||||
let hint = match err {
|
||||
Some(e) => format!("{e}\nEnter resubmits Esc back"),
|
||||
None => "paste onionwire:v1 payload, Enter to submit".to_string(),
|
||||
};
|
||||
format!("{hint}\n\n{buf}")
|
||||
}
|
||||
|
||||
fn char_len(s: &str) -> usize {
|
||||
|
|
@ -1429,11 +1512,7 @@ impl App {
|
|||
|
||||
f.render_widget(Paragraph::new(header).style(style_accent()), cols[0]);
|
||||
if let Some(msg) = &self.alert {
|
||||
let text = if color_mode() == ColorMode::None && !msg.starts_with("! ") {
|
||||
format!("! {msg}")
|
||||
} else {
|
||||
msg.clone()
|
||||
};
|
||||
let text = alert_line(msg, area.width);
|
||||
f.render_widget(Paragraph::new(text).style(style_alert()), cols[1]);
|
||||
}
|
||||
|
||||
|
|
@ -1559,7 +1638,10 @@ impl App {
|
|||
];
|
||||
};
|
||||
match self.node.list_messages(&friend.pubkey) {
|
||||
Ok(msgs) if msgs.is_empty() => vec!["no messages".into()],
|
||||
Ok(msgs) if msgs.is_empty() => empty_friend_chat_text()
|
||||
.lines()
|
||||
.map(str::to_string)
|
||||
.collect(),
|
||||
Ok(msgs) => msgs
|
||||
.iter()
|
||||
.map(|m| {
|
||||
|
|
@ -1583,8 +1665,7 @@ impl App {
|
|||
} else {
|
||||
style_dim()
|
||||
};
|
||||
Block::default()
|
||||
.borders(Borders::ALL)
|
||||
apply_chrome_borders(Block::default())
|
||||
.border_style(border)
|
||||
.title(title)
|
||||
.title_style(title_style)
|
||||
|
|
@ -1699,9 +1780,17 @@ fn footer_hints(screen: &Screen) -> &'static str {
|
|||
}
|
||||
}
|
||||
|
||||
fn apply_chrome_borders(block: Block<'_>) -> Block<'_> {
|
||||
let block = block.borders(Borders::ALL);
|
||||
if ascii_chrome() {
|
||||
block.border_set(ASCII_BORDERS)
|
||||
} else {
|
||||
block
|
||||
}
|
||||
}
|
||||
|
||||
fn themed_block(title: &str) -> Block<'_> {
|
||||
Block::default()
|
||||
.borders(Borders::ALL)
|
||||
apply_chrome_borders(Block::default())
|
||||
.border_style(style_accent())
|
||||
.title(title)
|
||||
.title_style(style_accent().add_modifier(Modifier::BOLD))
|
||||
|
|
@ -1712,8 +1801,10 @@ pub fn draw_share(f: &mut Frame, payload: &str) {
|
|||
Ok(p) => (truncate_fp(&to_hex(&p.pubkey)), p.onion),
|
||||
Err(_) => ("?".into(), "?".into()),
|
||||
};
|
||||
let inner = f.area().width.saturating_sub(2) as usize;
|
||||
let invite = wrap_invite(payload, inner.max(1));
|
||||
let body = format!(
|
||||
"give this string to a friend; they F3 paste it\n\nfp {fp}\nonion {onion}\n\n{payload}"
|
||||
"give this string to a friend; they F3 paste it\n\nfp {fp}\nonion {onion}\n\n{invite}"
|
||||
);
|
||||
f.render_widget(
|
||||
Paragraph::new(body)
|
||||
|
|
@ -1725,10 +1816,9 @@ 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")
|
||||
.to_string();
|
||||
let body = format!("{hint}\n\n{buf}");
|
||||
let inner = f.area().width.saturating_sub(2) as usize;
|
||||
let shown = wrap_invite(buf, inner.max(1));
|
||||
let body = paste_screen_text(&shown, err);
|
||||
let style = if err.is_some() {
|
||||
style_alert()
|
||||
} else {
|
||||
|
|
|
|||
Loading…
Reference in a new issue