readline: close dumb terminals on Control+D

This commit adds support for closing a readline interface
on Control+D when the terminal is dumb.

PR-URL: https://github.com/nodejs/node/pull/29149
Fixes: https://github.com/nodejs/node/issues/29111
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
cjihrig 2019-08-15 12:37:29 -04:00
parent a5edceea04
commit 93b341ed01
No known key found for this signature in database
GPG Key ID: 7434390BDBE9B9C5
2 changed files with 30 additions and 9 deletions

View File

@ -816,15 +816,20 @@ function _ttyWriteDumb(s, key) {
if (this._sawReturnAt && key.name !== 'enter') if (this._sawReturnAt && key.name !== 'enter')
this._sawReturnAt = 0; this._sawReturnAt = 0;
if (key.ctrl && key.name === 'c') { if (key.ctrl) {
if (this.listenerCount('SIGINT') > 0) { if (key.name === 'c') {
this.emit('SIGINT'); if (this.listenerCount('SIGINT') > 0) {
} else { this.emit('SIGINT');
// This readline instance is finished } else {
this.close(); // This readline instance is finished
} this.close();
}
return; return;
} else if (key.name === 'd') {
this.close();
return;
}
} }
switch (key.name) { switch (key.name) {

View File

@ -1,9 +1,10 @@
'use strict'; 'use strict';
require('../common'); const common = require('../common');
process.env.TERM = 'dumb'; process.env.TERM = 'dumb';
const repl = require('repl'); const repl = require('repl');
const ArrayStream = require('../common/arraystream');
repl.start('> '); repl.start('> ');
process.stdin.push('console.log("foo")\n'); process.stdin.push('console.log("foo")\n');
@ -13,3 +14,18 @@ process.stdin.push('console.dir({ a: 1 })\n');
process.stdin.push('{ a: 1 }\n'); process.stdin.push('{ a: 1 }\n');
process.stdin.push('\n'); process.stdin.push('\n');
process.stdin.push('.exit\n'); process.stdin.push('.exit\n');
// Verify Control+D support.
{
const stream = new ArrayStream();
const replServer = repl.start({
prompt: '> ',
terminal: true,
input: stream,
output: stream,
useColors: false
});
replServer.on('close', common.mustCall());
replServer.write(null, { ctrl: true, name: 'd' });
}