node icon indicating copy to clipboard operation
node copied to clipboard

readline: add paste bracket mode - fixes #45213

Open jcubic opened this issue 1 year ago • 2 comments

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.

jcubic avatar Mar 18 '23 17:03 jcubic

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.

jcubic avatar Mar 18 '23 17:03 jcubic

What's now? When this will be merged? It's been two months since this issue was approved.

jcubic avatar Jun 16 '23 09:06 jcubic

@bnoordhuis what is the status?

jcubic avatar Jul 06 '23 12:07 jcubic

@nodejs/repl - review requested

bnoordhuis avatar Jul 07 '23 18:07 bnoordhuis

CI: https://ci.nodejs.org/job/node-test-pull-request/52793/

nodejs-github-bot avatar Jul 16 '23 14:07 nodejs-github-bot

Will check the test when I get back home. Any clue why tests failed on Linux?

jcubic avatar Jul 16 '23 14:07 jcubic

CI: https://ci.nodejs.org/job/node-test-pull-request/52987/

nodejs-github-bot avatar Jul 30 '23 03:07 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/53140/

nodejs-github-bot avatar Aug 10 '23 18:08 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/53149/

nodejs-github-bot avatar Aug 10 '23 23:08 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/53199/

nodejs-github-bot avatar Aug 11 '23 15:08 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/53227/

nodejs-github-bot avatar Aug 12 '23 10:08 nodejs-github-bot

Landed in 87af913b66eab78088acfd7f3b57d35e22c5f1ba

aduh95 avatar Aug 12 '23 11:08 aduh95