mirror of
https://github.com/nodejs/node.git
synced 2024-11-29 15:06:33 +01:00
repl: add an internal "paused" mode
PR-URL: https://github.com/nodejs/node/pull/15566 Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
This commit is contained in:
parent
7ce6d23387
commit
cff6e69057
38
lib/repl.js
38
lib/repl.js
@ -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;
|
||||
@ -420,6 +448,10 @@ function REPLServer(prompt,
|
||||
'DEP0075');
|
||||
|
||||
self.on('close', function emitExit() {
|
||||
if (paused) {
|
||||
pausedBuffer.push(['close']);
|
||||
return;
|
||||
}
|
||||
self.emit('exit');
|
||||
});
|
||||
|
||||
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user