joyent/node — pull request #3587 by refractalize (2012-06-29 09:54:38+00:00)
Title: readline: fix for unicode prompts

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.