mux: add AttachDomain and DetachDomain key assignments

The launcher menu code to perform attaching has been generalized
into a key assignment action and reimplemented in terms of that
action.

A detach action has been added to disconnect and detach.

refs: https://github.com/wez/wezterm/issues/1874
This commit is contained in:
Wez Furlong committed 2022-04-17 21:41:50 -07:00
1 parent b375886073
commit 8fbb1d0c0b
12 files changed
+136 -82

No files matched your search

+2
View File
@@ -362,6 +362,8 @@ pub enum KeyAssignment {
},
PopKeyTable,
ClearKeyTableStack,
DetachDomain(SpawnTabDomain),
AttachDomain(String),
}
impl_lua_conversion!(KeyAssignment);
+1
View File
@@ -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)
@@ -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"}},
},
}
```
@@ -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"}}},
},
}
```
+1 -1
View File
@@ -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 {
+1 -1
View File
@@ -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<PaneId>,
+1 -1
View File
@@ -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 {
+1
View File
@@ -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();
+2 -1
View File
@@ -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 {
+7
View File
@@ -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],
},
];
+20 -78
View File
@@ -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<String>,
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();
}
+33
View File
@@ -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(())
}