From 46f74a5bf767ffd199e6e6605fb701f368b92675 Mon Sep 17 00:00:00 2001 From: mgpinf Date: Sat, 8 Mar 2025 22:52:11 +0530 Subject: [PATCH] Add alphabet to launcher args (#6677) * feat: add alphabet argument to ShowLauncherArgs - The labels are now based on the provided argument or the default value - The 'alphabet' string defaults to "1234567890abcdefghilmnopqrstuvwxyz" - Optional argument 'alphabet' is added to 'ShowLauncherArgs' * docs: update documentation for ShowLauncherArgs * fix: push (max_label_len+3) whitespaces if always_fuzzy false in InputSelector This is to handle the edge case in InputSelector when max_label_len is 2 and the labels shift one place to the left when entering fuzzy mode and back one place to the right when exiting fuzzy mode. * fix: push (max_label_len+3) whitespaces if 'FUZZY' not present This is to handle the edge case in Launcher when max_label_len is 2 and the labels shift one place to the left when entering fuzzy mode and back one place to the right when exiting fuzzy mode. * feat: add configuration option for Launcher alphabet - This is in order to be able to configure the alphabet for actions corresponding to KeyAssignment enums 'ShowLauncher', 'ShowTabNavigator' - If `launcher_alphabet` configuration option not set, the value defaults to '1234567890abcdefghilmnopqrstuvwxyz' * docs: add launcher_alphabet documentation * docs: update documentation for ShowLauncherArgs Co-authored-by: Wez Furlong --- config/src/config.rs | 8 ++ config/src/keyassignment.rs | 1 + docs/config/lua/config/launcher_alphabet.md | 13 +++ .../lua/keyassignment/ShowLauncherArgs.md | 4 + wezterm-gui/src/overlay/launcher.rs | 80 +++++++++++++------ wezterm-gui/src/overlay/selector.rs | 4 +- wezterm-gui/src/termwindow/mod.rs | 7 ++ 7 files changed, 93 insertions(+), 24 deletions(-) create mode 100644 docs/config/lua/config/launcher_alphabet.md diff --git a/config/src/config.rs b/config/src/config.rs index 21522f4a3..2270ceb6c 100644 --- a/config/src/config.rs +++ b/config/src/config.rs @@ -414,6 +414,9 @@ pub struct Config { pub disable_default_key_bindings: bool, pub leader: Option, + #[dynamic(default = "default_num_alphabet")] + pub launcher_alphabet: String, + #[dynamic(default)] pub disable_default_quick_select_patterns: bool, #[dynamic(default)] @@ -1809,6 +1812,11 @@ fn default_alternate_buffer_wheel_scroll_speed() -> u8 { 3 } +fn default_num_alphabet() -> String { + // Note: vi motion keys are intentionally excluded from this alphabet + "1234567890abcdefghilmnopqrstuvwxyz".to_string() +} + fn default_alphabet() -> String { "asdfqwerzxcvjklmiuopghtybn".to_string() } diff --git a/config/src/keyassignment.rs b/config/src/keyassignment.rs index 860c275ef..efd36fd89 100644 --- a/config/src/keyassignment.rs +++ b/config/src/keyassignment.rs @@ -19,6 +19,7 @@ pub struct LauncherActionArgs { pub title: Option, pub help_text: Option, pub fuzzy_help_text: Option, + pub alphabet: Option, } bitflags::bitflags! { diff --git a/docs/config/lua/config/launcher_alphabet.md b/docs/config/lua/config/launcher_alphabet.md new file mode 100644 index 000000000..3c4a08aeb --- /dev/null +++ b/docs/config/lua/config/launcher_alphabet.md @@ -0,0 +1,13 @@ +--- +tags: + - launcher +--- +# `launcher_alphabet` + +{{since('nightly')}} + +Specify a string of unique characters. The characters in the string are used +to calculate one or two key press shortcuts that can be used to quickly choose from +the Launcher when in the default mode. Defaults to: +`"1234567890abcdefghilmnopqrstuvwxyz"`. (Without j/k so they can be used for movement +up and down.) diff --git a/docs/config/lua/keyassignment/ShowLauncherArgs.md b/docs/config/lua/keyassignment/ShowLauncherArgs.md index 64f48fabd..f80fdb8a0 100644 --- a/docs/config/lua/keyassignment/ShowLauncherArgs.md +++ b/docs/config/lua/keyassignment/ShowLauncherArgs.md @@ -13,6 +13,10 @@ The arguments are a lua table with the following keys: `"Select an item and press Enter=launch Esc=cancel /=filter"` {{since('nightly', inline=True)}} * `fuzzy_help_text` - a string to display when in fuzzy finding mode. Defaults to: `"Fuzzy matching: "` {{since('nightly', inline=True)}} +* `alphabet` - a string of unique characters. The characters in the string are used + to calculate one or two key press shortcuts that can be used to quickly choose from + the Launcher when in the default mode. Defaults to the same value as + [launcher_alphabet](../config/launcher_alphabet.md) {{since('nightly', inline=True)}} The possible flags are listed below. You must explicitly list each item that you want to include in the launcher. If you only specify `"FUZZY"` then you will see diff --git a/wezterm-gui/src/overlay/launcher.rs b/wezterm-gui/src/overlay/launcher.rs index f4a2031c4..25d01aaba 100644 --- a/wezterm-gui/src/overlay/launcher.rs +++ b/wezterm-gui/src/overlay/launcher.rs @@ -7,6 +7,7 @@ //! menus. use crate::commands::derive_command_from_key_assignment; use crate::inputmap::InputMap; +use crate::overlay::quickselect; use crate::overlay::selector::{matcher_pattern, matcher_score}; use crate::termwindow::TermWindowNotif; use config::configuration; @@ -59,6 +60,7 @@ pub struct LauncherArgs { workspaces: Vec, help_text: String, fuzzy_help_text: String, + alphabet: String, } impl LauncherArgs { @@ -71,6 +73,7 @@ impl LauncherArgs { domain_id_of_current_tab: DomainId, help_text: &str, fuzzy_help_text: &str, + alphabet: &str, ) -> Self { let mux = Mux::get(); @@ -161,6 +164,7 @@ impl LauncherArgs { active_workspace, help_text: help_text.to_string(), fuzzy_help_text: fuzzy_help_text.to_string(), + alphabet: alphabet.to_string(), } } } @@ -177,9 +181,12 @@ struct LauncherState { pane_id: PaneId, window: ::window::Window, filtering: bool, - flags: LauncherFlags, help_text: String, fuzzy_help_text: String, + labels: Vec, + alphabet: String, + selection: String, + always_fuzzy: bool, } impl LauncherState { @@ -360,6 +367,14 @@ impl LauncherState { fn render(&mut self, term: &mut TermWizTerminal) -> termwiz::Result<()> { let size = term.get_screen_size()?; let max_width = size.cols.saturating_sub(6); + let max_items = size.rows.saturating_sub(ROW_OVERHEAD); + if max_items != self.max_items { + self.labels = quickselect::compute_labels_for_alphabet_with_preserved_case( + &self.alphabet, + self.filtered_entries.len().min(max_items + 1), + ); + self.max_items = max_items; + } let mut changes = vec![ Change::ClearScreen(ColorAttribute::Default), @@ -374,7 +389,9 @@ impl LauncherState { Change::AllAttributes(CellAttributes::default()), ]; - let max_items = self.max_items; + let labels = &self.labels; + let max_label_len = labels.iter().map(|s| s.len()).max().unwrap_or(0); + let mut labels_iter = labels.into_iter(); for (row_num, (entry_idx, entry)) in self .filtered_entries @@ -394,8 +411,17 @@ impl LauncherState { attr.set_reverse(true); } - if row_num < 9 && !self.filtering { - changes.push(Change::Text(format!(" {}. ", row_num + 1))); + // from above we know that row_num <= max_items + // show labels as long as we have more labels left + // and we are not filtering + if !self.filtering { + if let Some(label) = labels_iter.next() { + changes.push(Change::Text(format!(" {label:>max_label_len$}. "))); + } else { + changes.push(Change::Text(" ".repeat(max_label_len + 3))); + } + } else if !self.always_fuzzy { + changes.push(Change::Text(" ".repeat(max_label_len + 3))); } else { changes.push(Change::Text(" ".to_string())); } @@ -464,22 +490,29 @@ impl LauncherState { match event { InputEvent::Key(KeyEvent { key: KeyCode::Char(c), - .. - }) if !self.filtering && c >= '1' && c <= '9' => { - if self.launch(self.top_row + (c as u32 - '1' as u32) as usize) { - break; + modifiers: Modifiers::NONE, + }) if !self.filtering && self.alphabet.contains(c) => { + self.selection.push(c); + if let Some(pos) = self.labels.iter().position(|x| *x == self.selection) { + // since the number of labels is always <= self.max_items + // by construction, we have pos as usize <= self.max_items + // for free + self.active_idx = self.top_row + pos as usize; + if self.launch(self.active_idx) { + break; + } } } InputEvent::Key(KeyEvent { key: KeyCode::Char('j'), .. - }) if !self.filtering => { + }) if !self.filtering && !self.alphabet.contains("j") => { self.move_down(); } InputEvent::Key(KeyEvent { key: KeyCode::Char('k'), .. - }) if !self.filtering => { + }) if !self.filtering && !self.alphabet.contains("k") => { self.move_up(); } InputEvent::Key(KeyEvent { @@ -504,12 +537,14 @@ impl LauncherState { key: KeyCode::Backspace, .. }) => { - if self.filter_term.pop().is_none() - && !self.flags.contains(LauncherFlags::FUZZY) - { - self.filtering = false; + if !self.filtering { + self.selection.pop(); + } else { + if self.filter_term.pop().is_none() && !self.always_fuzzy { + self.filtering = false; + } + self.update_filter(); } - self.update_filter(); } InputEvent::Key(KeyEvent { key: KeyCode::Char('G') | KeyCode::Char('['), @@ -583,9 +618,6 @@ impl LauncherState { break; } } - InputEvent::Resized { rows, .. } => { - self.max_items = rows.saturating_sub(ROW_OVERHEAD); - } _ => {} } self.render(term)?; @@ -601,21 +633,23 @@ pub fn launcher( window: ::window::Window, initial_choice_idx: usize, ) -> anyhow::Result<()> { - let size = term.get_screen_size()?; - let max_items = size.rows.saturating_sub(ROW_OVERHEAD); + let filtering = args.flags.contains(LauncherFlags::FUZZY); let mut state = LauncherState { active_idx: initial_choice_idx, - max_items, + max_items: 0, pane_id: args.pane_id, top_row: 0, entries: vec![], filter_term: String::new(), filtered_entries: vec![], window, - filtering: args.flags.contains(LauncherFlags::FUZZY), - flags: args.flags, + filtering, help_text: args.help_text.clone(), fuzzy_help_text: args.fuzzy_help_text.clone(), + labels: vec![], + selection: String::new(), + alphabet: args.alphabet.clone(), + always_fuzzy: filtering, }; term.set_raw_mode()?; diff --git a/wezterm-gui/src/overlay/selector.rs b/wezterm-gui/src/overlay/selector.rs index 1e85fa103..f426f9cfe 100644 --- a/wezterm-gui/src/overlay/selector.rs +++ b/wezterm-gui/src/overlay/selector.rs @@ -1,4 +1,4 @@ -use super::quickselect; +use crate::overlay::quickselect; use crate::scripting::guiwin::GuiWin; use config::keyassignment::{InputSelector, InputSelectorEntry, KeyAssignment}; use mux::termwiztermtab::TermWizTerminal; @@ -146,6 +146,8 @@ impl SelectorState { } else { changes.push(Change::Text(" ".repeat(max_label_len + 3))); } + } else if !self.always_fuzzy { + changes.push(Change::Text(" ".repeat(max_label_len + 3))); } else { changes.push(Change::Text(" ".to_string())); } diff --git a/wezterm-gui/src/termwindow/mod.rs b/wezterm-gui/src/termwindow/mod.rs index 5edf529b7..d094fb8c7 100644 --- a/wezterm-gui/src/termwindow/mod.rs +++ b/wezterm-gui/src/termwindow/mod.rs @@ -2367,6 +2367,7 @@ impl TermWindow { flags: LauncherFlags::TABS, help_text: None, fuzzy_help_text: None, + alphabet: None, }; self.show_launcher_impl(args, active_tab_idx); } @@ -2382,6 +2383,7 @@ impl TermWindow { | LauncherFlags::COMMANDS, help_text: None, fuzzy_help_text: None, + alphabet: None, }; self.show_launcher_impl(args, 0); } @@ -2418,6 +2420,9 @@ impl TermWindow { .fuzzy_help_text .unwrap_or("Fuzzy matching: ".to_string()); + let config = &self.config; + let alphabet = args.alphabet.unwrap_or(config.launcher_alphabet.clone()); + promise::spawn::spawn(async move { let args = LauncherArgs::new( &title, @@ -2427,6 +2432,7 @@ impl TermWindow { domain_id_of_current_pane, &help_text, &fuzzy_help_text, + &alphabet, ) .await; @@ -2767,6 +2773,7 @@ impl TermWindow { flags: args.flags, help_text: args.help_text.clone(), fuzzy_help_text: args.fuzzy_help_text.clone(), + alphabet: args.alphabet.clone(), }; self.show_launcher_impl(args, 0); }