refactor: move more spawn logic into new mux methods

This commit is contained in:
Wez Furlong committed 2022-01-14 22:28:35 -07:00
1 parent 30a9692f9d
commit 170bb31f4f
4 files changed
+81 -128

No files matched your search

Generated
-1
View File
@@ -4743,7 +4743,6 @@ dependencies = [
"open",
"ordered-float",
"palette",
"percent-encoding",
"portable-pty",
"pretty_env_logger",
"promise",
+45 -27
View File
@@ -731,32 +731,36 @@ impl Mux {
Ok(domain)
}
fn resolve_cwd(&self, command_dir: Option<String>, pane: Option<Rc<dyn Pane>>) ->
Option<String> {
fn resolve_cwd(
&self,
command_dir: Option<String>,
pane: Option<Rc<dyn Pane>>,
) -> Option<String> {
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
}
}),
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,
@@ -771,7 +775,9 @@ impl Mux {
.resolve_pane_id(pane_id)
.ok_or_else(|| anyhow!("pane_id {} invalid", pane_id))?;
let domain = self.resolve_spawn_tab_domain(Some(pane_id), &domain)?;
let domain = self
.resolve_spawn_tab_domain(Some(pane_id), &domain)
.context("resolve_spawn_tab_domain")?;
let current_pane = self
.get_pane(pane_id)
@@ -810,7 +816,9 @@ impl Mux {
size: PtySize,
current_pane_id: Option<PaneId>,
) -> anyhow::Result<(Rc<Tab>, Rc<dyn Pane>, WindowId)> {
let domain = self.resolve_spawn_tab_domain(None, &domain)?;
let domain = self
.resolve_spawn_tab_domain(current_pane_id, &domain)
.context("resolve_spawn_tab_domain")?;
let window_builder;
let term_config;
@@ -836,10 +844,13 @@ impl Mux {
(*window_builder, size)
};
let cwd = self.resolve_cwd(command_dir, match current_pane_id {
Some(id) => self.get_pane(id),
None => None
});
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?;
@@ -853,6 +864,13 @@ impl Mux {
// FIXME: clipboard?
let mut window = self
.get_window_mut(window_id)
.ok_or_else(|| anyhow!("no such window!?"))?;
if let Some(idx) = window.idx_by_id(tab.tab_id()) {
window.save_and_then_set_active(idx);
}
Ok((tab, pane, window_id))
}
}
-1
View File
@@ -48,7 +48,6 @@ mux = { path = "../mux" }
open = "2.0"
ordered-float = "2.10"
palette = "0.5"
percent-encoding = "2"
portable-pty = { path = "../pty", features = ["serde_support", "ssh"]}
promise = { path = "../promise" }
pulldown-cmark = "0.9"
+36 -99
View File
@@ -1,11 +1,10 @@
use crate::termwindow::{ClipboardHelper, MuxWindowId};
use anyhow::{anyhow, bail};
use anyhow::{anyhow, bail, Context};
use config::keyassignment::{SpawnCommand, SpawnTabDomain};
use config::TermConfig;
use mux::activity::Activity;
use mux::tab::SplitDirection;
use mux::Mux;
use percent_encoding::percent_decode_str;
use portable_pty::{CommandBuilder, PtySize};
use std::sync::Arc;
@@ -74,44 +73,11 @@ impl super::TermWindow {
) -> anyhow::Result<()> {
let mux = Mux::get().unwrap();
let activity = Activity::new();
let mux_builder;
let target_window_id = if spawn_where == SpawnWhere::NewWindow {
mux_builder = mux.new_empty_window();
*mux_builder
let current_pane_id = if let Some(tab) = mux.get_active_tab_for_window(src_window_id) {
tab.get_active_pane().map(|p| p.pane_id())
} else {
src_window_id
};
let domain = match (&spawn.domain, spawn_where) {
(SpawnTabDomain::DefaultDomain, _)
// CurrentPaneDomain is the default value for the spawn domain.
// It doesn't make sense to use it when spawning a new window,
// so we treat it as DefaultDomain instead.
| (SpawnTabDomain::CurrentPaneDomain, SpawnWhere::NewWindow) => {
mux.default_domain().clone()
}
(SpawnTabDomain::CurrentPaneDomain, _) => {
let tab = match mux.get_active_tab_for_window(src_window_id) {
Some(tab) => tab,
None => bail!("window has no tabs?"),
};
let pane = tab
.get_active_pane()
.ok_or_else(|| anyhow!("current tab has no pane!?"))?;
mux.get_domain(pane.domain_id())
.ok_or_else(|| anyhow!("current tab has unresolvable domain id!?"))?
}
(SpawnTabDomain::DomainName(name), _) => {
mux.get_domain_by_name(&name).ok_or_else(|| {
anyhow!("spawn_tab called with unresolvable domain name {}", name)
})?
}
(SpawnTabDomain::DomainId(domain_id), _) => {
mux.get_domain(*domain_id).ok_or_else(|| {
anyhow!("spawn_tab called with unresolvable domain id {}", domain_id)
})?
}
None
};
let cwd = if let Some(cwd) = spawn.cwd.as_ref() {
@@ -122,47 +88,7 @@ impl super::TermWindow {
)
})?)
} else {
let cwd = match (spawn.domain, spawn_where) {
(SpawnTabDomain::DefaultDomain, _)
// CurrentPaneDomain is the default value for the spawn domain.
// It doesn't make sense to use it when spawning a new window,
// so we treat it as DefaultDomain instead.
| (SpawnTabDomain::CurrentPaneDomain, SpawnWhere::NewWindow) => mux
.get_active_tab_for_window(src_window_id)
.and_then(|tab| tab.get_active_pane())
.and_then(|pane| pane.get_current_working_dir()),
(SpawnTabDomain::CurrentPaneDomain, _) => {
let tab = match mux.get_active_tab_for_window(src_window_id) {
Some(tab) => tab,
None => bail!("window has no tabs?"),
};
let pane = tab
.get_active_pane()
.ok_or_else(|| anyhow!("current tab has no pane!?"))?;
pane.get_current_working_dir()
}
_ => None,
};
match cwd {
Some(url) if url.scheme() == "file" => {
if let Ok(path) = percent_decode_str(url.path()).decode_utf8() {
let path = path.into_owned();
// 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':' {
Some(path[1..].to_owned())
} else {
Some(path)
}
} else {
None
}
}
Some(_) | None => None,
}
None
};
let cmd_builder = if let Some(args) = spawn.args {
@@ -184,16 +110,23 @@ impl super::TermWindow {
match spawn_where {
SpawnWhere::SplitPane(direction) => {
let mux = Mux::get().unwrap();
if let Some(tab) = mux.get_active_tab_for_window(target_window_id) {
if let Some(tab) = mux.get_active_tab_for_window(src_window_id) {
let pane = tab
.get_active_pane()
.ok_or_else(|| anyhow!("tab to have a pane"))?;
log::trace!("doing split_pane");
let pane = domain
.split_pane(cmd_builder, cwd, tab.tab_id(), pane.pane_id(), direction)
.await?;
let (pane, _size) = mux
.split_pane(
// tab.tab_id(),
pane.pane_id(),
direction,
cmd_builder,
cwd,
spawn.domain,
)
.await
.context("split_pane")?;
pane.set_config(term_config);
pane.set_clipboard(&clipboard);
pane.set_download_handler(&downloader);
@@ -202,24 +135,28 @@ impl super::TermWindow {
}
}
_ => {
let tab = domain
.spawn(size, cmd_builder, cwd, target_window_id)
.await?;
let tab_id = tab.tab_id();
let pane = tab
.get_active_pane()
.ok_or_else(|| anyhow!("newly spawned tab to have a pane"))?;
pane.set_config(term_config);
let (_tab, pane, window_id) = mux
.spawn_tab_or_window(
match spawn_where {
SpawnWhere::NewWindow => None,
_ => Some(src_window_id),
},
spawn.domain,
cmd_builder,
cwd,
size,
current_pane_id,
)
.await
.context("spawn_tab_or_window")?;
if spawn_where != SpawnWhere::NewWindow {
// If it was created in this window, it copies our handlers.
// Otherwise, we'll pick them up when we later respond to
// the new window being created.
if window_id == src_window_id {
pane.set_config(term_config);
pane.set_clipboard(&clipboard);
pane.set_download_handler(&downloader);
let mut window = mux
.get_window_mut(target_window_id)
.ok_or_else(|| anyhow!("no such window!?"))?;
if let Some(idx) = window.idx_by_id(tab_id) {
window.save_and_then_set_active(idx);
}
}
}
};