Skip to content

Commit

Permalink
readline: handle input starting with control chars
Browse files Browse the repository at this point in the history
Handle control characters only when there is a single byte in the
stream, otherwise fall through to the standard multibyte handling.
  • Loading branch information
Eric Schrock authored and tjfontaine committed Sep 23, 2013
1 parent 7c554a5 commit 35ae696
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,7 @@ function emitKey(stream, s) {
key.name = 'space';
key.meta = (s.length === 2);

} else if (s <= '\x1a') {
} else if (s.length === 1 && s <= '\x1a') {
// ctrl+letter
key.name = String.fromCharCode(s.charCodeAt(0) + 'a'.charCodeAt(0) - 1);
key.ctrl = true;
Expand Down
12 changes: 12 additions & 0 deletions test/simple/test-readline-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,18 @@ FakeInput.prototype.end = function() {};
assert.equal(callCount, expectedLines.length - 1);
rli.close();

// \r at start of input should output blank line
fi = new FakeInput();
rli = new readline.Interface({ input: fi, output: fi, terminal: true });
expectedLines = ['', 'foo' ];
callCount = 0;
rli.on('line', function(line) {
assert.equal(line, expectedLines[callCount]);
callCount++;
});
fi.emit('data', '\rfoo\r');
assert.equal(callCount, expectedLines.length);
rli.close();

// sending a multi-byte utf8 char over multiple writes
var buf = Buffer('☮', 'utf8');
Expand Down

0 comments on commit 35ae696

Please sign in to comment.