diff --git a/config/src/keyassignment.rs b/config/src/keyassignment.rs index 25834b42a..9d53d616a 100644 --- a/config/src/keyassignment.rs +++ b/config/src/keyassignment.rs @@ -362,6 +362,8 @@ pub enum KeyAssignment { }, PopKeyTable, ClearKeyTableStack, + DetachDomain(SpawnTabDomain), + AttachDomain(String), } impl_lua_conversion!(KeyAssignment); diff --git a/docs/changelog.md b/docs/changelog.md index 16c509882..0bfb7f874 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -16,6 +16,7 @@ As features stabilize some brief notes about them will accumulate here. * [PaneInformation.domain_name](config/lua/PaneInformation.md) and [pane:get_domain_name()](config/lua/pane/get_domain_name.md) which return the name of the domain with which a pane is associated. [#1881](https://github.com/wez/wezterm/issues/1881) * You may now use `CTRL-n` and `CTRL-p` (in addition to the up/down arrow and vi motion keys) to change the selected row in the Launcher. Thanks to [@Junnplus](https://github.com/Junnplus)! [#1880](https://github.com/wez/wezterm/pull/1880) * Attaching multiplexer domains now attaches the first window as a tab in the active window, rather than opening a new window. [#1874](https://github.com/wez/wezterm/issues/1874) +* [AttachDomain](config/lua/keyassignment/AttachDomain.md) and [DetachDomain](config/lua/keyassignment/DetachDomain.md) key assignments #### Changed * Debian packages now register wezterm as an alternative for `x-terminal-emulator`. Thanks to [@xpufx](https://github.com/xpufx)! [#1883](https://github.com/wez/wezterm/pull/1883) diff --git a/docs/config/lua/keyassignment/AttachDomain.md b/docs/config/lua/keyassignment/AttachDomain.md new file mode 100644 index 000000000..d1878ff71 --- /dev/null +++ b/docs/config/lua/keyassignment/AttachDomain.md @@ -0,0 +1,35 @@ +# AttachDomain(domain_name) + +*Since: nightly builds only* + +Attempts to attach the named multiplexing domain. The name can be any of the +names used in you `ssh_domains`, `unix_domains` or `tls_clients` +configurations. + +Attaching a domain will attempt to import the windows, tabs and panes from the +remote system into those of the local GUI. + +If there are no remote panes in that domain, wezterm will spawn a default +program into it. + +This action is not bound to any keys by default. The [Launcher Menu]() +(default: right click on the new tab `+` button in the tab bar) will synthesize +entries with this action. + +The example below shows how to bind a key to trigger attaching to an ssh domain: + +```lua +local wezterm = require 'wezterm' + +return { + ssh_domains = { + { + name = "devhost", + remote_address = "devhost.example.com", + } + }, + keys = { + {key="U", mods="CTRL|SHIFT", action=wezterm.action{AttachDomain="devhost"}}, + }, +} +``` diff --git a/docs/config/lua/keyassignment/DetachDomain.md b/docs/config/lua/keyassignment/DetachDomain.md new file mode 100644 index 000000000..2b28e6250 --- /dev/null +++ b/docs/config/lua/keyassignment/DetachDomain.md @@ -0,0 +1,32 @@ +# DetachDomain(domain) + +*Since: nightly builds only* + +Attempts to detach the specified domain. Detaching a domain causes +it to disconnect and remove its set of windows, tabs and panes from +the local GUI. Detaching does not cause those panes to close; if or +when you later attach to the domain, they'll still be there. + +Not every domain supports detaching, and will log an error to the +error log/debug overlay. + +```lua +local wezterm = require 'wezterm' + +return { + ssh_domains = { + { + name = "devhost", + remote_address = "devhost.example.com", + } + }, + keys = { + {key="U", mods="CTRL|SHIFT", action=wezterm.action{AttachDomain="devhost"}}, + -- Detaches the domain associated with the current pane + {key="D", mods="CTRL|SHIFT", action=wezterm.action{DetachDomain="CurrentPaneDomain"}}, + -- Detaches the "devhost" domain + {key="E", mods="CTRL|SHIFT", action=wezterm.action{DetachDomain={DomainName="devhost"}}}, + }, +} + +``` diff --git a/mux/src/domain.rs b/mux/src/domain.rs index b365e7382..b0d7d1930 100644 --- a/mux/src/domain.rs +++ b/mux/src/domain.rs @@ -327,7 +327,7 @@ impl Domain for LocalDomain { } fn detach(&self) -> anyhow::Result<()> { - bail!("detach not implemented"); + bail!("detach not implemented for LocalDomain"); } fn state(&self) -> DomainState { diff --git a/mux/src/lib.rs b/mux/src/lib.rs index 909e16961..40f3dabbd 100644 --- a/mux/src/lib.rs +++ b/mux/src/lib.rs @@ -879,7 +879,7 @@ impl Mux { *self.banner.borrow_mut() = banner; } - fn resolve_spawn_tab_domain( + pub fn resolve_spawn_tab_domain( &self, // TODO: disambiguate with TabId pane_id: Option, diff --git a/mux/src/ssh.rs b/mux/src/ssh.rs index 989e8e09c..4320bb1e1 100644 --- a/mux/src/ssh.rs +++ b/mux/src/ssh.rs @@ -683,7 +683,7 @@ impl Domain for RemoteSshDomain { } fn detach(&self) -> anyhow::Result<()> { - bail!("detach not implemented"); + bail!("detach not implemented for RemoteSshDomain"); } fn state(&self) -> DomainState { diff --git a/mux/src/termwiztermtab.rs b/mux/src/termwiztermtab.rs index ee9bba2d9..9cfb57397 100644 --- a/mux/src/termwiztermtab.rs +++ b/mux/src/termwiztermtab.rs @@ -531,6 +531,7 @@ pub async fn run< mux.kill_window(window_id); } else if let Some(pane) = mux.get_pane(pane_id) { pane.kill(); + mux.remove_pane(pane.pane_id()); } }) .detach(); diff --git a/wezterm-client/src/domain.rs b/wezterm-client/src/domain.rs index a2ab25dc2..0dc08f7b2 100644 --- a/wezterm-client/src/domain.rs +++ b/wezterm-client/src/domain.rs @@ -651,7 +651,8 @@ impl Domain for ClientDomain { } fn detach(&self) -> anyhow::Result<()> { - bail!("detach not implemented"); + self.perform_detach(); + Ok(()) } fn state(&self) -> DomainState { diff --git a/wezterm-gui/src/commands.rs b/wezterm-gui/src/commands.rs index a70c852bd..f0e3d22cc 100644 --- a/wezterm-gui/src/commands.rs +++ b/wezterm-gui/src/commands.rs @@ -719,4 +719,11 @@ static DEFS: &[CommandDef] = &[ keys: &[], args: &[ArgType::ActiveWindow], }, + CommandDef { + brief: "Detach the domain of the active pane", + doc: "Detaches (disconnects from) the domain of the active pane", + exp: |exp| exp.push(DetachDomain(SpawnTabDomain::CurrentPaneDomain)), + keys: &[], + args: &[ArgType::ActivePane], + }, ]; diff --git a/wezterm-gui/src/overlay/launcher.rs b/wezterm-gui/src/overlay/launcher.rs index 86fe3ad93..c13635df3 100644 --- a/wezterm-gui/src/overlay/launcher.rs +++ b/wezterm-gui/src/overlay/launcher.rs @@ -7,7 +7,6 @@ //! menus. use crate::inputmap::InputMap; use crate::termwindow::TermWindowNotif; -use anyhow::anyhow; use config::configuration; use config::keyassignment::{KeyAssignment, SpawnCommand, SpawnTabDomain}; use config::lua::truncate_right; @@ -29,16 +28,10 @@ use window::WindowOps; pub use config::keyassignment::LauncherFlags; -#[derive(Clone)] -enum EntryKind { - Attach { domain: DomainId }, - KeyAssignment(KeyAssignment), -} - #[derive(Clone)] struct Entry { pub label: String, - pub kind: EntryKind, + pub action: KeyAssignment, } pub struct LauncherTabEntry { @@ -65,7 +58,6 @@ pub struct LauncherArgs { title: String, active_workspace: String, workspaces: Vec, - mux_window_id: WindowId, } impl LauncherArgs { @@ -159,7 +151,6 @@ impl LauncherArgs { title: title.to_string(), workspaces, active_workspace, - mux_window_id, } } } @@ -177,7 +168,6 @@ struct LauncherState { window: ::window::Window, filtering: bool, flags: LauncherFlags, - mux_window_id: WindowId, } impl LauncherState { @@ -231,9 +221,7 @@ impl LauncherState { None => "(default shell)".to_string(), }, }, - kind: EntryKind::KeyAssignment(KeyAssignment::SpawnCommandInNewTab( - item.clone(), - )), + action: KeyAssignment::SpawnCommandInNewTab(item.clone()), }); } } @@ -242,19 +230,15 @@ impl LauncherState { let entry = if domain.state == DomainState::Attached { Entry { label: format!("New Tab ({})", domain.label), - kind: EntryKind::KeyAssignment(KeyAssignment::SpawnCommandInNewTab( - SpawnCommand { - domain: SpawnTabDomain::DomainName(domain.name.to_string()), - ..SpawnCommand::default() - }, - )), + action: KeyAssignment::SpawnCommandInNewTab(SpawnCommand { + domain: SpawnTabDomain::DomainName(domain.name.to_string()), + ..SpawnCommand::default() + }), } } else { Entry { label: format!("Attach {}", domain.label), - kind: EntryKind::Attach { - domain: domain.domain_id, - }, + action: KeyAssignment::AttachDomain(domain.name.to_string()), } }; @@ -272,10 +256,10 @@ impl LauncherState { if *ws != args.active_workspace { self.entries.push(Entry { label: format!("Switch to workspace: `{}`", ws), - kind: EntryKind::KeyAssignment(KeyAssignment::SwitchToWorkspace { + action: KeyAssignment::SwitchToWorkspace { name: Some(ws.clone()), spawn: None, - }), + }, }); } } @@ -284,17 +268,17 @@ impl LauncherState { "Create new Workspace (current is `{}`)", args.active_workspace ), - kind: EntryKind::KeyAssignment(KeyAssignment::SwitchToWorkspace { + action: KeyAssignment::SwitchToWorkspace { name: None, spawn: None, - }), + }, }); } for tab in &args.tabs { self.entries.push(Entry { label: format!("{}. {} panes", tab.title, tab.pane_count), - kind: EntryKind::KeyAssignment(KeyAssignment::ActivateTab(tab.tab_idx as isize)), + action: KeyAssignment::ActivateTab(tab.tab_idx as isize), }); } @@ -310,7 +294,7 @@ impl LauncherState { } self.entries.push(Entry { label: format!("{}. {}", cmd.brief, cmd.doc), - kind: EntryKind::KeyAssignment(cmd.action), + action: cmd.action, }); } } @@ -331,10 +315,7 @@ impl LauncherState { } if key_entries .iter() - .find(|ent| match &ent.kind { - EntryKind::KeyAssignment(a) => a == &entry.action, - _ => false, - }) + .find(|ent| ent.action == entry.action) .is_some() { // Avoid duplicate entries @@ -347,7 +328,7 @@ impl LauncherState { mods.to_string(), keycode.to_string().escape_debug() ), - kind: EntryKind::KeyAssignment(entry.action), + action: entry.action, }); } key_entries.sort_by(|a, b| a.label.cmp(&b.label)); @@ -422,24 +403,11 @@ impl LauncherState { } fn launch(&self, active_idx: usize) { - match self.filtered_entries[active_idx].clone().kind { - EntryKind::Attach { domain } => { - let window_id = self.mux_window_id; - promise::spawn::spawn_into_main_thread(async move { - // We can't inline do_domain_attach here directly - // because the compiler would then want its body - // to be Send :-/ - do_domain_attach(domain, window_id); - }) - .detach(); - } - EntryKind::KeyAssignment(assignment) => { - self.window.notify(TermWindowNotif::PerformAssignment { - pane_id: self.pane_id, - assignment, - }); - } - } + let assignment = self.filtered_entries[active_idx].action.clone(); + self.window.notify(TermWindowNotif::PerformAssignment { + pane_id: self.pane_id, + assignment, + }); } fn move_up(&mut self) { @@ -603,7 +571,6 @@ pub fn launcher( window, filtering: args.flags.contains(LauncherFlags::FUZZY), flags: args.flags, - mux_window_id: args.mux_window_id, }; term.set_raw_mode()?; @@ -613,28 +580,3 @@ pub fn launcher( state.render(&mut term)?; state.run_loop(&mut term) } - -fn do_domain_attach(domain: DomainId, window: WindowId) { - promise::spawn::spawn(async move { - let mux = Mux::get().unwrap(); - let domain = mux - .get_domain(domain) - .ok_or_else(|| anyhow!("launcher attach called with unresolvable domain id!?"))?; - domain.attach(Some(window)).await?; - - let have_panes_in_domain = mux - .iter_panes() - .iter() - .any(|p| p.domain_id() == domain.domain_id()); - - if !have_panes_in_domain { - let config = config::configuration(); - let _tab = domain - .spawn(config.initial_size(), None, None, window) - .await?; - } - - Result::<(), anyhow::Error>::Ok(()) - }) - .detach(); -} diff --git a/wezterm-gui/src/termwindow/mod.rs b/wezterm-gui/src/termwindow/mod.rs index 98b616581..60bead4f5 100644 --- a/wezterm-gui/src/termwindow/mod.rs +++ b/wezterm-gui/src/termwindow/mod.rs @@ -2346,6 +2346,39 @@ impl TermWindow { switcher.do_switch(); } } + DetachDomain(domain) => { + let domain = Mux::get() + .expect("running on GUI thread") + .resolve_spawn_tab_domain(Some(pane.pane_id()), domain)?; + domain.detach()?; + } + AttachDomain(domain) => { + let window = self.mux_window_id; + let domain = domain.to_string(); + + promise::spawn::spawn(async move { + let mux = Mux::get().unwrap(); + let domain = mux + .get_domain_by_name(&domain) + .ok_or_else(|| anyhow!("{} is not a valid domain name", domain))?; + domain.attach(Some(window)).await?; + + let have_panes_in_domain = mux + .iter_panes() + .iter() + .any(|p| p.domain_id() == domain.domain_id()); + + if !have_panes_in_domain { + let config = config::configuration(); + let _tab = domain + .spawn(config.initial_size(), None, None, window) + .await?; + } + + Result::<(), anyhow::Error>::Ok(()) + }) + .detach(); + } }; Ok(()) }