Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
readline: fix line event, if input emit 'end'
Browse files Browse the repository at this point in the history
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
yorkie authored and tjfontaine committed Feb 18, 2014
1 parent c980280 commit cfe0bab
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
8 changes: 8 additions & 0 deletions lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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 = '';
Expand Down
22 changes: 21 additions & 1 deletion test/simple/test-readline-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down Expand Up @@ -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'), []);
});
3 changes: 1 addition & 2 deletions test/simple/test-readline-set-raw-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var rli = readline.createInterface({
output: stream,
terminal: true
});
assert(rli.terminal)
assert(rli.terminal);
assert(rawModeCalled);
assert(resumeCalled);
assert(!pauseCalled);
Expand Down Expand Up @@ -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.