* fix: honor environment variables and cwd unconditionally for SpawnCommand The SpawnCommand specifies information about a new command to be spawned. If the args field is omitted, the default program for the target domain is spawned. However, this also causes other fields like environment variables and cwd to be wiped out. The command builder now behaves consistently for all fields, whether the args field is set or omitted. Co-authored-by: Wez Furlong <wez@wezfurlong.org>
156 lines
4.5 KiB
Rust
156 lines
4.5 KiB
Rust
use anyhow::{anyhow, bail, Context};
|
|
use config::keyassignment::SpawnCommand;
|
|
use config::TermConfig;
|
|
use mux::activity::Activity;
|
|
use mux::domain::SplitSource;
|
|
use mux::tab::SplitRequest;
|
|
use mux::window::WindowId as MuxWindowId;
|
|
use mux::Mux;
|
|
use portable_pty::CommandBuilder;
|
|
use std::sync::Arc;
|
|
use wezterm_term::TerminalSize;
|
|
|
|
#[derive(Copy, Debug, Clone, Eq, PartialEq)]
|
|
pub enum SpawnWhere {
|
|
NewWindow,
|
|
NewTab,
|
|
SplitPane(SplitRequest),
|
|
}
|
|
|
|
pub fn spawn_command_impl(
|
|
spawn: &SpawnCommand,
|
|
spawn_where: SpawnWhere,
|
|
size: TerminalSize,
|
|
src_window_id: Option<MuxWindowId>,
|
|
term_config: Arc<TermConfig>,
|
|
) {
|
|
let spawn = spawn.clone();
|
|
|
|
promise::spawn::spawn(async move {
|
|
if let Err(err) =
|
|
spawn_command_internal(spawn, spawn_where, size, src_window_id, term_config).await
|
|
{
|
|
log::error!("Failed to spawn: {:#}", err);
|
|
}
|
|
})
|
|
.detach();
|
|
}
|
|
|
|
pub async fn spawn_command_internal(
|
|
spawn: SpawnCommand,
|
|
spawn_where: SpawnWhere,
|
|
size: TerminalSize,
|
|
src_window_id: Option<MuxWindowId>,
|
|
term_config: Arc<TermConfig>,
|
|
) -> anyhow::Result<()> {
|
|
let mux = Mux::get();
|
|
let activity = Activity::new();
|
|
|
|
let current_pane_id = match src_window_id {
|
|
Some(window_id) => {
|
|
if let Some(tab) = mux.get_active_tab_for_window(window_id) {
|
|
tab.get_active_pane().map(|p| p.pane_id())
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
None => None,
|
|
};
|
|
|
|
let cwd = if let Some(cwd) = spawn.cwd.as_ref() {
|
|
Some(cwd.to_str().map(|s| s.to_owned()).ok_or_else(|| {
|
|
anyhow!(
|
|
"Domain::spawn requires that the cwd be unicode in {:?}",
|
|
cwd
|
|
)
|
|
})?)
|
|
} else {
|
|
None
|
|
};
|
|
|
|
let cmd_builder = match (
|
|
spawn.args.as_ref(),
|
|
spawn.cwd.as_ref(),
|
|
spawn.set_environment_variables.is_empty(),
|
|
) {
|
|
(None, None, true) => None,
|
|
_ => {
|
|
let mut builder = spawn
|
|
.args
|
|
.as_ref()
|
|
.map(|args| CommandBuilder::from_argv(args.iter().map(Into::into).collect()))
|
|
.unwrap_or_else(CommandBuilder::new_default_prog);
|
|
for (k, v) in spawn.set_environment_variables.iter() {
|
|
builder.env(k, v);
|
|
}
|
|
if let Some(cwd) = &spawn.cwd {
|
|
builder.cwd(cwd);
|
|
}
|
|
Some(builder)
|
|
}
|
|
};
|
|
|
|
let workspace = mux.active_workspace().clone();
|
|
|
|
match spawn_where {
|
|
SpawnWhere::SplitPane(direction) => {
|
|
let src_window_id = match src_window_id {
|
|
Some(id) => id,
|
|
None => anyhow::bail!("no src window when splitting a pane?"),
|
|
};
|
|
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, _size) = mux
|
|
.split_pane(
|
|
// tab.tab_id(),
|
|
pane.pane_id(),
|
|
direction,
|
|
SplitSource::Spawn {
|
|
command: cmd_builder,
|
|
command_dir: cwd,
|
|
},
|
|
spawn.domain,
|
|
)
|
|
.await
|
|
.context("split_pane")?;
|
|
pane.set_config(term_config);
|
|
} else {
|
|
bail!("there is no active tab while splitting pane!?");
|
|
}
|
|
}
|
|
_ => {
|
|
let (_tab, pane, window_id) = mux
|
|
.spawn_tab_or_window(
|
|
match spawn_where {
|
|
SpawnWhere::NewWindow => None,
|
|
_ => src_window_id,
|
|
},
|
|
spawn.domain,
|
|
cmd_builder,
|
|
cwd,
|
|
size,
|
|
current_pane_id,
|
|
workspace,
|
|
spawn.position,
|
|
)
|
|
.await
|
|
.context("spawn_tab_or_window")?;
|
|
|
|
// 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 Some(window_id) == src_window_id {
|
|
pane.set_config(term_config);
|
|
}
|
|
}
|
|
};
|
|
|
|
drop(activity);
|
|
|
|
Ok(())
|
|
}
|