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`.
This commit is contained in:
parent
c980280159
commit
cfe0bab85b
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user