Skip to content
Permalink
Browse files

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 <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information...
cjihrig authored and targos committed Aug 15, 2019
1 parent 3c346b8 commit 7f11c72cc5ef7507d52976b7efeec631d9490662
Showing with 29 additions and 8 deletions.
  1. +12 −7 lib/readline.js
  2. +17 −1 test/pseudo-tty/repl-dumb-tty.js
@@ -816,15 +816,20 @@ function _ttyWriteDumb(s, key) {
if (this._sawReturnAt && key.name !== 'enter')
this._sawReturnAt = 0;

if (key.ctrl && key.name === 'c') {
if (this.listenerCount('SIGINT') > 0) {
this.emit('SIGINT');
} else {
// This readline instance is finished
if (key.ctrl) {
if (key.name === 'c') {
if (this.listenerCount('SIGINT') > 0) {
this.emit('SIGINT');
} else {
// This readline instance is finished
this.close();
}

return;
} else if (key.name === 'd') {
this.close();
return;
}

return;
}

switch (key.name) {
@@ -1,9 +1,10 @@
'use strict';
require('../common');
const common = require('../common');

process.env.TERM = 'dumb';

const repl = require('repl');
const ArrayStream = require('../common/arraystream');

repl.start('> ');
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('\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' });
}

0 comments on commit 7f11c72

Please sign in to comment.
You can’t perform that action at this time.