diff --git a/mux/src/lib.rs b/mux/src/lib.rs index 63ab5e41f..46e915a95 100644 --- a/mux/src/lib.rs +++ b/mux/src/lib.rs @@ -11,9 +11,9 @@ use std::collections::HashMap; use std::io::Read; use std::rc::Rc; use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; -use std::sync::mpsc::{sync_channel, Receiver, TryRecvError}; use std::sync::Arc; use std::thread; +use termwiz::escape::Action; use thiserror::*; pub mod activity; @@ -53,111 +53,44 @@ pub struct Mux { banner: RefCell>, } -/// This function bounces the data over to the main thread to feed to -/// the pty in the mux. It blocks until the mux has finished consuming -/// the data. -fn send_to_mux(pane_id: PaneId, dead: &Arc, data: Vec) { +/// This function bounces parsed actions over to the main thread to feed to +/// the pty in the mux. +/// It blocks until the mux has finished consuming the data, which provides +/// some back-pressure so that eg: ctrl-c can remain responsive. +fn send_actions_to_mux(pane_id: PaneId, dead: &Arc, actions: Vec) { promise::spawn::block_on(promise::spawn::spawn_into_main_thread_with_low_priority({ let dead = Arc::clone(&dead); async move { let mux = Mux::get().unwrap(); if let Some(pane) = mux.get_pane(pane_id) { - pane.advance_bytes(&data); + pane.perform_actions(actions); mux.notify(MuxNotification::PaneOutput(pane_id)); } else { // Something else removed the pane from - // the mux, so we should stop trying to - // process it. + // the mux, so signal that we should stop + // trying to process it in read_from_pane_pty. dead.store(true, Ordering::Relaxed); } } })); } -/// The accumulator tries to keep runs of text together, which is important -/// with various emoji sequences as it is not possible to detect all kinds -/// of combining sequences based on their leading bytes. -/// This function prefers to send lines of text to the output parser. -/// If it doesn't find a complete line then it will do a non-blocking poll -/// to allow additional data to appear in the channel so that it can be -/// combined together. -/// If this function takes too long to batch the data together then text -/// input/output latency suffers and feels janky. -fn accumulator(pane_id: PaneId, dead: &Arc, rx: Receiver>) { - let mut buf = vec![]; - - 'outer: while let Ok(mut data) = rx.recv() { - buf.append(&mut data); - - while !buf.is_empty() { - /* - if let Some(idx) = buf.iter().rposition(|&b| b == b'\n') { - let mut split = buf.split_off(idx + 1); - std::mem::swap(&mut split, &mut buf); - send_to_mux(pane_id, &dead, split); - } - */ - - match rx.try_recv() { - Ok(mut extra) => { - buf.append(&mut extra); - } - Err(TryRecvError::Empty) => { - // No more data to read right now, so pass whatever - // we have pending on to the mux thread and then block - // waiting for the next. - let mut to_send = vec![]; - std::mem::swap(&mut to_send, &mut buf); - send_to_mux(pane_id, &dead, to_send); - } - Err(TryRecvError::Disconnected) => break 'outer, - } - } - - if dead.load(Ordering::Relaxed) { - break; - } - } - match configuration().exit_behavior { - ExitBehavior::Hold | ExitBehavior::CloseOnCleanExit => { - // We don't know if we can unilaterally close - // this pane right now, so don't! - send_to_mux(pane_id, &dead, b"\n[Process completed]".to_vec()); - return; - } - ExitBehavior::Close => { - promise::spawn::spawn_into_main_thread(async move { - let mux = Mux::get().unwrap(); - mux.remove_pane(pane_id); - }) - .detach(); - } - } -} - /// This function is run in a separate thread; its purpose is to perform /// blocking reads from the pty (non-blocking reads are not portable to -/// all platforms and pty/tty types) and relay the data to the `accumulator` -/// function above that this function spawns a new thread. +/// all platforms and pty/tty types), parse the escape sequences and +/// relay the actions to the mux thread to apply them to the pane. fn read_from_pane_pty(pane_id: PaneId, banner: Option, mut reader: Box) { const BUFSIZE: usize = 4 * 1024; let mut buf = [0; BUFSIZE]; // This is used to signal that an error occurred either in this thread, - // in the accumulator, or in the main mux thread. If `true`, both this - // and the accumulator thread will terminate + // or in the main mux thread. If `true`, this thread will terminate. let dead = Arc::new(AtomicBool::new(false)); - let (tx, rx) = sync_channel(1); - std::thread::spawn({ - let dead = Arc::clone(&dead); - move || { - accumulator(pane_id, &dead, rx); - } - }); + let mut parser = termwiz::escape::parser::Parser::new(); if let Some(banner) = banner { - tx.send(banner.into_bytes()).ok(); + send_actions_to_mux(pane_id, &dead, parser.parse_as_vec(banner.as_bytes())); } while !dead.load(Ordering::Relaxed) { @@ -172,14 +105,35 @@ fn read_from_pane_pty(pane_id: PaneId, banner: Option, mut reader: Box { let buf = &buf[..size]; - if tx.send(buf.to_vec()).is_err() { - break; + let actions = parser.parse_as_vec(&buf); + if !actions.is_empty() { + send_actions_to_mux(pane_id, &dead, actions); } } } } dead.store(true, Ordering::Relaxed); + + match configuration().exit_behavior { + ExitBehavior::Hold | ExitBehavior::CloseOnCleanExit => { + // We don't know if we can unilaterally close + // this pane right now, so don't! + send_actions_to_mux( + pane_id, + &dead, + parser.parse_as_vec(b"\n[Process completed]"), + ); + return; + } + ExitBehavior::Close => { + promise::spawn::spawn_into_main_thread(async move { + let mux = Mux::get().unwrap(); + mux.remove_pane(pane_id); + }) + .detach(); + } + } } thread_local! { diff --git a/mux/src/localpane.rs b/mux/src/localpane.rs index 85ef8efcb..3bac0448a 100644 --- a/mux/src/localpane.rs +++ b/mux/src/localpane.rs @@ -144,8 +144,8 @@ impl Pane for LocalPane { self.terminal.borrow_mut().set_clipboard(clipboard); } - fn advance_bytes(&self, buf: &[u8]) { - self.terminal.borrow_mut().advance_bytes(buf) + fn perform_actions(&self, actions: Vec) { + self.terminal.borrow_mut().perform_actions(actions) } fn mouse_event(&self, event: MouseEvent) -> Result<(), Error> { diff --git a/mux/src/pane.rs b/mux/src/pane.rs index 86eacb92a..66db3cfda 100644 --- a/mux/src/pane.rs +++ b/mux/src/pane.rs @@ -112,7 +112,7 @@ pub trait Pane: Downcast { fn set_zoomed(&self, _zoomed: bool) {} fn key_down(&self, key: KeyCode, mods: KeyModifiers) -> anyhow::Result<()>; fn mouse_event(&self, event: MouseEvent) -> anyhow::Result<()>; - fn advance_bytes(&self, buf: &[u8]); + fn perform_actions(&self, _actions: Vec) {} fn is_dead(&self) -> bool; fn kill(&self) {} fn palette(&self) -> ColorPalette; diff --git a/mux/src/tab.rs b/mux/src/tab.rs index ca1d7fec3..ebb77b1b4 100644 --- a/mux/src/tab.rs +++ b/mux/src/tab.rs @@ -1555,9 +1555,6 @@ mod test { fn mouse_event(&self, _event: MouseEvent) -> anyhow::Result<()> { unimplemented!() } - fn advance_bytes(&self, _buf: &[u8]) { - unimplemented!() - } fn is_dead(&self) -> bool { false } diff --git a/mux/src/termwiztermtab.rs b/mux/src/termwiztermtab.rs index 7b298d6fe..492259b7c 100644 --- a/mux/src/termwiztermtab.rs +++ b/mux/src/termwiztermtab.rs @@ -225,8 +225,8 @@ impl Pane for TermWizTerminalPane { Ok(()) } - fn advance_bytes(&self, buf: &[u8]) { - self.terminal.borrow_mut().advance_bytes(buf) + fn perform_actions(&self, actions: Vec) { + self.terminal.borrow_mut().perform_actions(actions) } fn is_dead(&self) -> bool { diff --git a/term/src/terminal.rs b/term/src/terminal.rs index 7ee849ea2..a3262bf34 100644 --- a/term/src/terminal.rs +++ b/term/src/terminal.rs @@ -126,4 +126,11 @@ impl Terminal { self.parser.parse(bytes, |action| performer.perform(action)); } + + pub fn perform_actions(&mut self, actions: Vec) { + let mut performer = Performer::new(&mut self.state); + for action in actions { + performer.perform(action); + } + } } diff --git a/wezterm-client/src/pane/clienttab.rs b/wezterm-client/src/pane/clienttab.rs index 381595237..a6558be42 100644 --- a/wezterm-client/src/pane/clienttab.rs +++ b/wezterm-client/src/pane/clienttab.rs @@ -306,10 +306,6 @@ impl Pane for ClientPane { Ok(()) } - fn advance_bytes(&self, _buf: &[u8]) { - panic!("ClientPane::advance_bytes not impl"); - } - fn is_dead(&self) -> bool { self.renderable.borrow().inner.borrow().dead } diff --git a/wezterm-gui/src/overlay/copy.rs b/wezterm-gui/src/overlay/copy.rs index 2ed5e03d1..a8e1a0d32 100644 --- a/wezterm-gui/src/overlay/copy.rs +++ b/wezterm-gui/src/overlay/copy.rs @@ -482,8 +482,8 @@ impl Pane for CopyOverlay { anyhow::bail!("ignoring mouse while copying"); } - fn advance_bytes(&self, buf: &[u8]) { - self.delegate.advance_bytes(buf) + fn perform_actions(&self, actions: Vec) { + self.delegate.perform_actions(actions) } fn is_dead(&self) -> bool { diff --git a/wezterm-gui/src/overlay/search.rs b/wezterm-gui/src/overlay/search.rs index d49b6c8c9..f935972f3 100644 --- a/wezterm-gui/src/overlay/search.rs +++ b/wezterm-gui/src/overlay/search.rs @@ -228,9 +228,10 @@ impl Pane for SearchOverlay { self.delegate.mouse_event(event) } - fn advance_bytes(&self, buf: &[u8]) { - self.delegate.advance_bytes(buf) + fn perform_actions(&self, actions: Vec) { + self.delegate.perform_actions(actions) } + fn is_dead(&self) -> bool { self.delegate.is_dead() } diff --git a/wezterm/src/main.rs b/wezterm/src/main.rs index 95d45afb7..33a770abb 100644 --- a/wezterm/src/main.rs +++ b/wezterm/src/main.rs @@ -15,8 +15,6 @@ use wezterm_client::client::{unix_connect_with_retry, Client}; use wezterm_gui_subcommands::*; // let message = "; ❤ 😍🤢\n\x1b[91;mw00t\n\x1b[37;104;m bleet\x1b[0;m."; -// terminal.advance_bytes(message); -// != #[derive(Debug, StructOpt)] #[structopt(