diff --git a/codec/src/lib.rs b/codec/src/lib.rs index 527f7cf2e..08432da03 100644 --- a/codec/src/lib.rs +++ b/codec/src/lib.rs @@ -416,7 +416,7 @@ macro_rules! pdu { /// The overall version of the codec. /// This must be bumped when backwards incompatible changes /// are made to the types and protocol. -pub const CODEC_VERSION: usize = 23; +pub const CODEC_VERSION: usize = 24; // Defines the Pdu enum. // Each struct has an explicit identifying number. @@ -596,6 +596,9 @@ pub struct SplitPane { pub command: Option, pub command_dir: Option, pub domain: config::keyassignment::SpawnTabDomain, + /// Instead of spawning a command, move the specified + /// pane into the new split target + pub move_pane_id: Option, } #[derive(Deserialize, Serialize, PartialEq, Debug)] diff --git a/mux/src/domain.rs b/mux/src/domain.rs index 6de09afdd..2a0fdeab2 100644 --- a/mux/src/domain.rs +++ b/mux/src/domain.rs @@ -31,6 +31,15 @@ pub fn alloc_domain_id() -> DomainId { DOMAIN_ID.fetch_add(1, ::std::sync::atomic::Ordering::Relaxed) } +#[derive(Debug, Clone, PartialEq)] +pub enum SplitSource { + Spawn { + command: Option, + command_dir: Option, + }, + MovePane(PaneId), +} + #[async_trait(?Send)] pub trait Domain: Downcast { /// Spawn a new command within this domain @@ -55,8 +64,7 @@ pub trait Domain: Downcast { async fn split_pane( &self, - command: Option, - command_dir: Option, + source: SplitSource, tab: TabId, pane_id: PaneId, split_request: SplitRequest, @@ -81,9 +89,34 @@ pub trait Domain: Downcast { None => anyhow::bail!("invalid pane index {}", pane_index), }; - let pane = self - .spawn_pane(split_size.second, command, command_dir) - .await?; + let pane = match source { + SplitSource::Spawn { + command, + command_dir, + } => { + self.spawn_pane(split_size.second, command, command_dir) + .await? + } + SplitSource::MovePane(src_pane_id) => { + let (_domain, _window, src_tab) = mux + .resolve_pane_id(src_pane_id) + .ok_or_else(|| anyhow::anyhow!("pane {} not found", src_pane_id))?; + let src_tab = match mux.get_tab(src_tab) { + Some(t) => t, + None => anyhow::bail!("Invalid tab id {}", src_tab), + }; + + let pane = src_tab.remove_pane(src_pane_id).ok_or_else(|| { + anyhow::anyhow!("pane {} not found in its containing tab!?", src_pane_id) + })?; + + if src_tab.is_dead() { + mux.remove_tab(src_tab.tab_id()); + } + + pane + } + }; tab.split_and_insert(pane_index, split_request, Rc::clone(&pane))?; Ok(pane) diff --git a/mux/src/lib.rs b/mux/src/lib.rs index bc932c63c..c3a67eb73 100644 --- a/mux/src/lib.rs +++ b/mux/src/lib.rs @@ -5,7 +5,7 @@ use crate::window::{Window, WindowId}; use anyhow::{anyhow, Context, Error}; use config::keyassignment::SpawnTabDomain; use config::{configuration, ExitBehavior}; -use domain::{Domain, DomainId, DomainState}; +use domain::{Domain, DomainId, DomainState, SplitSource}; use filedescriptor::{socketpair, AsRawSocketDescriptor, FileDescriptor}; #[cfg(unix)] use libc::{SOL_SOCKET, SO_RCVBUF, SO_SNDBUF}; @@ -942,8 +942,7 @@ impl Mux { // TODO: disambiguate with TabId pane_id: PaneId, request: SplitRequest, - command: Option, - command_dir: Option, + source: SplitSource, domain: config::keyassignment::SpawnTabDomain, ) -> anyhow::Result<(Rc, PtySize)> { let (_pane_domain_id, window_id, tab_id) = self @@ -963,11 +962,18 @@ impl Mux { .ok_or_else(|| anyhow!("pane_id {} is invalid", pane_id))?; let term_config = current_pane.get_config(); - let cwd = self.resolve_cwd(command_dir, Some(Rc::clone(¤t_pane))); + let source = match source { + SplitSource::Spawn { + command, + command_dir, + } => SplitSource::Spawn { + command, + command_dir: self.resolve_cwd(command_dir, Some(Rc::clone(¤t_pane))), + }, + other => other, + }; - let pane = domain - .split_pane(command, cwd, tab_id, pane_id, request) - .await?; + let pane = domain.split_pane(source, tab_id, pane_id, request).await?; if let Some(config) = term_config { pane.set_config(config); } diff --git a/mux/src/tab.rs b/mux/src/tab.rs index ee89f0293..d96cbdb41 100644 --- a/mux/src/tab.rs +++ b/mux/src/tab.rs @@ -1264,18 +1264,35 @@ impl Tab { } pub fn prune_dead_panes(&self) -> bool { - self.remove_pane_if(|_, pane| pane.is_dead()) + !self + .remove_pane_if(|_, pane| pane.is_dead(), true) + .is_empty() } pub fn kill_pane(&self, pane_id: PaneId) -> bool { - self.remove_pane_if(|_, pane| pane.pane_id() == pane_id) + !self + .remove_pane_if(|_, pane| pane.pane_id() == pane_id, true) + .is_empty() } pub fn kill_panes_in_domain(&self, domain: DomainId) -> bool { - self.remove_pane_if(|_, pane| pane.domain_id() == domain) + !self + .remove_pane_if(|_, pane| pane.domain_id() == domain, true) + .is_empty() } - fn remove_pane_if(&self, f: F) -> bool + /// Remove pane from tab. + /// The pane is still live in the mux; the intent is for the pane to + /// be added to a different tab. + pub fn remove_pane(&self, pane_id: PaneId) -> Option> { + let panes = self.remove_pane_if(|_, pane| pane.pane_id() == pane_id, false); + for pane in panes { + return Some(pane); + } + None + } + + fn remove_pane_if(&self, f: F, kill: bool) -> Vec> where F: Fn(usize, &Rc) -> bool, { @@ -1316,7 +1333,7 @@ impl Tab { let parent; match cursor.unsplit_leaf() { Ok((c, dead, p)) => { - dead_panes.push(dead.pane_id()); + dead_panes.push(dead); parent = p.unwrap(); cursor = c; } @@ -1324,7 +1341,7 @@ impl Tab { // We might be the root, for example if c.is_top() && c.is_leaf() { root.replace(Tree::Empty); - dead_panes.push(pane.pane_id()); + dead_panes.push(pane); } else { root.replace(c.tree()); } @@ -1366,18 +1383,17 @@ impl Tab { *self.active.borrow_mut() = active_idx; } - if !dead_panes.is_empty() { + if !dead_panes.is_empty() && kill { + let to_kill: Vec<_> = dead_panes.iter().map(|p| p.pane_id()).collect(); promise::spawn::spawn_into_main_thread(async move { let mux = Mux::get().unwrap(); - for pane_id in dead_panes.into_iter() { + for pane_id in to_kill.into_iter() { mux.remove_pane(pane_id); } }) .detach(); - true - } else { - false } + dead_panes } pub fn can_close_without_prompting(&self, reason: CloseReason) -> bool { diff --git a/wezterm-client/src/domain.rs b/wezterm-client/src/domain.rs index 3d19c28cb..685f9e4ee 100644 --- a/wezterm-client/src/domain.rs +++ b/wezterm-client/src/domain.rs @@ -6,7 +6,7 @@ use codec::{ListPanesResponse, SpawnV2, SplitPane}; use config::keyassignment::SpawnTabDomain; use config::{SshDomain, TlsDomainClient, UnixDomain}; use mux::connui::{ConnectionUI, ConnectionUIParams}; -use mux::domain::{alloc_domain_id, Domain, DomainId, DomainState}; +use mux::domain::{alloc_domain_id, Domain, DomainId, DomainState, SplitSource}; use mux::pane::{Pane, PaneId}; use mux::tab::{SplitRequest, Tab, TabId}; use mux::window::WindowId; @@ -537,8 +537,7 @@ impl Domain for ClientDomain { async fn split_pane( &self, - command: Option, - command_dir: Option, + source: SplitSource, tab_id: TabId, pane_id: PaneId, split_request: SplitRequest, @@ -559,6 +558,14 @@ impl Domain for ClientDomain { .downcast_ref::() .ok_or_else(|| anyhow!("pane_id {} is not a ClientPane", pane_id))?; + let (command, command_dir, move_pane_id) = match source { + SplitSource::Spawn { + command, + command_dir, + } => (command, command_dir, None), + SplitSource::MovePane(move_pane_id) => (None, None, Some(move_pane_id)), + }; + let result = inner .client .split_pane(SplitPane { @@ -567,6 +574,7 @@ impl Domain for ClientDomain { split_request, command, command_dir, + move_pane_id, }) .await?; diff --git a/wezterm-gui/src/termwindow/spawn.rs b/wezterm-gui/src/termwindow/spawn.rs index 204968115..e662f2969 100644 --- a/wezterm-gui/src/termwindow/spawn.rs +++ b/wezterm-gui/src/termwindow/spawn.rs @@ -3,6 +3,7 @@ use anyhow::{anyhow, bail, Context}; use config::keyassignment::{SpawnCommand, SpawnTabDomain}; use config::TermConfig; use mux::activity::Activity; +use mux::domain::SplitSource; use mux::tab::SplitRequest; use mux::Mux; use portable_pty::{CommandBuilder, PtySize}; @@ -102,8 +103,10 @@ impl super::TermWindow { // tab.tab_id(), pane.pane_id(), direction, - cmd_builder, - cwd, + SplitSource::Spawn { + command: cmd_builder, + command_dir: cwd, + }, spawn.domain, ) .await diff --git a/wezterm-mux-server-impl/src/sessionhandler.rs b/wezterm-mux-server-impl/src/sessionhandler.rs index 731c9472e..cead79ac0 100644 --- a/wezterm-mux-server-impl/src/sessionhandler.rs +++ b/wezterm-mux-server-impl/src/sessionhandler.rs @@ -2,6 +2,7 @@ use crate::PKI; use anyhow::{anyhow, Context}; use codec::*; use mux::client::ClientId; +use mux::domain::SplitSource; use mux::pane::{Pane, PaneId}; use mux::renderable::{RenderableDimensions, StableCursorPosition}; use mux::tab::TabId; @@ -719,14 +720,17 @@ async fn split_pane(split: SplitPane, client_id: Option>) -> anyho .resolve_pane_id(split.pane_id) .ok_or_else(|| anyhow!("pane_id {} invalid", split.pane_id))?; + let source = if let Some(move_pane_id) = split.move_pane_id { + SplitSource::MovePane(move_pane_id) + } else { + SplitSource::Spawn { + command: split.command, + command_dir: split.command_dir, + } + }; + let (pane, size) = mux - .split_pane( - split.pane_id, - split.split_request, - split.command, - split.command_dir, - split.domain, - ) + .split_pane(split.pane_id, split.split_request, source, split.domain) .await?; Ok::(Pdu::SpawnResponse(SpawnResponse { diff --git a/wezterm/src/main.rs b/wezterm/src/main.rs index d55c31164..d0f759b58 100644 --- a/wezterm/src/main.rs +++ b/wezterm/src/main.rs @@ -212,6 +212,11 @@ Outputs the pane-id for the newly created pane on success" #[structopt(long, parse(from_os_str))] cwd: Option, + /// Instead of spawning a new command, move the specified + /// pane into the newly created split. + #[structopt(long, conflicts_with_all=&["cwd", "prog"])] + move_pane_id: Option, + /// Instead of executing your shell, run PROG. /// For example: `wezterm cli split-pane -- bash -l` will spawn bash /// as if it were a login shell. @@ -787,6 +792,7 @@ async fn run_cli_async(config: config::ConfigHandle, cli: CliCommand) -> anyhow: top_level, cells, percent, + move_pane_id, } => { let pane_id = resolve_pane_id(&client, pane_id).await?; @@ -823,6 +829,7 @@ async fn run_cli_async(config: config::ConfigHandle, cli: CliCommand) -> anyhow: Some(builder) }, command_dir: canon_cwd(cwd)?, + move_pane_id, }) .await?;