1
1
mirror of https://github.com/wez/wezterm.git synced 2024-11-26 08:25:50 +03:00

Add CloseCurrentTab key binding

Added CMD-W as a default binding for this action.

refs: https://github.com/wez/wezterm/pull/26
This commit is contained in:
Wez Furlong 2019-06-08 10:25:59 -07:00
parent 25da2ebbbf
commit 000c5eddac
5 changed files with 26 additions and 12 deletions

View File

@ -144,6 +144,7 @@ The default key bindings are:
| `SUPER` | `0` | `ResetFontSize` |
| `CTRL` | `0` | `ResetFontSize` |
| `SUPER` | `t` | `SpawnTab` |
| `SUPER` | `w` | `CloseCurrentTab` |
| `SUPER` | `1` | `ActivateTab(0)` |
| `SUPER` | `2` | `ActivateTab(1)` |
| `SUPER` | `3` | `ActivateTab(2)` |
@ -217,6 +218,7 @@ specified via the `arg` key; see examples below.
| `Nop` | Does nothing. This is useful to disable a default key assignment. |
| `Hide` | Hides the current window |
| `Show` | Shows the current window |
| `CloseCurrentTab` | Equivalent to clicking the `x` on the window title bar to close it: Closes the current tab. If that was the last tab, closes that window. If that was the last window, wezterm terminates. |
Example:

View File

@ -110,6 +110,7 @@ impl std::convert::TryInto<KeyAssignment> for &Key {
KeyAction::DecreaseFontSize => KeyAssignment::DecreaseFontSize,
KeyAction::ResetFontSize => KeyAssignment::ResetFontSize,
KeyAction::Nop => KeyAssignment::Nop,
KeyAction::CloseCurrentTab => KeyAssignment::CloseCurrentTab,
KeyAction::ActivateTab => KeyAssignment::ActivateTab(
self.arg
.as_ref()
@ -148,6 +149,7 @@ pub enum KeyAction {
Nop,
Hide,
Show,
CloseCurrentTab,
}
fn de_keycode<'de, D>(deserializer: D) -> Result<KeyCode, D::Error>

View File

@ -747,17 +747,7 @@ impl GliumTerminalWindow {
event: WindowEvent::CloseRequested,
..
} => {
let mux = Mux::get().unwrap();
let tab = match mux.get_active_tab_for_window(self.get_mux_window_id()) {
Some(tab) => tab,
None => return Ok(()),
};
mux.remove_tab(tab.tab_id());
if let Some(mut win) = mux.get_window_mut(self.get_mux_window_id()) {
win.remove_by_id(tab.tab_id());
}
// Explicit drop of mux to satisfy the borrow checker
drop(mux);
self.host.close_current_tab();
}
_ => {}
}

View File

@ -31,6 +31,7 @@ pub enum KeyAssignment {
Nop,
Hide,
Show,
CloseCurrentTab,
}
pub trait HostHelper {
@ -129,6 +130,7 @@ fn key_bindings() -> KeyMap {
[KeyModifiers::CTRL, KeyCode::Char('0'), ResetFontSize],
// Tab navigation and management
[KeyModifiers::SUPER, KeyCode::Char('t'), SpawnTab],
[KeyModifiers::SUPER, KeyCode::Char('w'), CloseCurrentTab],
[KeyModifiers::SUPER, KeyCode::Char('1'), ActivateTab(0)],
[KeyModifiers::SUPER, KeyCode::Char('2'), ActivateTab(1)],
[KeyModifiers::SUPER, KeyCode::Char('3'), ActivateTab(2)],
@ -241,6 +243,7 @@ impl<H: HostHelper> HostImpl<H> {
SendString(s) => tab.writer().write_all(s.as_bytes())?,
Hide => self.hide_window(),
Show => self.show_window(),
CloseCurrentTab => self.close_current_tab(),
Nop => {}
}
Ok(())
@ -291,6 +294,21 @@ impl<H: HostHelper> HostImpl<H> {
})
}
pub fn close_current_tab(&mut self) {
self.with_window(move |win| {
let mux = Mux::get().unwrap();
let tab = match mux.get_active_tab_for_window(win.get_mux_window_id()) {
Some(tab) => tab,
None => return Ok(()),
};
mux.remove_tab(tab.tab_id());
if let Some(mut win) = mux.get_window_mut(win.get_mux_window_id()) {
win.remove_by_id(tab.tab_id());
}
win.activate_tab_relative(0)
});
}
pub fn hide_window(&mut self) {
self.with_window(move |win| {
win.hide_window();

View File

@ -5,7 +5,7 @@ use crate::mux::window::WindowId;
use crate::mux::Mux;
use crate::opengl::render::Renderer;
use crate::opengl::textureatlas::OutOfTextureSpace;
use failure::{format_err, Error};
use failure::{ensure, format_err, Error};
use glium;
use portable_pty::PtySize;
use std::rc::Rc;
@ -71,6 +71,8 @@ pub trait TerminalWindow {
.ok_or_else(|| format_err!("no such window"))?;
let max = window.len();
ensure!(max > 0, "no more tabs");
let active = window.get_active_idx() as isize;
let tab = active + delta;
let tab = if tab < 0 { max as isize + tab } else { tab };