Feat: expose a key in context for a custom keypress handler
Exposing a key to add a custom keypress handler option in the context object, would make it easy to add some functionality without needing to write a new plugin (like coping select to allow extra buttons to be clicked).
I still would like to see this supported for all elements in by using context ...
... maybe this gives someone ideas ...
... Playing around and trying to find a way on how this could be solved, I ended up editing select/checkbox/input/... to see what I needed and how inquirer was build.
This is a very crude hack, it came down to this: ( in my testcase i just used options )
//checkbox
useKeypress(async (key, rl) => {
if (config.keypressHandler) {
let act = await config.keypressHandler({key, active, setActive, items, theme}); // may optionally return {isDone, isConsumed}
if (act?.isDone) {
isDone = true;
setStatus('done');
done(items.filter(isChecked).map((choice) => choice.value));
}
if (act?.isConsumed) return; // if isDone & isConsumed, do not trigger validation
}
if (isEnterKey(key) || isDone) {
//....
- so the handler could always get the active index when a key is pressed
- overwrite a default keypress (isConsumed)
- always exit out and returning the value (isDone)
- exit out, setting an external var like wasAborted = true, so after isDone exits, after the await checkbox(), i can check with wasAborted to see what to do next
- set another active element after filtering items
- change help messages and more
My first try was to export everything
let act = await config.keypressHandler({key, rl, shortcuts, theme, firstRender, status, setStatus, prefix, items, setItems, active, setActive, showHelpTip, setShowHelpTip});
My crude implementations:
https://github.com/BananaAcid/ai-operator/blob/main/src/libs/%40inquirer-contrib/checkbox-with-actions.ts https://github.com/BananaAcid/ai-operator/blob/main/src/libs/%40inquirer-contrib/input-with-actions.ts https://github.com/BananaAcid/ai-operator/blob/main/src/libs/%40inquirer-contrib/select-with-actions.ts https://github.com/BananaAcid/ai-operator/blob/main/src/libs/%40inquirer-contrib/search-with-actions.ts
Usage: https://github.com/BananaAcid/ai-operator/blob/1.0.40/src/index.ts#L1052-L1087
I think a user space prompt is the right place for this feature to live in. Thanks for posting your implementation!