From 5031a53ff989a085f9f71a00a5545912cb30d767 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 26 Sep 2020 10:30:17 -0700 Subject: [PATCH] wezterm: add CloseActivePane key assignment This removes the active pane from the current tab, causing the tab to close if it was the last remaining pane in the tab. ```lua {key="W", mods="CTRL", action="CloseActivePane"}, ``` refs: https://github.com/wez/wezterm/issues/157 --- src/frontend/gui/termwindow.rs | 10 ++++++++++ src/keyassignment.rs | 1 + src/mux/tab.rs | 22 +++++++++++++++++++--- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/frontend/gui/termwindow.rs b/src/frontend/gui/termwindow.rs index 783cd7090..1c18eb932 100644 --- a/src/frontend/gui/termwindow.rs +++ b/src/frontend/gui/termwindow.rs @@ -1855,6 +1855,7 @@ impl TermWindow { } } CloseCurrentTab => self.close_current_tab(), + CloseActivePane => self.close_active_pane(), Nop | DisableDefaultAssignment => {} ReloadConfiguration => crate::config::reload(), MoveTab(n) => self.move_tab(*n)?, @@ -2125,6 +2126,15 @@ impl TermWindow { self.scaling_changed(self.dimensions, 1.); } + fn close_active_pane(&mut self) { + let mux = Mux::get().unwrap(); + let tab = match mux.get_active_tab_for_window(self.mux_window_id) { + Some(tab) => tab, + None => return, + }; + tab.kill_active_pane(); + } + fn close_current_tab(&mut self) { let mux = Mux::get().unwrap(); let tab = match mux.get_active_tab_for_window(self.mux_window_id) { diff --git a/src/keyassignment.rs b/src/keyassignment.rs index 3c277550a..2b9e3e3b2 100644 --- a/src/keyassignment.rs +++ b/src/keyassignment.rs @@ -123,6 +123,7 @@ pub enum KeyAssignment { AdjustPaneSize(PaneDirection, usize), ActivatePaneDirection(PaneDirection), TogglePaneZoomState, + CloseActivePane, } impl_lua_conversion!(KeyAssignment); diff --git a/src/mux/tab.rs b/src/mux/tab.rs index 1a25e379f..79b80381c 100644 --- a/src/mux/tab.rs +++ b/src/mux/tab.rs @@ -711,6 +711,18 @@ impl Tab { } pub fn prune_dead_panes(&self) -> bool { + self.remove_pane_if(|_, pane| pane.is_dead()) + } + + pub fn kill_active_pane(&self) -> bool { + let active_idx = *self.active.borrow(); + self.remove_pane_if(|idx, _| idx == active_idx) + } + + fn remove_pane_if(&self, f: F) -> bool + where + F: Fn(usize, &Rc) -> bool, + { let mut dead_panes = vec![]; { @@ -723,7 +735,7 @@ impl Tab { loop { // Figure out the available size by looking at our immediate parent node. - // If we are the root, look at the provided new size + // If we are the root, look at the tab size let pane_size = if let Some((branch, Some(parent))) = cursor.path_to_root().next() { if branch == PathBranch::IsRight { parent.second @@ -736,7 +748,7 @@ impl Tab { if cursor.is_leaf() { let pane = Rc::clone(cursor.leaf_mut().unwrap()); - if pane.is_dead() { + if f(pane_index, &pane) { if pane_index == active_idx { active_idx = pane_index.saturating_sub(1); } @@ -749,7 +761,11 @@ impl Tab { } Err(c) => { // We might be the root, for example - root.replace(c.tree()); + if c.is_top() && c.is_leaf() { + root.replace(Tree::Empty); + } else { + root.replace(c.tree()); + } break; } };