Skip to content

Commit

Permalink
add buffer to stdout to prevent prompt flicker in Node REPL
Browse files Browse the repository at this point in the history
  • Loading branch information
jcubic committed Nov 6, 2024
1 parent 38f3d79 commit 147fc84
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion bin/lips.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ if (options.version || options.V) {
var terminal = !!process.stdin.isTTY && !(process.env.EMACS || process.env.INSIDE_EMACS);
rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
output: buffer(process.stdout),
prompt: prompt,
terminal
});
Expand All @@ -446,6 +446,40 @@ function is_close(token) {
return [')', ']'].includes(token);
}

// buffer Proxy to prevent flicker when Node writes to stdout
function buffer(stream) {
const fname = '/home/kuba/lips__debug.log';
const buffer = [];
function log(data) {
return;
fs.appendFile(fname, data + '\n', function (err) {
if (err) throw err;
});
}
return new Proxy(stream, {
get(target, prop) {
if (prop === 'write') {
return function(data, ...args) {
log(data);
if (data.match(/\x1b\[(?:1G|0J)|(^(?:lips>|\.\.\.) )/)) {
buffer.push(data);
log('buffer :::');
} else if (buffer.length) {
const payload = buffer.join('') + data;
buffer.length = 0;
log(`flush ::: ${payload}`);
target.write(payload, ...args);
} else {
log('write :::');
target.write(data, ...args);
}
}
}
return target[prop];
}
});
}

// find matching open parentheses. The last token is always
// a closing parenthesis
function matched_token(code) {
Expand Down

0 comments on commit 147fc84

Please sign in to comment.