prompts icon indicating copy to clipboard operation
prompts copied to clipboard

Add ability to set hotkeys for multiselect

Open NickHeiner opened this issue 5 years ago • 13 comments

image

I'd like the ability to customize the hotkey options available to the user for multiselect. My dream is to have something like git add -p, where you get hotkeys like:

Stage this hunk [y,n,q,a,d,e,?]?

Example usage:

await prompts({
      // ...
      hotkeys: {
        d: {
          handle() {
            /* handle user action */
          },
          instruction: 'Save current progress and stop answering questions.'
        }
      },
    })

This PR is a draft. If you like the idea, I'll add tests / docs.

NickHeiner avatar Nov 09 '19 00:11 NickHeiner

This is cool – I like the idea a lot. Wonder if this could be a completely new prompt type?

terkelg avatar Nov 09 '19 03:11 terkelg

I think it could be a new prompt type, but at least for my use case, I'd also like to add it to multiselect. I'll add tests / docs to this PR next.

NickHeiner avatar Nov 10 '19 18:11 NickHeiner

This PR currently isn't passing on Node 6. (And there are a few cases, most notable lack of async/await support, that make supporting Node 6 make the code less readable.)

Node 6 is currently considered to be "end of life". It was released in 2016. How would you feel about dropping support for it?

NickHeiner avatar Nov 11 '19 22:11 NickHeiner

@terkelg how does this look to you now?

NickHeiner avatar Nov 11 '19 22:11 NickHeiner

@NickHeiner thank you for the PR! I'll have a look at this over the weekend. Quick note: You can use async/await. It's already used quite a few places

terkelg avatar Nov 13 '19 15:11 terkelg

Ok @terkelg I addressed your comments. It's now passing in Node 6.

NickHeiner avatar Nov 17 '19 00:11 NickHeiner

@terkelg I'm ready to move forward on this whenever you are. :smile:

NickHeiner avatar Nov 27 '19 19:11 NickHeiner

Looks good. I wonder if we should pass some arguments to the handler callback? Like a reference to the prompts so that you can provide some sort of visual feedback?

terkelg avatar Nov 28 '19 15:11 terkelg

Looks good. I wonder if we should pass some arguments to the handler callback? Like a reference to the prompts so that you can provide some sort of visual feedback?

Sounds interesting, but I'm not sure precisely what you have in mind. Can you elaborate?

NickHeiner avatar Nov 29 '19 18:11 NickHeiner

Right now there's no feedback to the user to indicate a hotkey has been pressed/activated. I wonder if there could be a way to change selected mode in the hotkey callbacks?

terkelg avatar Nov 30 '19 21:11 terkelg

If a hotkey changes which answers are selected, that's reflected in the rendered output. In this gif, we can see that pressing a (which is a "built in hotkey") and r (which is a hotkey added by this PR) both provide the same type of feedback.

multiselect hotkey feedback

Does this address what you're looking for, or am I misunderstanding?

NickHeiner avatar Dec 03 '19 20:12 NickHeiner

I'll have a look at it this weekend – I didn't get that for some reason

terkelg avatar Dec 05 '19 14:12 terkelg

        {
            type: 'multiselect',
            name: 'multicolor',
            message: 'Pick colors',
            hint: false,
            hotkeys: {
                d: {
                    handle(x, y) {
                        /* handle user action */
                    },
                    instruction: 'Save current progress and stop answering questions.'
                }
            },
            choices: [
                { title: 'Red', description: 'This option has a description.', value: '#ff0000' },
                { title: 'Green', value: '#00ff00' },
                { title: 'Yellow', value: '#ffff00', disabled: true },
                { title: 'Blue', value: '#0000ff' }
            ]
        },

When I run this I get no visual feedback as a user that I hit the hotkey?

terkelg avatar Dec 08 '19 18:12 terkelg