fix(tui): do not freeze the UI on chat send #19
3 changed files with 75 additions and 7 deletions
|
|
@ -110,7 +110,7 @@ or `cargo run --release`.
|
||||||
|
|
||||||
On start you are prompted for a store passphrase (echo off, like other CLI passwords) or set `ONIONWIRE_STORE_PASSPHRASE`. Empty passphrase is rejected; a wrong passphrase does not open chat. Then you should see `onionwire: bootstrapping Arti…` on stderr. Directory bootstrap is usually under a minute; the onion is ready once a probe connect works (combined Arti status may still say Bootstrapping). Fail closed at 360s. Data lives in `ONIONWIRE_HOME` if set, otherwise `~/.local/share/onionwire/` (`onionwire.db` + Arti state, mode 0700). First open creates an ed25519 identity key. That key **is** you.
|
On start you are prompted for a store passphrase (echo off, like other CLI passwords) or set `ONIONWIRE_STORE_PASSPHRASE`. Empty passphrase is rejected; a wrong passphrase does not open chat. Then you should see `onionwire: bootstrapping Arti…` on stderr. Directory bootstrap is usually under a minute; the onion is ready once a probe connect works (combined Arti status may still say Bootstrapping). Fail closed at 360s. Data lives in `ONIONWIRE_HOME` if set, otherwise `~/.local/share/onionwire/` (`onionwire.db` + Arti state, mode 0700). First open creates an ed25519 identity key. That key **is** you.
|
||||||
|
|
||||||
Then `F2` to share your invite, `F3` to paste a friend’s. Mouse-select the `onionwire:v1:…` line to copy. Highlight them in the roster, type in the composer, Enter to send. Fail closed: if their onion is down, send fails — no outbox.
|
Then `F2` to share your invite, `F3` to paste a friend’s. Mouse-select the `onionwire:v1:…` line to copy. Highlight them in the roster, type in the composer, Enter to send. Footer shows `sending`, then `sent` or `send failed` — the TUI stays usable while Tor retries (up to 180s). Fail closed: if their onion is down, send fails — no outbox.
|
||||||
|
|
||||||
## Friends are keys
|
## Friends are keys
|
||||||
|
|
||||||
|
|
|
||||||
49
src/tui.rs
49
src/tui.rs
|
|
@ -1,5 +1,6 @@
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::sync::Arc;
|
use std::sync::{mpsc, Arc};
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
use ratatui::crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
|
use ratatui::crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
|
||||||
use ratatui::layout::{Constraint, Layout, Rect};
|
use ratatui::layout::{Constraint, Layout, Rect};
|
||||||
|
|
@ -19,6 +20,18 @@ const C_STALE: Color = Color::Rgb(210, 180, 80);
|
||||||
const C_HL_BG: Color = Color::Rgb(48, 42, 28);
|
const C_HL_BG: Color = Color::Rgb(48, 42, 28);
|
||||||
const C_BORDER: Color = Color::Rgb(72, 84, 64);
|
const C_BORDER: Color = Color::Rgb(72, 84, 64);
|
||||||
|
|
||||||
|
/// 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);
|
||||||
|
|
||||||
|
/// Non-blocking read of a background send/job status. Never waits on Tor.
|
||||||
|
pub fn take_job_result(rx: &mpsc::Receiver<String>) -> Option<String> {
|
||||||
|
match rx.try_recv() {
|
||||||
|
Ok(s) => Some(s),
|
||||||
|
Err(mpsc::TryRecvError::Empty) => None,
|
||||||
|
Err(mpsc::TryRecvError::Disconnected) => Some("send failed".into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn fingerprint_mismatch_banner() -> &'static str {
|
pub fn fingerprint_mismatch_banner() -> &'static str {
|
||||||
"fingerprint mismatch"
|
"fingerprint mismatch"
|
||||||
}
|
}
|
||||||
|
|
@ -608,6 +621,7 @@ struct App {
|
||||||
help_open: bool,
|
help_open: bool,
|
||||||
who_open: bool,
|
who_open: bool,
|
||||||
chat_scroll: u16,
|
chat_scroll: u16,
|
||||||
|
job_rx: Option<mpsc::Receiver<String>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
enum Screen {
|
enum Screen {
|
||||||
|
|
@ -666,12 +680,17 @@ impl App {
|
||||||
help_open: false,
|
help_open: false,
|
||||||
who_open: false,
|
who_open: false,
|
||||||
chat_scroll: 0,
|
chat_scroll: 0,
|
||||||
|
job_rx: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(&mut self, terminal: &mut DefaultTerminal) -> Result<AppExit, String> {
|
fn run(&mut self, terminal: &mut DefaultTerminal) -> Result<AppExit, String> {
|
||||||
loop {
|
loop {
|
||||||
|
self.drain_job();
|
||||||
terminal.draw(|f| self.draw(f)).map_err(|e| e.to_string())?;
|
terminal.draw(|f| self.draw(f)).map_err(|e| e.to_string())?;
|
||||||
|
if !event::poll(TUI_POLL).map_err(io_err)? {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
let Event::Key(key) = event::read().map_err(io_err)? else {
|
let Event::Key(key) = event::read().map_err(io_err)? else {
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
|
|
@ -1234,14 +1253,34 @@ impl App {
|
||||||
self.status_note = Some("no friend selected".into());
|
self.status_note = Some("no friend selected".into());
|
||||||
return Ok(());
|
return Ok(());
|
||||||
};
|
};
|
||||||
let pk = friend.pubkey.clone();
|
if self.job_rx.is_some() {
|
||||||
match self.rt.block_on(self.node.send(&pk, text.as_bytes())) {
|
self.status_note = Some("already sending".into());
|
||||||
Ok(()) => self.status_note = Some("sent".into()),
|
return Ok(());
|
||||||
Err(e) => self.status_note = Some(format!("send failed: {e}")),
|
|
||||||
}
|
}
|
||||||
|
let pk = friend.pubkey.clone();
|
||||||
|
let body = text.as_bytes().to_vec();
|
||||||
|
let node = Arc::clone(&self.node);
|
||||||
|
let (tx, rx) = mpsc::channel();
|
||||||
|
self.status_note = Some("sending".into());
|
||||||
|
std::mem::drop(self.rt.spawn(async move {
|
||||||
|
let note = match node.send(&pk, &body).await {
|
||||||
|
Ok(()) => "sent".to_string(),
|
||||||
|
Err(e) => format!("send failed: {e}"),
|
||||||
|
};
|
||||||
|
let _ = tx.send(note);
|
||||||
|
}));
|
||||||
|
self.job_rx = Some(rx);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn drain_job(&mut self) {
|
||||||
|
let Some(note) = self.job_rx.as_ref().and_then(take_job_result) else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
self.status_note = Some(note);
|
||||||
|
self.job_rx = None;
|
||||||
|
}
|
||||||
|
|
||||||
fn who_body(&self) -> String {
|
fn who_body(&self) -> String {
|
||||||
let Some(friend) = self.friends.get(self.selected) else {
|
let Some(friend) = self.friends.get(self.selected) else {
|
||||||
return "no friend selected\n\nEsc closes".into();
|
return "no friend selected\n\nEsc closes".into();
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,10 @@
|
||||||
//! Composer Enter must send chat to the selected friend, not only slash commands.
|
//! Composer Enter must send chat to the selected friend, not only slash commands.
|
||||||
|
|
||||||
use onionwire::tui::{composer_enter, ComposerAction, SlashCmd, WipeKind};
|
use onionwire::tui::{
|
||||||
|
composer_enter, take_job_result, ComposerAction, SlashCmd, WipeKind, TUI_POLL,
|
||||||
|
};
|
||||||
|
use std::sync::mpsc;
|
||||||
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn enter_plain_text_is_send() {
|
fn enter_plain_text_is_send() {
|
||||||
|
|
@ -43,3 +47,28 @@ fn enter_unknown_slash_is_not_chat() {
|
||||||
Some(ComposerAction::UnknownSlash("/nope".into()))
|
Some(ComposerAction::UnknownSlash("/nope".into()))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn take_job_result_is_nonblocking_when_empty() {
|
||||||
|
let (tx, rx) = mpsc::channel::<String>();
|
||||||
|
let start = Instant::now();
|
||||||
|
assert_eq!(take_job_result(&rx), None);
|
||||||
|
assert!(
|
||||||
|
start.elapsed() < Duration::from_millis(50),
|
||||||
|
"empty job rx must not wait on the 180s send retry"
|
||||||
|
);
|
||||||
|
drop(tx);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn take_job_result_reads_status_without_blocking() {
|
||||||
|
let (tx, rx) = mpsc::channel();
|
||||||
|
tx.send("sent".into()).unwrap();
|
||||||
|
assert_eq!(take_job_result(&rx).as_deref(), Some("sent"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn tui_poll_is_short_so_keys_keep_working() {
|
||||||
|
assert!(TUI_POLL <= Duration::from_millis(200));
|
||||||
|
assert!(TUI_POLL > Duration::ZERO);
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue