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
//! An abstraction over a terminal device
#[cfg(any(all(unix, not(target_os = "ios")), windows))]
use crate::caps::probed::ProbeCapabilities;
#[cfg(any(all(unix, not(target_os = "ios")), windows))]
use crate::caps::Capabilities;
#[cfg(any(all(unix, not(target_os = "ios")), windows))]
use crate::input::InputEvent;
#[cfg(any(all(unix, not(target_os = "ios")), windows))]
use crate::surface::Change;
#[cfg(any(all(unix, not(target_os = "ios")), windows))]
use crate::{format_err, Result};
#[cfg(any(all(unix, not(target_os = "ios")), windows))]
use num_traits::NumCast;
#[cfg(any(all(unix, not(target_os = "ios")), windows))]
use std::fmt::Display;
#[cfg(any(all(unix, not(target_os = "ios")), windows))]
use std::time::Duration;
#[cfg(feature = "use_serde")]
use serde::Deserialize;
#[cfg(feature = "use_serde")]
use serde::Serialize;
#[cfg(all(unix, not(target_os = "ios")))]
pub mod unix;
#[cfg(windows)]
pub mod windows;
// Everything below that drives a live terminal -- the Terminal trait, its
// buffered wrapper and new_terminal -- exists only where there is a terminal
// to drive. ScreenSize and Blocking stay unconditional: they are plain data
// that the surface and cell layers use on every target, wasm included.
#[cfg(any(all(unix, not(target_os = "ios")), windows))]
pub mod buffered;
#[cfg(all(unix, not(target_os = "ios")))]
pub use self::unix::{UnixTerminal, UnixTerminalWaker as TerminalWaker};
#[cfg(windows)]
pub use self::windows::{WindowsTerminal, WindowsTerminalWaker as TerminalWaker};
/// Represents the size of the terminal screen.
/// The number of rows and columns of character cells are expressed.
/// Some implementations populate the size of those cells in pixels.
// On Windows, GetConsoleFontSize() can return the size of a cell in
// logical units and we can probably use this to populate xpixel, ypixel.
// GetConsoleScreenBufferInfo() can return the rows and cols.
#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ScreenSize {
/// The number of rows of text
pub rows: usize,
/// The number of columns per row
pub cols: usize,
/// The width of a cell in pixels. Some implementations never
/// set this to anything other than zero.
pub xpixel: usize,
/// The height of a cell in pixels. Some implementations never
/// set this to anything other than zero.
pub ypixel: usize,
}
#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Blocking {
DoNotWait,
Wait,
}
/// `Terminal` abstracts over some basic terminal capabilities.
/// If the `set_raw_mode` or `set_cooked_mode` functions are used in
/// any combination, the implementation is required to restore the
/// terminal mode that was in effect when it was created.
#[cfg(any(all(unix, not(target_os = "ios")), windows))]
pub trait Terminal {
/// Raw mode disables input line buffering, allowing data to be
/// read as the user presses keys, disables local echo, so keys
/// pressed by the user do not implicitly render to the terminal
/// output, and disables canonicalization of unix newlines to CRLF.
fn set_raw_mode(&mut self) -> Result<()>;
fn set_cooked_mode(&mut self) -> Result<()>;
/// Enter the alternate screen. The alternate screen will be left
/// automatically when the `Terminal` is dropped.
fn enter_alternate_screen(&mut self) -> Result<()>;
/// Exit the alternate screen.
fn exit_alternate_screen(&mut self) -> Result<()>;
/// Queries the current screen size, returning width, height.
fn get_screen_size(&mut self) -> Result<ScreenSize>;
/// Returns a capability probing helper that will use escape
/// sequences to attempt to probe information from the terminal
fn probe_capabilities(&mut self) -> Option<ProbeCapabilities<'_>> {
None
}
/// Sets the current screen size
fn set_screen_size(&mut self, size: ScreenSize) -> Result<()>;
/// Render a series of changes to the terminal output
fn render(&mut self, changes: &[Change]) -> Result<()>;
/// Flush any buffered output
fn flush(&mut self) -> Result<()>;
/// Check for a parsed input event.
/// `wait` indicates the behavior in the case that no input is
/// immediately available. If wait is `None` then `poll_input`
/// will not return until an event is available. If wait is
/// `Some(duration)` then `poll_input` will wait up to the given
/// duration for an event before returning with a value of
/// `Ok(None)`. If wait is `Some(Duration::ZERO)` then the
/// poll is non-blocking.
///
/// The possible values returned as `InputEvent`s depend on the
/// mode of the terminal. Most values are not returned unless
/// the terminal is set to raw mode.
fn poll_input(&mut self, wait: Option<Duration>) -> Result<Option<InputEvent>>;
fn waker(&self) -> TerminalWaker;
}
/// `SystemTerminal` is a concrete implementation of `Terminal`.
/// Ideally you wouldn't reference `SystemTerminal` in consuming
/// code. This type is exposed for convenience if you are doing
/// something unusual and want easier access to the constructors.
#[cfg(all(unix, not(target_os = "ios")))]
pub type SystemTerminal = UnixTerminal;
#[cfg(windows)]
pub type SystemTerminal = WindowsTerminal;
/// Construct a new instance of Terminal.
/// The terminal will have a renderer that is influenced by the configuration
/// in the provided `Capabilities` instance.
/// The terminal will explicitly open `/dev/tty` on Unix systems and
/// `CONIN$` and `CONOUT$` on Windows systems, so that it should yield a
/// functioning console with minimal headaches.
/// If you have a more advanced use case you will want to look to the
/// constructors for `UnixTerminal` and `WindowsTerminal` and call whichever
/// one is most suitable for your needs.
#[cfg(any(all(unix, not(target_os = "ios")), windows))]
pub fn new_terminal(caps: Capabilities) -> Result<impl Terminal> {
SystemTerminal::new(caps)
}
#[cfg(any(all(unix, not(target_os = "ios")), windows))]
pub(crate) fn cast<T: NumCast + Display + Copy, U: NumCast>(n: T) -> Result<U> {
num_traits::cast(n).ok_or_else(|| format_err!("{} is out of bounds for this system", n))
}