Skip to content

A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.

mux: write a pane's raw bytes through the terminal's writer thread

RoversXadde1d0170bb
diff --git a/mux/src/domain.rs b/mux/src/domain.rs
index 0953f13b..8aa4fd39 100644
--- a/mux/src/domain.rs
+++ b/mux/src/domain.rs
@@ -758,6 +758,7 @@ impl Domain for LocalDomain {
if self.is_conpty() {
terminal.enable_conpty_quirks();
}
+ let pane_writer = terminal.writer_handle();
let pane: Arc<dyn Pane> = match child_result {
Ok(child) => Arc::new(LocalPane::new(
@@ -765,7 +766,7 @@ impl Domain for LocalDomain {
terminal,
child,
pair.master,
- Box::new(writer),
+ pane_writer,
self.id,
command_description,
)),
@@ -781,7 +782,7 @@ impl Domain for LocalDomain {
Box::new(FailedSpawnPty {
inner: Mutex::new(pair.master),
}),
- Box::new(writer),
+ pane_writer,
self.id,
command_description,
))
diff --git a/mux/src/ssh.rs b/mux/src/ssh.rs
index 9ada5654..1e9abbed 100644
--- a/mux/src/ssh.rs
+++ b/mux/src/ssh.rs
@@ -1,5 +1,5 @@
use crate::connui::ConnectionUI;
-use crate::domain::{alloc_domain_id, Domain, DomainId, DomainState, WriterWrapper};
+use crate::domain::{alloc_domain_id, Domain, DomainId, DomainState};
use crate::localpane::LocalPane;
use crate::pane::{alloc_pane_id, Pane, PaneId};
use crate::Mux;
@@ -911,22 +911,21 @@ impl Domain for RemoteSshDomain {
// eg: tmux integration to be tunnelled via the remote
// session without duplicating a lot of logic over here.
- let writer = WriterWrapper::new(writer);
-
let terminal = wezterm_term::Terminal::new(
size,
std::sync::Arc::new(config::TermConfig::new()),
"ThinkTerm",
config::wezterm_version(),
- Box::new(writer.clone()),
+ writer,
);
+ let pane_writer = terminal.writer_handle();
let pane: Arc<dyn Pane> = Arc::new(LocalPane::new(
pane_id,
terminal,
child,
pty,
- Box::new(writer),
+ pane_writer,
self.id,
"RemoteSshDomain".to_string(),
));
diff --git a/mux/src/tmux_commands.rs b/mux/src/tmux_commands.rs
index c307613c..2d0edd66 100644
--- a/mux/src/tmux_commands.rs
+++ b/mux/src/tmux_commands.rs
@@ -1,4 +1,4 @@
-use crate::domain::{DomainId, WriterWrapper};
+use crate::domain::DomainId;
use crate::localpane::LocalPane;
use crate::pane::{alloc_pane_id, PaneId};
use crate::tab::{SplitDirection, SplitRequest, SplitSize, Tab, TabId};
@@ -197,7 +197,7 @@ impl TmuxDomainState {
master_pane: ref_pane,
};
- let writer = WriterWrapper::new(pane_pty.take_writer()?);
+ let writer = pane_pty.take_writer()?;
let size = TerminalSize {
rows: pane.pane_height as usize,
@@ -216,15 +216,16 @@ impl TmuxDomainState {
std::sync::Arc::new(config::TermConfig::new()),
"ThinkTerm",
config::wezterm_version(),
- Box::new(writer.clone()),
+ writer,
);
+ let pane_writer = terminal.writer_handle();
Ok(Arc::new(LocalPane::new(
local_pane_id,
terminal,
Box::new(child),
Box::new(pane_pty),
- Box::new(writer),
+ pane_writer,
self.domain_id,
"tmux pane".to_string(),
)))
diff --git a/term/src/terminalstate/mod.rs b/term/src/terminalstate/mod.rs
index 753c9115..3c9f9f42 100644
--- a/term/src/terminalstate/mod.rs
+++ b/term/src/terminalstate/mod.rs
@@ -528,6 +528,15 @@ impl ThreadedWriter {
Self { sender }
}
+
+ /// Another way into the same writer thread: what is written through
+ /// the handle goes out behind what the terminal flushed before it.
+ #[cfg(not(target_family = "wasm"))]
+ fn handle(&self) -> Self {
+ Self {
+ sender: self.sender.clone(),
+ }
+ }
}
#[cfg(target_family = "wasm")]
@@ -935,6 +944,19 @@ impl TerminalState {
/// in the bracketing, otherwise it is fed to the writer as-is.
/// De-fang the text by removing any embedded bracketed paste
/// sequence that may be present.
+ /// A writer to the same pty as the terminal's own, usable without the
+ /// terminal. Bytes the embedder encodes itself -- kitty-protocol keys,
+ /// text from an input method, a Ctrl-C for a program the parser is
+ /// busy with -- go out through the terminal's writer thread, behind
+ /// whatever the terminal flushed before them (a key, a paste, a reply
+ /// to a query) and never ahead: one pty, one order. A writer that went
+ /// to the pty directly raced that thread, and a paste applied a moment
+ /// before a direct write landed after it.
+ #[cfg(not(target_family = "wasm"))]
+ pub fn writer_handle(&self) -> Box<dyn std::io::Write + Send> {
+ Box::new(self.writer.get_ref().handle())
+ }
+
pub fn send_paste(&mut self, text: &str) -> Result<(), Error> {
let mut buf = String::new();
if self.bracketed_paste {
diff --git a/term/src/test/mod.rs b/term/src/test/mod.rs
index 81d38a7b..600bbb94 100644
--- a/term/src/test/mod.rs
+++ b/term/src/test/mod.rs
@@ -1516,3 +1516,53 @@ fn test_hyperlinks() {
Compare::TEXT | Compare::ATTRS,
);
}
+
+/// What the embedder writes through the terminal's writer handle goes out
+/// behind what the terminal itself wrote, never ahead: one pty, one order.
+#[cfg(not(target_family = "wasm"))]
+#[test]
+fn the_writer_handle_keeps_the_terminals_order() {
+ use std::io::Write;
+
+ #[derive(Clone, Default)]
+ struct Sink(Arc<Mutex<Vec<u8>>>);
+
+ impl std::io::Write for Sink {
+ fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
+ self.0.lock().unwrap().extend_from_slice(buf);
+ Ok(buf.len())
+ }
+ fn flush(&mut self) -> std::io::Result<()> {
+ Ok(())
+ }
+ }
+
+ let sink = Sink::default();
+ let mut term = Terminal::new(
+ TerminalSize {
+ rows: 24,
+ cols: 80,
+ pixel_width: 640,
+ pixel_height: 384,
+ dpi: 0,
+ },
+ Arc::new(TestTermConfig { scrollback: 0 }),
+ "ThinkTerm",
+ "O_o",
+ Box::new(sink.clone()),
+ );
+ let mut handle = term.writer_handle();
+ let mut expected = Vec::new();
+ for _ in 0..50 {
+ // A paste the terminal applies, then bytes the embedder encoded.
+ term.send_paste("p").unwrap();
+ handle.write_all(b"h").unwrap();
+ expected.extend_from_slice(b"ph");
+ }
+ let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
+ while sink.0.lock().unwrap().len() < expected.len() && std::time::Instant::now() < deadline
+ {
+ std::thread::sleep(std::time::Duration::from_millis(5));
+ }
+ assert_eq!(sink.0.lock().unwrap().clone(), expected);
+}