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
//! Bridge our gui config into the terminal crate configuration
use crate::{configuration, ConfigHandle, NewlineCanon};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Mutex;
use termwiz::cell::UnicodeVersion;
use wezterm_term::color::ColorPalette;
use wezterm_term::config::BidiMode;
#[derive(Debug)]
pub struct TermConfig {
config: Mutex<Option<ConfigHandle>>,
/// Bumped by every `set_config`. With the global configuration's
/// generation it forms `change_key`, so the terminal can tell cheaply
/// whether settings it already read are still current.
version: AtomicUsize,
client_palette: Mutex<Option<ColorPalette>>,
/// True while `client_palette` is a scheme being *shown* rather than
/// chosen. See [`TermConfig::set_preview_palette`].
client_palette_is_preview: Mutex<bool>,
}
impl TermConfig {
pub fn new() -> Self {
Self {
config: Mutex::new(None),
version: AtomicUsize::new(0),
client_palette: Mutex::new(None),
client_palette_is_preview: Mutex::new(false),
}
}
pub fn with_config(config: ConfigHandle) -> Self {
Self {
config: Mutex::new(Some(config)),
version: AtomicUsize::new(0),
client_palette: Mutex::new(None),
client_palette_is_preview: Mutex::new(false),
}
}
pub fn set_config(&self, config: ConfigHandle) {
let mut current = self.config.lock().unwrap();
current.replace(config);
// Published before the lock is released, like the global generation.
self.version.fetch_add(1, Ordering::Release);
}
pub fn set_client_palette(&self, palette: ColorPalette) {
self.client_palette.lock().unwrap().replace(palette);
*self.client_palette_is_preview.lock().unwrap() = false;
}
/// A palette the window is *showing* rather than choosing.
///
/// Rendered like any other client palette, but a remote pane must not
/// carry it any further: telling the mux server would leave a scheme the
/// user may abandon with Escape installed as the server's answer to OSC
/// colour queries, and would spend a round trip per pane on every arrow
/// key through a thousand-entry list.
pub fn set_preview_palette(&self, palette: ColorPalette) {
self.client_palette.lock().unwrap().replace(palette);
*self.client_palette_is_preview.lock().unwrap() = true;
}
pub fn client_palette_is_preview(&self) -> bool {
*self.client_palette_is_preview.lock().unwrap()
}
pub fn clear_client_palette(&self) {
self.client_palette.lock().unwrap().take();
*self.client_palette_is_preview.lock().unwrap() = false;
}
fn configuration(&self) -> ConfigHandle {
match self.config.lock().unwrap().as_ref() {
Some(h) => h.clone(),
None => configuration(),
}
}
}
impl wezterm_term::TerminalConfiguration for TermConfig {
fn generation(&self) -> usize {
self.configuration().generation()
}
fn change_key(&self) -> Option<(usize, usize)> {
Some((
self.version.load(Ordering::Acquire),
crate::configuration_generation(),
))
}
fn scrollback_size(&self) -> usize {
self.configuration().scrollback_lines
}
fn enable_csi_u_key_encoding(&self) -> bool {
self.configuration().enable_csi_u_key_encoding
}
fn color_palette(&self) -> ColorPalette {
let client_palette = self.client_palette.lock().unwrap();
if let Some(p) = client_palette.as_ref().cloned() {
return p;
}
let config = self.configuration();
config.resolved_palette.clone().into()
}
fn alternate_buffer_wheel_scroll_speed(&self) -> u8 {
self.configuration().alternate_buffer_wheel_scroll_speed
}
fn enq_answerback(&self) -> String {
configuration().enq_answerback.clone()
}
fn enable_kitty_graphics(&self) -> bool {
self.configuration().enable_kitty_graphics
}
fn kitty_image_memory_budget(&self) -> usize {
match self.configuration().kitty_image_memory_budget_mib {
// The natural reading of 0, and the safe one: taking it
// literally would evict every picture on arrival.
0 => usize::MAX,
mib => (mib as usize).saturating_mul(1024 * 1024),
}
}
fn enable_title_reporting(&self) -> bool {
self.configuration().enable_title_reporting
}
fn enable_checksum_rectangular_area(&self) -> bool {
self.configuration().enable_checksum_rectangular_area
}
fn enable_kitty_keyboard(&self) -> bool {
self.configuration().enable_kitty_keyboard
}
fn canonicalize_pasted_newlines(&self) -> wezterm_term::config::NewlineCanon {
match self.configuration().canonicalize_pasted_newlines {
None => wezterm_term::config::NewlineCanon::default(),
Some(NewlineCanon::None) => wezterm_term::config::NewlineCanon::None,
Some(NewlineCanon::LineFeed) => wezterm_term::config::NewlineCanon::LineFeed,
Some(NewlineCanon::CarriageReturn) => {
wezterm_term::config::NewlineCanon::CarriageReturn
}
Some(NewlineCanon::CarriageReturnAndLineFeed) => {
wezterm_term::config::NewlineCanon::CarriageReturnAndLineFeed
}
}
}
fn unicode_version(&self) -> UnicodeVersion {
let config = self.configuration();
config.unicode_version()
}
fn debug_key_events(&self) -> bool {
self.configuration().debug_key_events
}
fn log_unknown_escape_sequences(&self) -> bool {
self.configuration().log_unknown_escape_sequences
}
fn normalize_output_to_unicode_nfc(&self) -> bool {
self.configuration().normalize_output_to_unicode_nfc
}
fn bidi_mode(&self) -> BidiMode {
let config = self.configuration();
BidiMode {
enabled: config.bidi_enabled,
hint: config.bidi_direction,
}
}
}
#[cfg(test)]
mod test {
use super::*;
use wezterm_term::TerminalConfiguration;
#[test]
fn change_key_moves_with_every_set_config() {
let config = TermConfig::new();
let before = config.change_key();
assert!(before.is_some());
config.set_config(crate::configuration());
let after = config.change_key();
assert_ne!(after, before);
config.set_config(crate::configuration());
assert_ne!(config.change_key(), after);
}
}