console-rs / dialoguer

Rust utility library for nice command line prompts and similar things
MIT License
1.31k stars 143 forks source link

Custom shortcuts/keybindings/hotkeys support #285

Open Andrew15-5 opened 11 months ago

Andrew15-5 commented 11 months ago

I'm new to Rust, but after using inquirerpy for some time, I now assume that every CLI prompter allows custom keybinding/shortcuts/hotkeys. But now I know that this assumption is wrong. Why? Why I can't define custom shortcuts?

I like to use Ctrl + k and Ctrl + j to go up and down when selecting something. Specifically, when using FuzzySelect. I can first insert text to filter options and then use Ctrl + j to quickly select option if it is not the first in the list. I can use Tab, but this is not the point.

pksunkara commented 11 months ago

Why? Why I can't define custom shortcuts?

Because nobody needed it until you and therefore nobody contributed it. Please feel free to contribute.

Andrew15-5 commented 11 months ago

Huh, is that so? 6 years and I'm the first one, hmm.

I would love to contribute, but I need to gain some experience first.

Andrew15-5 commented 6 months ago

I tried doing what was said here: https://github.com/console-rs/console/issues/125#issuecomment-1454487396.

diff --git a/src/prompts/fuzzy_select.rs b/src/prompts/fuzzy_select.rs
index 9f06798..894561e 100644
--- a/src/prompts/fuzzy_select.rs
+++ b/src/prompts/fuzzy_select.rs
@@ -280,7 +280,7 @@ impl FuzzySelect<'_> {
                 (Key::Char('i' | 'a'), _, true) => {
                     vim_mode = false;
                 }
-                (Key::ArrowUp | Key::BackTab, _, _) | (Key::Char('k'), _, true)
+                (Key::ArrowUp | Key::BackTab | Key::Char('\u{b}'), _, _) | (Key::Char('k'), _, true)
                     if !filtered_list.is_empty() =>
                 {
                     if sel == Some(0) {
@@ -299,7 +299,7 @@ impl FuzzySelect<'_> {
                     };
                     term.flush()?;
                 }
-                (Key::ArrowDown | Key::Tab, _, _) | (Key::Char('j'), _, true)
+                (Key::ArrowDown | Key::Tab | Key::Enter, _, _) | (Key::Char('j'), _, true)
                     if !filtered_list.is_empty() =>
                 {
                     sel = match sel {

The problem is not only that Ctrl doesn't work and Ctrl+J is actually registered (at least by the console library) as Enter (which means I can't confirm my selection), but that modifiers are not supported (natively): https://github.com/console-rs/console/issues/125.

https://www.physics.udel.edu/~watson/scen103/ascii.html:

image

So with that issue being opened, there is no sane way of adding shortcut support to the dialoguer (just noticed that both projects are under the same owner).