Skip to content

Commit

Permalink
repl: add an internal "paused" mode
Browse files Browse the repository at this point in the history
PR-URL: #15566
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
  • Loading branch information
TimothyGu committed Nov 16, 2017
1 parent 7ce6d23 commit cff6e69
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,34 @@ function REPLServer(prompt,

eval_ = eval_ || defaultEval;

// Pause taking in new input, and store the keys in a buffer.
const pausedBuffer = [];
let paused = false;
function pause() {
paused = true;
}
function unpause() {
if (!paused) return;
paused = false;
let entry;
while (entry = pausedBuffer.shift()) {
const [type, payload] = entry;
switch (type) {
case 'key': {
const [d, key] = payload;
self._ttyWrite(d, key);
break;
}
case 'close':
self.emit('exit');
break;
}
if (paused) {
break;
}
}
}

function defaultEval(code, context, file, cb) {
var err, result, script, wrappedErr;
var wrappedCmd = false;
Expand Down Expand Up @@ -420,6 +448,10 @@ function REPLServer(prompt,
'DEP0075');

self.on('close', function emitExit() {
if (paused) {
pausedBuffer.push(['close']);
return;
}
self.emit('exit');
});

Expand Down Expand Up @@ -557,10 +589,14 @@ function REPLServer(prompt,
}
});

// Wrap readline tty to enable editor mode
// Wrap readline tty to enable editor mode and pausing.
const ttyWrite = self._ttyWrite.bind(self);
self._ttyWrite = (d, key) => {
key = key || {};
if (paused && !(self.breakEvalOnSigint && key.ctrl && key.name === 'c')) {
pausedBuffer.push(['key', [d, key]]);
return;
}
if (!self.editorMode || !self.terminal) {
ttyWrite(d, key);
return;
Expand Down

0 comments on commit cff6e69

Please sign in to comment.