node
node copied to clipboard
readline: add paste bracket mode - fixes #45213
The paste bracket mode allows REPL to have auto-indentation that is handled differently when the user copy-pastes the code from the clipboard and the code already has an indentation.
I was testing this code with something like this, this is a basic test:
const readline = require('readline');
let cmd = '';
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// add the paste bracket markers to output code
process.stdin.on('keypress', (c, k) => {
if (k?.name?.match(/^paste-/)) {
cmd += k.sequence;
}
});
// enable paste bracket mode
process.stdout.write('\x1b[?2004h');
rl.on('line', function(line) {
cmd += line;
// we clear old paste brackets that we don't need anymore
if (cmd.match(/\x1b\[201~$/)) {
cmd = cmd.replace(/\x1b\[(200|201)~/g, '');
}
cmd += '\n';
// get rid of the opening paste bracket
const code = cmd.replace(/\x1b\[200~/g, '');
// first you check if this is multiline if the code ends
// my case is lisp code it's easy to check
// if there are balanced parentheses
if (/* multi-line */) {
if (cmd.match(/\x1b\[200~/)) {
// do indentation when copy-pasting the code
// you can check if the code is indented or not
} else {
// user pressed enter so you can use normal auto-indent
}
} else {
// evaluate the code
}
});
in the base case when you want to indent the next line you check if cmd has paste brackets code and you evaluate the code variable.
What's now? When this will be merged? It's been two months since this issue was approved.
@bnoordhuis what is the status?
@nodejs/repl - review requested
CI: https://ci.nodejs.org/job/node-test-pull-request/52793/
Will check the test when I get back home. Any clue why tests failed on Linux?
CI: https://ci.nodejs.org/job/node-test-pull-request/52987/
CI: https://ci.nodejs.org/job/node-test-pull-request/53140/
CI: https://ci.nodejs.org/job/node-test-pull-request/53149/
CI: https://ci.nodejs.org/job/node-test-pull-request/53199/
CI: https://ci.nodejs.org/job/node-test-pull-request/53227/
Landed in 87af913b66eab78088acfd7f3b57d35e22c5f1ba