From cd2c2a1a8353a9e0ea2243dc2aa8cf5aed80a3b1 Mon Sep 17 00:00:00 2001 From: Funami580 Date: Wed, 7 Sep 2022 04:45:15 +0200 Subject: [PATCH] charselect: add option to cycle to previous group of characters --- config/src/keyassignment.rs | 47 ++++++++++++++------- docs/config/lua/keyassignment/CharSelect.md | 19 +++++---- wezterm-gui/src/termwindow/charselect.rs | 10 +++++ 3 files changed, 51 insertions(+), 25 deletions(-) diff --git a/config/src/keyassignment.rs b/config/src/keyassignment.rs index 2bb669d98..dc7e8eb70 100644 --- a/config/src/keyassignment.rs +++ b/config/src/keyassignment.rs @@ -335,25 +335,40 @@ pub enum CharSelectGroup { UnicodeNames, } -impl CharSelectGroup { - pub fn next(self) -> Self { - match self { - Self::RecentlyUsed => Self::SmileysAndEmotion, - Self::SmileysAndEmotion => Self::PeopleAndBody, - Self::PeopleAndBody => Self::AnimalsAndNature, - Self::AnimalsAndNature => Self::FoodAndDrink, - Self::FoodAndDrink => Self::TravelAndPlaces, - Self::TravelAndPlaces => Self::Activities, - Self::Activities => Self::Objects, - Self::Objects => Self::Symbols, - Self::Symbols => Self::Flags, - Self::Flags => Self::NerdFonts, - Self::NerdFonts => Self::UnicodeNames, - Self::UnicodeNames => Self::RecentlyUsed, +// next is default, previous is the reverse +macro_rules! char_select_group_impl_next_prev { + ($($x:ident => $y:ident),+ $(,)?) => { + impl CharSelectGroup { + pub const fn next(self) -> Self { + match self { + $(CharSelectGroup::$x => CharSelectGroup::$y),+ + } + } + + pub const fn previous(self) -> Self { + match self { + $(CharSelectGroup::$y => CharSelectGroup::$x),+ + } + } } - } + }; } +char_select_group_impl_next_prev! ( + RecentlyUsed => SmileysAndEmotion, + SmileysAndEmotion => PeopleAndBody, + PeopleAndBody => AnimalsAndNature, + AnimalsAndNature => FoodAndDrink, + FoodAndDrink => TravelAndPlaces, + TravelAndPlaces => Activities, + Activities => Objects, + Objects => Symbols, + Symbols => Flags, + Flags => NerdFonts, + NerdFonts => UnicodeNames, + UnicodeNames => RecentlyUsed, +); + impl Default for CharSelectGroup { fn default() -> Self { Self::SmileysAndEmotion diff --git a/docs/config/lua/keyassignment/CharSelect.md b/docs/config/lua/keyassignment/CharSelect.md index b038baa23..85a319b68 100644 --- a/docs/config/lua/keyassignment/CharSelect.md +++ b/docs/config/lua/keyassignment/CharSelect.md @@ -23,15 +23,16 @@ Characters are categorized into the following groups: The following key assignments are available (they are not currently configurable): -|Key | Action | -|----------|--------| -|UpArrow |Move Up | -|DownArrow |Move Down| -|Enter |Accept the current item, copy it to the clipboard, insert it into the active pane, and cancel the modal| -|Esc |Cancel the modal| -|CTRL-g |Cancel the modal| -|CTRL-r |Cycle to the next group of characters| -|CTRL-u |Clear text input| +|Key | Action | +|----------------|--------| +|UpArrow |Move Up | +|DownArrow |Move Down| +|Enter |Accept the current item, copy it to the clipboard, insert it into the active pane, and cancel the modal| +|Esc |Cancel the modal| +|CTRL-g |Cancel the modal| +|CTRL-r |Cycle to the next group of characters| +|CTRL-SHIFT-r |Cycle to the previous group of characters| +|CTRL-u |Clear text input| Typing a name or a hex unicode codepoint value will fuzzy search across all possible groups (not just the current group) and filter the results. diff --git a/wezterm-gui/src/termwindow/charselect.rs b/wezterm-gui/src/termwindow/charselect.rs index 43600310b..dab6b18e7 100644 --- a/wezterm-gui/src/termwindow/charselect.rs +++ b/wezterm-gui/src/termwindow/charselect.rs @@ -19,6 +19,7 @@ use serde::{Deserialize, Serialize}; use std::borrow::Cow; use std::cell::{Ref, RefCell}; use std::path::PathBuf; +use termwiz::input::Modifiers; use wezterm_term::{KeyCode, KeyModifiers, MouseEvent}; use window::color::LinearRgba; @@ -551,6 +552,8 @@ impl Modal for CharSelector { mods: KeyModifiers, term_window: &mut TermWindow, ) -> anyhow::Result<()> { + const CTRL_AND_SHIFT: Modifiers = KeyModifiers::CTRL.union(KeyModifiers::SHIFT); + match (key, mods) { (KeyCode::Escape, KeyModifiers::NONE) | (KeyCode::Char('g'), KeyModifiers::CTRL) => { term_window.cancel_modal(); @@ -562,6 +565,13 @@ impl Modal for CharSelector { self.selection.borrow_mut().clear(); self.updated_input(); } + (KeyCode::Char('R'), KeyModifiers::CTRL) | (KeyCode::Char('r'), CTRL_AND_SHIFT) => { + // Cycle the selected group in reverse direction + let mut group = self.group.borrow_mut(); + *group = group.previous(); + self.selection.borrow_mut().clear(); + self.updated_input(); + } (KeyCode::UpArrow, KeyModifiers::NONE) => { self.move_up(); }