Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Paste brackets mode in REPL #16530

Open
jcubic opened this issue Jan 19, 2025 · 1 comment
Open

Paste brackets mode in REPL #16530

jcubic opened this issue Jan 19, 2025 · 1 comment
Labels
enhancement New feature or request repl An issue with `bun repl`

Comments

@jcubic
Copy link

jcubic commented Jan 19, 2025

What is the problem this feature would solve?

I just tested my Node.js REPL in bun, and it works fine except paste bracket mode.

To enable the mode you use ?2004h ANSI escape code:

process.stdout.write('\x1b[?2004h');

and then you will get \x1b\[201~ and \x1b\[200~ ANSI escapes around the pasted text.

What is the feature you are proposing to solve the problem?

It solves the issue with auto indent inside REPL, if you create auto-indent you do this in line by line basis, and you don't know if the code already have indentation (when you copy paste the code often the code is formatted).

The auto indent should only happen when you press enter, but the REPL don't differentiate between enter and line from copy pasted. Except when you use paste bracket mode.

NOTE: I've added this feature to Node.js, if you're interested I can see if I can contribute and add same feature to Bun.

Node.js require this piece of code for the escape appear in the output:

let cmd;

process.stdin.on('keypress', (key, meta) => {
  if (meta?.name?.match(/^paste-/)) {
    cmd += meta.sequence;
  }
});
process.stdout.write('\x1b[?2004h');

rl.on('line', function(line) {
   cmd += line;
});

Now I think this is not a good API, the code should always be added when in bracket mode, but this is how it was implemented.

If you're interested, I've written an article about this on my blog:

I just noticed that the code is there:

https://github.com/oven-sh/bun/blob/main/src/js/node/readline.ts#L466-L472

But bun don't behave like Node.js, when you paste, the text is wrapped in ~, but you can't distinguish that from text that use ~, you need an escape sequence.

What alternatives have you considered?

There are no alternatives, the code without this feature is a mess, and it doesn't work correctly.

@jcubic jcubic added the enhancement New feature or request label Jan 19, 2025
@RiskyMH RiskyMH added the repl An issue with `bun repl` label Jan 20, 2025
@RiskyMH
Copy link
Member

RiskyMH commented Jan 20, 2025

Fwiw the repl in current state is a npm package that appears to be inactive https://github.com/jhmaster2000/bun-repl, there are plans to rewrite in native zig sometime though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request repl An issue with `bun repl`
Projects
None yet
Development

No branches or pull requests

2 participants