From db8f588c5ee5b906d96d583a2140f65cf501f376 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Mon, 17 Jan 2022 09:54:11 -0700 Subject: [PATCH] Add SwitchWorkspaceRelative key assignment --- config/src/keyassignment.rs | 1 + docs/changelog.md | 2 +- .../keyassignment/SwitchWorkspaceRelative.md | 31 +++++++++++++++++++ wezterm-gui/src/termwindow/mod.rs | 16 ++++++++++ 4 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 docs/config/lua/keyassignment/SwitchWorkspaceRelative.md diff --git a/config/src/keyassignment.rs b/config/src/keyassignment.rs index 314b2dad8..b42f20549 100644 --- a/config/src/keyassignment.rs +++ b/config/src/keyassignment.rs @@ -346,6 +346,7 @@ pub enum KeyAssignment { name: Option, spawn: Option, }, + SwitchWorkspaceRelative(isize), } impl_lua_conversion!(KeyAssignment); diff --git a/docs/changelog.md b/docs/changelog.md index 6df1e5670..f6b1dcd5b 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -22,7 +22,7 @@ As features stabilize some brief notes about them will accumulate here. * `Symbols Nerd Font Mono` is now bundled with WezTerm and is included as a default fallback font. This means that you may use any of the glyphs available in the [Nerd Fonts](https://github.com/ryanoasis/nerd-fonts) collection with any font without patching fonts and without explicitly adding that font to your fallback list. Pomicons have an unclear license for distribution and are excluded from this bundled font, however, you may manually install the font with those icons from the Nerd Font site itself and it will take precedence over the bundled font. This font replaces the older `PowerlineExtraSymbols` font. [#1521](https://github.com/wez/wezterm/issues/1521). * [wezterm.nerdfonts](config/lua/wezterm/nerdfonts.md) as a convenient way to resolve Nerd Fonts glyphs by name in your config file * [ShowLauncherArgs](config/lua/keyassignment/ShowLauncherArgs.md) key assignment to show the launcher scoped to certain items, or to launch it directly in fuzzy matching mode -* Workspaces. Follow work in progress on [#1531](https://github.com/wez/wezterm/issues/1531) and [#1322](https://github.com/wez/wezterm/discussions/1322)! [window:active_workspace()](config/lua/window/active_workspace.md) [default_workspace](config/lua/config/default_workspace.md) +* Workspaces. Follow work in progress on [#1531](https://github.com/wez/wezterm/issues/1531) and [#1322](https://github.com/wez/wezterm/discussions/1322)! [window:active_workspace()](config/lua/window/active_workspace.md) [default_workspace](config/lua/config/default_workspace.md) [SwitchWorkspaceRelative](config/lua/keyassignment/SwitchWorkspaceRelative.md) #### Changed diff --git a/docs/config/lua/keyassignment/SwitchWorkspaceRelative.md b/docs/config/lua/keyassignment/SwitchWorkspaceRelative.md new file mode 100644 index 000000000..75403c974 --- /dev/null +++ b/docs/config/lua/keyassignment/SwitchWorkspaceRelative.md @@ -0,0 +1,31 @@ +# SwitchWorkspaceRelative + +*Since: nightly builds only* + +Switch to the workspace relative to the current workspace. Workspaces are ordered +lexicographically based on their names. + +The argument value specifies an offset. eg: `-1` switches to the workspace +immediately prior to the current workspace, while `1` switches to the workspace +immediately following the current workspace. + +This example binds CTRL-N and CTRL-P to move forwards, backwards through workspaces. +It shows the active workspace in the title bar. The launcher menu can be used +to create workspaces. + +```lua +local wezterm = require 'wezterm' + +wezterm.on("update-right-status", function(window, pane) + window:set_right_status(window:active_workspace()) +end) + +return { + keys = { + {key="9", mods="ALT", action=wezterm.action{ShowLauncherArgs={flags="FUZZY|WORKSPACES"}}}, + {key="n", mods="CTRL", action=wezterm.action{SwitchWorkspaceRelative=1}}, + {key="p", mods="CTRL", action=wezterm.action{SwitchWorkspaceRelative=-1}}, + }, +} +``` + diff --git a/wezterm-gui/src/termwindow/mod.rs b/wezterm-gui/src/termwindow/mod.rs index d448c59a0..855a92ce8 100644 --- a/wezterm-gui/src/termwindow/mod.rs +++ b/wezterm-gui/src/termwindow/mod.rs @@ -2100,6 +2100,22 @@ impl TermWindow { }; tab.toggle_zoom(); } + SwitchWorkspaceRelative(delta) => { + let mux = Mux::get().unwrap(); + let workspace = mux.active_workspace(); + let workspaces = mux.iter_workspaces(); + let idx = workspaces.iter().position(|w| *w == workspace).unwrap_or(0); + let new_idx = idx as isize + delta; + let new_idx = if new_idx < 0 { + workspaces.len() as isize + new_idx + } else { + new_idx + }; + let new_idx = new_idx as usize % workspaces.len(); + if let Some(w) = workspaces.get(new_idx) { + front_end().switch_workspace(w); + } + } SwitchToWorkspace { name, spawn } => { let activity = crate::Activity::new(); let mux = Mux::get().unwrap();