Skip to content

Commit 7f11c72

Browse files
cjihrigtargos
authored andcommitted
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: #29149 Fixes: #29111 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 3c346b8 commit 7f11c72

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

lib/readline.js

+12-7
Original file line numberDiff line numberDiff line change
@@ -816,15 +816,20 @@ function _ttyWriteDumb(s, key) {
816816
if (this._sawReturnAt && key.name !== 'enter')
817817
this._sawReturnAt = 0;
818818

819-
if (key.ctrl && key.name === 'c') {
820-
if (this.listenerCount('SIGINT') > 0) {
821-
this.emit('SIGINT');
822-
} else {
823-
// This readline instance is finished
819+
if (key.ctrl) {
820+
if (key.name === 'c') {
821+
if (this.listenerCount('SIGINT') > 0) {
822+
this.emit('SIGINT');
823+
} else {
824+
// This readline instance is finished
825+
this.close();
826+
}
827+
828+
return;
829+
} else if (key.name === 'd') {
824830
this.close();
831+
return;
825832
}
826-
827-
return;
828833
}
829834

830835
switch (key.name) {

test/pseudo-tty/repl-dumb-tty.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
'use strict';
2-
require('../common');
2+
const common = require('../common');
33

44
process.env.TERM = 'dumb';
55

66
const repl = require('repl');
7+
const ArrayStream = require('../common/arraystream');
78

89
repl.start('> ');
910
process.stdin.push('console.log("foo")\n');
@@ -13,3 +14,18 @@ process.stdin.push('console.dir({ a: 1 })\n');
1314
process.stdin.push('{ a: 1 }\n');
1415
process.stdin.push('\n');
1516
process.stdin.push('.exit\n');
17+
18+
// Verify Control+D support.
19+
{
20+
const stream = new ArrayStream();
21+
const replServer = repl.start({
22+
prompt: '> ',
23+
terminal: true,
24+
input: stream,
25+
output: stream,
26+
useColors: false
27+
});
28+
29+
replServer.on('close', common.mustCall());
30+
replServer.write(null, { ctrl: true, name: 'd' });
31+
}

0 commit comments

Comments
 (0)