readline: fix for unicode prompts #3587

Closed
wants to merge 1 commit into from

4 participants

@refractalize

Bug:

With a unicode prompt, type some text into it, move the cursor backwards and forwards, delete some characters, etc, the cursor and the text are mangled and misplaced.

I.e. Run this script:

var readline = require('readline');

var rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

rl.setPrompt('✪☪⍟✩');

rl.on('line', function (cmd) {
  console.log('You just typed: '+cmd);
  rl.prompt();
});

rl.prompt();

Fix: use the prompts character length instead of the utf-8 byte length as the cursor offset.

This patch will fix ttys running utf-8. However, ASCII ttys are still weird for unicode prompts (cursor location more left than should be.) Arguably writing utf-8 to ASCII ttys is broken anyway, and should be up to application to detect (using $LANG and $LC_*) and use an ASCII prompt. Nevertheless, this patch makes it better than it was before.

@refractalize refractalize readline: fix for unicode prompts
prompt length is char length, not byte length
76d72bf
@Sannis

Related issue: #3418

@refractalize

Kind of related. Issue #3418 has to do with the way readline.js is implemented assuming one column = one character, this issue has to do with assuming one column = one (utf-8) byte. Ruby (I think) uses readline(3), which doesn't suffer from this. Not sure how readline(3) does it, but there's a lot of C code in readline, no doubt some of it dedicated to solving this issue.

@langpavel

Also note that ANSI escape codes ("coloring" REPL) break cursor position too

@bnoordhuis
Node.js Foundation member

Good catch. Can you sign the CLA?

@refractalize

Signed

@bnoordhuis
Node.js Foundation member

Thanks Tim, landed in 0dba28b.

@bnoordhuis bnoordhuis closed this Jul 6, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment