-
-
Notifications
You must be signed in to change notification settings - Fork 595
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
feat: add support for custom keybindings and editor actions in the REPL #2739
Conversation
Signed-off-by: Snehil Shah <snehilshah.989@gmail.com>
Another solution to the problem in OP is, we can expect the user to give us the ASCII sequence of the keypress instead. Every keypress will always have a Now this is not user-friendly as the user is expected to know the control sequence of the keypress they are wanting to assign. So, another approach would be to maintain a table mapping all possible keybindings to their corresponding emitted control sequences. Something like: const CONTROL_SEQUENCES = {
'\x01': 'Ctrl+A',
'\x02': 'Ctrl+B',
'\x03': 'Ctrl+C',
'\x04': 'Ctrl+D',
'\x05': 'Ctrl+E',
'\x06': 'Ctrl+F',
'\x07': 'Ctrl+G',
'\x08': 'Ctrl+H',
'\x09': 'Ctrl+I',
'\x0A': 'Ctrl+J',
'\x0B': 'Ctrl+K',
'\x0C': 'Ctrl+L',
'\x0D': 'Ctrl+M',
'\x0E': 'Ctrl+N',
'\x0F': 'Ctrl+O',
'\x10': 'Ctrl+P',
'\x11': 'Ctrl+Q',
'\x12': 'Ctrl+R',
'\x13': 'Ctrl+S',
'\x14': 'Ctrl+T',
'\x15': 'Ctrl+U',
'\x16': 'Ctrl+V',
'\x17': 'Ctrl+W',
'\x18': 'Ctrl+X',
'\x19': 'Ctrl+Y',
'\x1A': 'Ctrl+Z'
}; But not sure if control sequences and their corresponding keys are standardized across emulators..? |
https://github.com/nodejs/node/blob/main/lib/internal/readline/utils.js#L59
|
@Snehil-Shah Not requiring providing a Enforcing xterm standards for key parsing could be feasible. xterm is widely used and AFAIK many terminal emulators aim for compatibility with xterm, so this sounds worth investigating. I also do think that it would be a pragmatic choice to handle the |
Now if we decide to go ahead and enforce xterm control sequences, we also have the flexibility to change the input format the user can give. So we can have something like the string "CTRL-E" as input, and map this to the corresponding xterm control sequence.. |
Signed-off-by: Snehil Shah <snehilshah.989@gmail.com>
Signed-off-by: Snehil Shah <snehilshah.989@gmail.com>
Went ahead and added parsing of unrecognized keypresses. Fortunately, there were not many cases to cover, the above problems were mainly around keys involving symbols. @Planeshifter I think it would be better to stick to |
Signed-off-by: Snehil Shah <snehilshah.989@gmail.com>
Signed-off-by: Snehil Shah <snehilshah.989@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me!
One small comment: I believe we can safely assume that _rli.line
is a string, correct? Hence, we could use the base package equivalents, so e.g. @stdlib/string/base/uppercase
instead of @stdlib/string/uppercase
, to bypass runtime argument validation of these functions.
@Planeshifter Makes sense.. |
We should go ahead and make those changes. Too easy to forget to do them later. |
Signed-off-by: Snehil Shah <snehilshah.989@gmail.com>
For instance, the right arrow key should also complete the completion. It would be a better idea to clean up this logic in the PR where we add support for configuring keybindings for existing actions. Signed-off-by: Snehil Shah <snehilshah.989@gmail.com>
Signed-off-by: Snehil Shah <snehilshah.989@gmail.com>
Signed-off-by: Snehil Shah <snehilshah.989@gmail.com>
@Snehil-Shah I'll defer to your judgement on that. |
Signed-off-by: Snehil Shah <snehilshah.989@gmail.com>
Signed-off-by: Snehil Shah <snehilshah.989@gmail.com>
Signed-off-by: Snehil Shah <snehilshah.989@gmail.com>
Signed-off-by: Snehil Shah <snehilshah.989@gmail.com>
@Snehil-Shah Gentle ping regarding resolving the above comments. |
Coverage Report
The above coverage report was generated for the changes in this PR. |
Signed-off-by: Snehil Shah <snehilshah.989@gmail.com>
@kgryte cleaned up some stuff. Regarding keybindings overlapping with system preferences, I don't see a way out. |
@Snehil-Shah Maybe the way out is just clearly documenting that overlap may happen and that there is likely also going to be some platform variability, so MMV. |
@kgryte should I update the docs in this PR itself? I'll raise another PR to add keybindings to existing actions.. |
@Snehil-Shah Let's punt updating the docs, and let the keybindings fly under the radar for the time being while we test out, and then we can add to docs in a follow-up PR. |
Signed-off-by: Athan <kgryte@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apart from one small comment, this PR LGTM. Once resolved, this PR should be ready for merge. Thanks, @Snehil-Shah!
Signed-off-by: Snehil Shah <snehilshah.989@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, @Snehil-Shah! Great to have this finally land. 🚀
This comment was marked as outdated.
This comment was marked as outdated.
PR-URL: stdlib-js#2739 Ref: stdlib-js#2647 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com> Signed-off-by: Athan Reines <kgryte@gmail.com> Signed-off-by: Snehil Shah <snehilshah.989@gmail.com>
Towards #2647.
Description
This pull request:
Related Issues
This pull request:
Questions
For the sake of user experience, I decided the key object can have 4 properties:
name
,ctrl
,shift
, andmeta
. In actuality, a readlinekey
object also has properties likesequence
andcode
, which mainly contain the escape sequence of the keypress.In most keypresses, the
name
,ctrl
,shift
, andmeta
properties are correctly emitted and are enough to determine the action we have to trigger. But in some cases, for instance, CTRL+/, instead of emitting an event with{ 'name': '/', 'ctrl': true }
, it emits a sequence{ 'sequence': '\x1f' }
. So if a user has keybindings with CTRL+/ as the key, it won't work, as such a keypress is never emitted.Now such cases might be limited, as readline only uses the
sequence
prop to identify two keypress actions, namely undo and redo.Maybe we can handle these two special cases manually in code? Not sure if a robust solution exists.
Ofcourse, we can also punt the entire responsibility of giving the readline-ready
key
object, onto the user, but that's not user friendly IMO.Other
No.
Checklist
@stdlib-js/reviewers