Permalink
Browse files

readline: fix `line` event, if input emit 'end'

If an input stream would emit `end` event, like
`fs.createReadStream`, then readline need to get the last line
correctly even though that line isnt ended with `\n`.
  • Loading branch information...
1 parent c980280 commit cfe0bab85b8b0974c3683c434fa33663e0b02906 @yorkie yorkie committed with tjfontaine Jan 22, 2014
Showing with 30 additions and 3 deletions.
  1. +8 −0 lib/readline.js
  2. +21 −1 test/simple/test-readline-interface.js
  3. +1 −2 test/simple/test-readline-set-raw-mode.js
View
@@ -91,6 +91,9 @@ function Interface(input, output, completer, terminal) {
}
function onend() {
+ if (util.isString(self._line_buffer) && self._line_buffer.length > 0) {
+ self.emit('line', self._line_buffer);
+ }
self.close();
}
@@ -118,6 +121,11 @@ function Interface(input, output, completer, terminal) {
// input usually refers to stdin
input.on('keypress', onkeypress);
+ input.on('end', function inputEnd() {
+ if (util.isString(self.line) && self.line.length > 0)
+ self.emit('line', self.line);
+ self.close();
+ });
// Current line
this.line = '';
@@ -113,6 +113,27 @@ FakeInput.prototype.end = function() {};
assert.equal(callCount, expectedLines.length - 1);
rli.close();
+ // sending multiple newlines at once that does not end with a new(empty)
+ // line and a `end` event
+ fi = new FakeInput();
+ rli = new readline.Interface({ input: fi, output: fi, terminal: terminal });
+ expectedLines = ['foo', 'bar', 'baz', ''];
+ callCount = 0;
+ rli.on('line', function(line) {
+ assert.equal(line, expectedLines[callCount]);
+ callCount++;
+ });
+ rli.on('close', function() {
+ callCount++;
+ })
+ fi.emit('data', expectedLines.join('\n'));
+ fi.emit('end');
+ assert.equal(callCount, expectedLines.length);
+ rli.close();
+
+ // sending multiple newlines at once that does not end with a new line
+ // and a `end` event(last line is)
+
// \r\n should emit one line event, not two
fi = new FakeInput();
rli = new readline.Interface({ input: fi, output: fi, terminal: terminal });
@@ -214,6 +235,5 @@ FakeInput.prototype.end = function() {};
assert.equal(readline.getStringWidth('\u001b[31m\u001b[39m'), 0);
assert.equal(readline.getStringWidth('> '), 2);
- assert.deepEqual(fi.listeners('end'), []);
assert.deepEqual(fi.listeners(terminal ? 'keypress' : 'data'), []);
});
@@ -49,7 +49,7 @@ var rli = readline.createInterface({
output: stream,
terminal: true
});
-assert(rli.terminal)
+assert(rli.terminal);
assert(rawModeCalled);
assert(resumeCalled);
assert(!pauseCalled);
@@ -85,7 +85,6 @@ assert(rawModeCalled);
assert(!resumeCalled);
assert(pauseCalled);
-assert.deepEqual(stream.listeners('end'), []);
assert.deepEqual(stream.listeners('keypress'), []);
// one data listener for the keypress events.
assert.equal(stream.listeners('data').length, 1);

0 comments on commit cfe0bab

Please sign in to comment.