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.
Rust agentagent-orchestrationaiai-agentsclaude-codecodexcoding-agentlinuxmacosmultiplayermultiplexerrustterminalterminal-emulatorterminal-multiplexertmuxtuiweztermwindows
use crate::tmux::{RefTmuxRemotePane, TmuxCmdQueue, TmuxDomainState};
use crate::tmux_commands::{Resize, SendKeys};
use crate::DomainId;
use filedescriptor::FileDescriptor;
use parking_lot::{Condvar, Mutex};
use portable_pty::{Child, ChildKiller, ExitStatus, MasterPty};
use std::io::{Read, Write};
use std::sync::Arc;
/// A local tmux pane(tab) based on a tmux pty
#[derive(Debug)]
pub(crate) struct TmuxPty {
pub domain_id: DomainId,
pub master_pane: RefTmuxRemotePane,
pub reader: FileDescriptor,
pub cmd_queue: Arc<Mutex<TmuxCmdQueue>>,
}
struct TmuxPtyWriter {
domain_id: DomainId,
master_pane: RefTmuxRemotePane,
cmd_queue: Arc<Mutex<TmuxCmdQueue>>,
}
impl Write for TmuxPtyWriter {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
let pane_id = {
let pane_lock = self.master_pane.lock();
pane_lock.pane_id
};
log::trace!("pane:{}, content:{:?}", &pane_id, buf);
let mut cmd_queue = self.cmd_queue.lock();
cmd_queue.push_back(Box::new(SendKeys {
pane: pane_id,
keys: buf.to_vec(),
}));
TmuxDomainState::schedule_send_next_command(self.domain_id);
Ok(buf.len())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
impl Write for TmuxPty {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
let pane_id = {
let pane_lock = self.master_pane.lock();
pane_lock.pane_id
};
log::trace!("pane:{}, content:{:?}", &pane_id, buf);
let mut cmd_queue = self.cmd_queue.lock();
cmd_queue.push_back(Box::new(SendKeys {
pane: pane_id,
keys: buf.to_vec(),
}));
TmuxDomainState::schedule_send_next_command(self.domain_id);
Ok(buf.len())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
#[derive(Clone, Debug)]
pub(crate) struct TmuxChild {
pub active_lock: Arc<(Mutex<bool>, Condvar)>,
}
impl Child for TmuxChild {
fn try_wait(&mut self) -> std::io::Result<Option<portable_pty::ExitStatus>> {
todo!()
}
fn wait(&mut self) -> std::io::Result<portable_pty::ExitStatus> {
let &(ref lock, ref var) = &*self.active_lock;
let mut released = lock.lock();
while !*released {
var.wait(&mut released);
}
return Ok(ExitStatus::with_exit_code(0));
}
fn process_id(&self) -> Option<u32> {
None
}
#[cfg(windows)]
fn as_raw_handle(&self) -> Option<std::os::windows::io::RawHandle> {
None
}
}
#[derive(Clone, Debug)]
struct TmuxChildKiller {}
impl ChildKiller for TmuxChildKiller {
fn kill(&mut self) -> std::io::Result<()> {
Err(std::io::Error::new(
std::io::ErrorKind::Other,
"TmuxChildKiller: kill not implemented!",
))
}
fn clone_killer(&self) -> Box<dyn ChildKiller + Send + Sync> {
Box::new(self.clone())
}
}
impl ChildKiller for TmuxChild {
fn kill(&mut self) -> std::io::Result<()> {
Err(std::io::Error::new(
std::io::ErrorKind::Other,
"TmuxPty: kill not implemented!",
))
}
fn clone_killer(&self) -> Box<dyn ChildKiller + Send + Sync> {
Box::new(TmuxChildKiller {})
}
}
impl MasterPty for TmuxPty {
fn resize(&self, size: portable_pty::PtySize) -> Result<(), anyhow::Error> {
let mut cmd_queue = self.cmd_queue.lock();
cmd_queue.push_back(Box::new(Resize {
size,
pane_id: self.master_pane.lock().pane_id,
}));
TmuxDomainState::schedule_send_next_command(self.domain_id);
Ok(())
}
fn get_size(&self) -> Result<portable_pty::PtySize, anyhow::Error> {
let pane = self.master_pane.lock();
Ok(portable_pty::PtySize {
rows: pane.pane_height as u16,
cols: pane.pane_width as u16,
pixel_width: 0,
pixel_height: 0,
})
}
fn try_clone_reader(&self) -> Result<Box<dyn Read + Send>, anyhow::Error> {
Ok(Box::new(self.reader.try_clone()?))
}
fn take_writer(&self) -> Result<Box<dyn Write + Send>, anyhow::Error> {
Ok(Box::new(TmuxPtyWriter {
domain_id: self.domain_id,
master_pane: self.master_pane.clone(),
cmd_queue: self.cmd_queue.clone(),
}))
}
#[cfg(unix)]
fn process_group_leader(&self) -> Option<libc::pid_t> {
return None;
}
#[cfg(unix)]
fn as_raw_fd(&self) -> Option<std::os::fd::RawFd> {
None
}
#[cfg(unix)]
fn tty_name(&self) -> Option<std::path::PathBuf> {
None
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tmux::TmuxRemotePane;
use filedescriptor::Pipe;
use std::collections::VecDeque;
/// The terminal's writer thread uses `write_all`, which gives up on a
/// write that reports nothing written, and with it on the pane's input.
#[test]
fn queued_keys_count_as_written() {
static SCHEDULER: std::sync::Once = std::sync::Once::new();
SCHEDULER.call_once(|| {
promise::spawn::SimpleExecutor::new();
});
let mut writer = TmuxPtyWriter {
domain_id: 0,
master_pane: Arc::new(Mutex::new(TmuxRemotePane {
local_pane_id: 0,
output_write: Pipe::new().unwrap().write,
active_lock: Arc::new((Mutex::new(false), Condvar::new())),
session_id: 0,
window_id: 0,
pane_id: 0,
cursor_x: 0,
cursor_y: 0,
pane_width: 80,
pane_height: 24,
pane_left: 0,
pane_top: 0,
})),
cmd_queue: Arc::new(Mutex::new(VecDeque::new())),
};
writer.write_all(b"first").unwrap();
writer.write_all(b"second").unwrap();
assert_eq!(writer.cmd_queue.lock().len(), 2);
}
}