Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

readline: document emitKeypressEvents() #6024

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc/api/readline.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,11 @@ a `'resize'` event on the `output` if/when the columns ever change

Move cursor to the specified position in a given TTY stream.

## readline.emitKeypressEvents(stream)

Causes `stream` to begin emitting `'keypress'` events corresponding to its
input.

## readline.moveCursor(stream, dx, dy)

Move cursor relative to it's current position in a given TTY stream.
Expand Down
5 changes: 2 additions & 3 deletions lib/internal/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,8 @@ function* emitKeys(stream) {
stream.emit('keypress', escaped ? undefined : s, key);
} else if (s.length === 1) {
/* Single unnamed character, e.g. "." */
stream.emit('keypress', s);
} else {
/* Unrecognized or broken escape sequence, don't emit anything */
stream.emit('keypress', s, key);
}
/* Unrecognized or broken escape sequence, don't emit anything */
}
}
30 changes: 30 additions & 0 deletions test/parallel/test-readline-emit-keypress-events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';
// emitKeypressEvents is thoroughly tested in test-readline-keys.js.
// However, that test calls it implicitly. This is just a quick sanity check
// to verify that it works when called explicitly.

require('../common');
const assert = require('assert');
const readline = require('readline');
const PassThrough = require('stream').PassThrough;
const stream = new PassThrough();
const sequence = [];
const keys = [];

readline.emitKeypressEvents(stream);

stream.on('keypress', (s, k) => {
sequence.push(s);
keys.push(k);
});

stream.write('foo');

process.on('exit', () => {
assert.deepStrictEqual(sequence, ['f', 'o', 'o']);
assert.deepStrictEqual(keys, [
{ sequence: 'f', name: 'f', ctrl: false, meta: false, shift: false },
{ sequence: 'o', name: 'o', ctrl: false, meta: false, shift: false },
{ sequence: 'o', name: 'o', ctrl: false, meta: false, shift: false }
]);
});
2 changes: 1 addition & 1 deletion test/parallel/test-readline-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function addTest(sequences, expectedKeys) {
addTest('io.JS', [
{ name: 'i', sequence: 'i' },
{ name: 'o', sequence: 'o' },
undefined, // emitted as `emit('keypress', '.', undefined)`
{ name: undefined, sequence: '.' },
{ name: 'j', sequence: 'J', shift: true },
{ name: 's', sequence: 'S', shift: true },
]);
Expand Down