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::cli::resolve_relative_cwd;
use clap::{Parser, ValueHint};
use mux::pane::PaneId;
use mux::command_spec::{CommandSpec, CommandSpecExt};
use mux::tab::{SplitDirection, SplitRequest, SplitSize};
use portable_pty::cmdbuilder::CommandBuilder;
use std::ffi::OsString;
use wezterm_client::client::Client;
#[derive(Debug, Parser, Clone)]
pub struct SplitPane {
/// Specify the pane that should be split.
/// The default is to use the current pane based on the
/// environment variable WEZTERM_PANE.
#[arg(long)]
pane_id: Option<PaneId>,
/// The pane by name rather than id: its label, its thread's name (a
/// thread with one pane), or `thread/label`.
#[arg(long, conflicts_with = "pane_id")]
pane: Option<String>,
/// Equivalent to `--right`. If neither this nor any other direction
/// is specified, the default is equivalent to `--bottom`.
#[arg(long, conflicts_with_all=&["left", "right", "top", "bottom"])]
horizontal: bool,
/// Split horizontally, with the new pane on the left
#[arg(long, conflicts_with_all=&["right", "top", "bottom"])]
left: bool,
/// Split horizontally, with the new pane on the right
#[arg(long, conflicts_with_all=&["left", "top", "bottom"])]
right: bool,
/// Split vertically, with the new pane on the top
#[arg(long, conflicts_with_all=&["left", "right", "bottom"])]
top: bool,
/// Split vertically, with the new pane on the bottom
#[arg(long, conflicts_with_all=&["left", "right", "top"])]
bottom: bool,
/// Rather than splitting the active pane, split the entire
/// window.
#[arg(long)]
top_level: bool,
/// The number of cells that the new split should have.
/// If omitted, 50% of the available space is used.
#[arg(long)]
cells: Option<usize>,
/// Specify the number of cells that the new split should
/// have, expressed as a percentage of the available space.
#[arg(long, conflicts_with = "cells")]
percent: Option<u8>,
/// Specify the current working directory for the initially
/// spawned program
#[arg(long, value_parser, value_hint=ValueHint::DirPath)]
cwd: Option<OsString>,
/// Instead of spawning a new command, move the specified
/// pane into the newly created split.
#[arg(long, conflicts_with_all=&["cwd", "prog"])]
move_pane_id: Option<PaneId>,
/// Wait for the program to end, and exit with its status. The pane
/// id is printed first, as without it.
#[arg(long, conflicts_with = "move_pane_id")]
wait: bool,
/// With --wait, give up after this long, exiting with status 124: a
/// number of seconds, or a number with s, m or h after it. `--for` is
/// the same.
#[arg(
long,
visible_alias = "for",
value_parser = crate::cli::wait::parse_timeout,
requires = "wait"
)]
timeout: Option<std::time::Duration>,
/// Label the new pane, so that `--pane LABEL` finds it.
#[arg(long)]
label: Option<String>,
/// Instead of executing your shell, run PROG.
/// For example: `thinkterm cli split-pane -- bash -l` will spawn bash
/// as if it were a login shell.
#[arg(value_parser, value_hint=ValueHint::CommandWithArguments, num_args=1..)]
prog: Vec<OsString>,
}
impl SplitPane {
pub async fn run(self, client: Client) -> anyhow::Result<()> {
// Checked before anything is spawned.
let label = match &self.label {
Some(text) => crate::cli::set_pane_label::checked_label(text)?,
None => None,
};
let pane_id =
crate::cli::target::resolve(&client, self.pane_id, self.pane.as_deref()).await?;
let direction = if self.left || self.right || self.horizontal {
SplitDirection::Horizontal
} else if self.top || self.bottom {
SplitDirection::Vertical
} else {
SplitDirection::Vertical
};
let target_is_second = !(self.left || self.top);
let size = match (self.cells, self.percent) {
(Some(c), _) => SplitSize::Cells(c),
(_, Some(p)) => SplitSize::Percent(p),
(None, None) => SplitSize::Percent(50),
};
let split_request = SplitRequest {
direction,
target_is_second,
size,
top_level: self.top_level,
};
let spawned = client
.split_pane(codec::SplitPane {
pane_id,
split_request,
domain: config::keyassignment::SpawnTabDomain::CurrentPaneDomain,
command: if self.prog.is_empty() {
None
} else {
let builder = CommandBuilder::from_argv(self.prog);
Some(CommandSpec::from_command_builder(&builder))
},
command_dir: resolve_relative_cwd(self.cwd)?,
move_pane_id: self.move_pane_id,
})
.await?;
log::debug!("{:?}", spawned);
if let Some(label) = label {
// The pane is there either way, and a script needs its id, and
// what it ran, more than the label: a program that ended at once
// can take its pane with it before the label arrives.
let labelled = client
.set_pane_label(codec::PaneLabelChanged {
pane_id: spawned.pane_id,
label: Some(label),
})
.await;
if let Err(err) = labelled {
eprintln!("pane {} was not labelled: {err:#}", spawned.pane_id);
}
}
println!("{}", spawned.pane_id);
if self.wait {
let deadline = self.timeout.map(|t| std::time::Instant::now() + t);
let code =
crate::cli::wait::wait_for_program(&client, spawned.pane_id, deadline).await?;
crate::cli::wait::exit_with(code);
}
Ok(())
}
}