Skip to content

Commit

Permalink
readline: fix key name for function keys with modifiers
Browse files Browse the repository at this point in the history
Fixes: nodejs#35251

PR-URL: nodejs#35268
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
elie-g authored and joesepi committed Oct 22, 2020
1 parent 4ae4ef4 commit 2203dc2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
11 changes: 10 additions & 1 deletion lib/internal/readline/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,13 @@ function* emitKeys(stream) {

// Parse the key itself
switch (code) {
/* xterm/gnome ESC O letter */
/* xterm/gnome ESC [ letter (with modifier) */
case '[P': key.name = 'f1'; break;
case '[Q': key.name = 'f2'; break;
case '[R': key.name = 'f3'; break;
case '[S': key.name = 'f4'; break;

/* xterm/gnome ESC O letter (without modifier) */
case 'OP': key.name = 'f1'; break;
case 'OQ': key.name = 'f2'; break;
case 'OR': key.name = 'f3'; break;
Expand Down Expand Up @@ -296,12 +302,15 @@ function* emitKeys(stream) {
} else if (ch === '\r') {
// carriage return
key.name = 'return';
key.meta = escaped;
} else if (ch === '\n') {
// Enter, should have been called linefeed
key.name = 'enter';
key.meta = escaped;
} else if (ch === '\t') {
// tab
key.name = 'tab';
key.meta = escaped;
} else if (ch === '\b' || ch === '\x7f') {
// backspace or ctrl+h
key.name = 'backspace';
Expand Down
24 changes: 23 additions & 1 deletion test/parallel/test-readline-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,13 @@ addTest('io.JS', [
]);

// Named characters
addTest('\n\r\t', [
addTest('\n\r\t\x1b\n\x1b\r\x1b\t', [
{ name: 'enter', sequence: '\n' },
{ name: 'return', sequence: '\r' },
{ name: 'tab', sequence: '\t' },
{ name: 'enter', sequence: '\x1b\n', meta: true },
{ name: 'return', sequence: '\x1b\r', meta: true },
{ name: 'tab', sequence: '\x1b\t', meta: true },
]);

// Space and backspace
Expand Down Expand Up @@ -132,6 +135,25 @@ addTest('a\x1baA\x1bA', [
{ name: 'a', sequence: '\x1bA', meta: true, shift: true },
]);

// xterm/gnome ESC [ letter (with modifiers)
/* eslint-disable max-len */
addTest('\x1b[2P\x1b[3P\x1b[4P\x1b[5P\x1b[6P\x1b[7P\x1b[8P\x1b[3Q\x1b[8Q\x1b[3R\x1b[8R\x1b[3S\x1b[8S', [
{ name: 'f1', sequence: '\x1b[2P', code: '[P', shift: true, meta: false, ctrl: false },
{ name: 'f1', sequence: '\x1b[3P', code: '[P', shift: false, meta: true, ctrl: false },
{ name: 'f1', sequence: '\x1b[4P', code: '[P', shift: true, meta: true, ctrl: false },
{ name: 'f1', sequence: '\x1b[5P', code: '[P', shift: false, meta: false, ctrl: true },
{ name: 'f1', sequence: '\x1b[6P', code: '[P', shift: true, meta: false, ctrl: true },
{ name: 'f1', sequence: '\x1b[7P', code: '[P', shift: false, meta: true, ctrl: true },
{ name: 'f1', sequence: '\x1b[8P', code: '[P', shift: true, meta: true, ctrl: true },
{ name: 'f2', sequence: '\x1b[3Q', code: '[Q', meta: true },
{ name: 'f2', sequence: '\x1b[8Q', code: '[Q', shift: true, meta: true, ctrl: true },
{ name: 'f3', sequence: '\x1b[3R', code: '[R', meta: true },
{ name: 'f3', sequence: '\x1b[8R', code: '[R', shift: true, meta: true, ctrl: true },
{ name: 'f4', sequence: '\x1b[3S', code: '[S', meta: true },
{ name: 'f4', sequence: '\x1b[8S', code: '[S', shift: true, meta: true, ctrl: true },
]);
/* eslint-enable max-len */

// xterm/gnome ESC O letter
addTest('\x1bOP\x1bOQ\x1bOR\x1bOS', [
{ name: 'f1', sequence: '\x1bOP', code: 'OP' },
Expand Down

0 comments on commit 2203dc2

Please sign in to comment.