From 30a9692f9dfabff8e1fe7787ded83e435569973c Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Fri, 14 Jan 2022 11:20:59 -0700 Subject: [PATCH] refactor: centralize some more spawn logic --- mux/src/lib.rs | 67 ++++++++++++------- wezterm-gui/src/termwindow/spawn.rs | 5 -- wezterm-mux-server-impl/src/sessionhandler.rs | 1 + 3 files changed, 43 insertions(+), 30 deletions(-) diff --git a/mux/src/lib.rs b/mux/src/lib.rs index 1ab35ffdd..6993c77a2 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}; +use domain::{Domain, DomainId, DomainState}; use filedescriptor::{socketpair, AsRawSocketDescriptor, FileDescriptor}; #[cfg(unix)] use libc::{SOL_SOCKET, SO_RCVBUF, SO_SNDBUF}; @@ -707,7 +707,7 @@ impl Mux { pane_id: Option, domain: &config::keyassignment::SpawnTabDomain, ) -> anyhow::Result> { - Ok(match domain { + let domain = match domain { SpawnTabDomain::DefaultDomain => self.default_domain(), SpawnTabDomain::CurrentPaneDomain => { let pane_id = pane_id @@ -724,9 +724,40 @@ impl Mux { SpawnTabDomain::DomainName(name) => self .get_domain_by_name(&name) .ok_or_else(|| anyhow!("domain name {} is invalid", name))?, - }) + }; + if domain.state() == DomainState::Detached { + anyhow::bail!("Cannot spawn a tab into a Detached domain"); + } + Ok(domain) } + fn resolve_cwd(&self, command_dir: Option, pane: Option>) -> + Option { + command_dir.or_else(|| { + match pane { + Some(pane) => pane.get_current_working_dir() + .and_then(|url| { + percent_decode_str(url.path()) + .decode_utf8() + .ok() + .map(|path| path.into_owned()) + }) + .map(|path| { + // On Windows the file URI can produce a path like: + // `/C:\Users` which is valid in a file URI, but the leading slash + // is not liked by the windows file APIs, so we strip it off here. + let bytes = path.as_bytes(); + if bytes.len() > 2 && bytes[0] == b'/' && bytes[2] == b':' { + path[1..].to_owned() + } else { + path + } + }), + None => None, + } + }) + } + pub async fn split_pane( &self, // TODO: disambiguate with TabId @@ -747,27 +778,7 @@ impl Mux { .ok_or_else(|| anyhow!("pane_id {} is invalid", pane_id))?; let term_config = current_pane.get_config(); - let cwd = command_dir.or_else(|| { - self.get_pane(pane_id) - .and_then(|pane| pane.get_current_working_dir()) - .and_then(|url| { - percent_decode_str(url.path()) - .decode_utf8() - .ok() - .map(|path| path.into_owned()) - }) - .map(|path| { - // On Windows the file URI can produce a path like: - // `/C:\Users` which is valid in a file URI, but the leading slash - // is not liked by the windows file APIs, so we strip it off here. - let bytes = path.as_bytes(); - if bytes.len() > 2 && bytes[0] == b'/' && bytes[2] == b':' { - path[1..].to_owned() - } else { - path - } - }) - }); + let cwd = self.resolve_cwd(command_dir, Some(Rc::clone(¤t_pane))); let pane = domain .split_pane(command, cwd, tab_id, pane_id, direction) @@ -797,6 +808,7 @@ impl Mux { command: Option, command_dir: Option, size: PtySize, + current_pane_id: Option, ) -> anyhow::Result<(Rc, Rc, WindowId)> { let domain = self.resolve_spawn_tab_domain(None, &domain)?; @@ -824,7 +836,12 @@ impl Mux { (*window_builder, size) }; - let tab = domain.spawn(size, command, command_dir, window_id).await?; + let cwd = self.resolve_cwd(command_dir, match current_pane_id { + Some(id) => self.get_pane(id), + None => None + }); + + let tab = domain.spawn(size, command, cwd, window_id).await?; let pane = tab .get_active_pane() diff --git a/wezterm-gui/src/termwindow/spawn.rs b/wezterm-gui/src/termwindow/spawn.rs index a0db0b9ae..21b249e55 100644 --- a/wezterm-gui/src/termwindow/spawn.rs +++ b/wezterm-gui/src/termwindow/spawn.rs @@ -3,7 +3,6 @@ use anyhow::{anyhow, bail}; use config::keyassignment::{SpawnCommand, SpawnTabDomain}; use config::TermConfig; use mux::activity::Activity; -use mux::domain::DomainState; use mux::tab::SplitDirection; use mux::Mux; use percent_encoding::percent_decode_str; @@ -115,10 +114,6 @@ impl super::TermWindow { } }; - if domain.state() == DomainState::Detached { - bail!("Cannot spawn a tab into a Detached domain"); - } - let cwd = if let Some(cwd) = spawn.cwd.as_ref() { Some(cwd.to_str().map(|s| s.to_owned()).ok_or_else(|| { anyhow!( diff --git a/wezterm-mux-server-impl/src/sessionhandler.rs b/wezterm-mux-server-impl/src/sessionhandler.rs index a882eecae..e3b4b158e 100644 --- a/wezterm-mux-server-impl/src/sessionhandler.rs +++ b/wezterm-mux-server-impl/src/sessionhandler.rs @@ -733,6 +733,7 @@ async fn domain_spawn_v2(spawn: SpawnV2, sender: PduSender) -> anyhow::Result