Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 56 additions & 10 deletions lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,16 +454,27 @@ Interface.prototype._normalWrite = function(b) {
}
};

// Count the Unicode code points in a string.
function codePointLen(str) {
let count = 0;
// eslint-disable-next-line no-unused-vars
for (const char of str) {
count++;
}
return count;
}
Comment thread
Avi-D-coder marked this conversation as resolved.
Outdated

Interface.prototype._insertString = function(c) {
if (this.cursor < this.line.length) {
var beg = this.line.slice(0, this.cursor);
var end = this.line.slice(this.cursor, this.line.length);
this.line = beg + c + end;
this.cursor += c.length;
// Count Unicode code points
this.cursor += codePointLen(c);
this._refreshLine();
} else {
this.line += c;
this.cursor += c.length;
this.cursor += codePointLen(c);

if (this._getCursorPos().cols === 0) {
this._refreshLine();
Expand Down Expand Up @@ -580,27 +591,57 @@ Interface.prototype._wordLeft = function() {
Interface.prototype._wordRight = function() {
if (this.cursor < this.line.length) {
var trailing = this.line.slice(this.cursor);
var match = trailing.match(/^(?:\s+|\W+|\w+)\s*/);
var match = trailing.match(/^(?:\s+|[^\w\s]+|\w+)\s*/);
this._moveCursor(match[0].length);
}
};

function codePointLeftOf(i, str) {
// obtain the code point to the left
let previousChar;
let len = 0;
for (const char of str) {
len += char.length;
previousChar = char;
if (len === i) {
return previousChar;
}
}
}
Comment thread
Avi-D-coder marked this conversation as resolved.

function codePointRightOf(i, str) {
// obtain the code point to the right
let len = 0;
for (const char of str) {
len += char.length;
if (len > i) {
// UTF-16 units comprising the character
return char;
}
}
}
Comment thread
Avi-D-coder marked this conversation as resolved.

Interface.prototype._deleteLeft = function() {
if (this.cursor > 0 && this.line.length > 0) {
this.line = this.line.slice(0, this.cursor - 1) +
// The number of UTF-16 units comprising the character to the left
const charSize = codePointLeftOf(this.cursor, this.line).length;
this.line = this.line.slice(0, this.cursor - charSize) +
this.line.slice(this.cursor, this.line.length);

this.cursor--;
this.cursor -= charSize;
this._refreshLine();
}
};


Interface.prototype._deleteRight = function() {
this.line = this.line.slice(0, this.cursor) +
this.line.slice(this.cursor + 1, this.line.length);
this._refreshLine();
if (this.cursor < this.line.length) {
// The number of UTF-16 units comprising the character to the left
const charSize = codePointRightOf(this.cursor, this.line).length;
this.line = this.line.slice(0, this.cursor) +
this.line.slice(this.cursor + charSize, this.line.length);
this._refreshLine();
}
};


Expand Down Expand Up @@ -952,11 +993,16 @@ Interface.prototype._ttyWrite = function(s, key) {
break;

case 'left':
this._moveCursor(-1);
if (this.cursor > 0) {
// obtain the code point to the left
this._moveCursor(-codePointLeftOf(this.cursor, this.line).length);
}
Comment thread
Avi-D-coder marked this conversation as resolved.
Outdated
break;

case 'right':
this._moveCursor(+1);
if (this.cursor < this.line.length) {
this._moveCursor(+codePointRightOf(this.cursor, this.line).length);
}
break;

case 'home':
Expand Down
142 changes: 142 additions & 0 deletions test/parallel/test-readline-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,94 @@ function isWarned(emitter) {
rli.close();
}

// Back and Forward one astral character
{
const fi = new FakeInput();
const rli = new readline.Interface({
input: fi,
output: fi,
prompt: '',
terminal: terminal
});
fi.emit('data', '💻');

// move left one character/code point
fi.emit('keypress', '.', { name: 'left' });
let cursorPos = rli._getCursorPos();
assert.strictEqual(cursorPos.rows, 0);
assert.strictEqual(cursorPos.cols, 0);

// move right one character/code point
fi.emit('keypress', '.', { name: 'right' });
cursorPos = rli._getCursorPos();
assert.strictEqual(cursorPos.rows, 0);
assert.strictEqual(cursorPos.cols, 2);

rli.on('line', common.mustCall((line) => {
assert.strictEqual(line, '💻');
}));
fi.emit('data', '\n');
rli.close();
}

// Two astral characters left
{
const fi = new FakeInput();
const rli = new readline.Interface({
input: fi,
output: fi,
prompt: '',
terminal: terminal
});
fi.emit('data', '💻');

// move left one character/code point
fi.emit('keypress', '.', { name: 'left' });
let cursorPos = rli._getCursorPos();
assert.strictEqual(cursorPos.rows, 0);
assert.strictEqual(cursorPos.cols, 0);

fi.emit('data', '🐕');
cursorPos = rli._getCursorPos();
assert.strictEqual(cursorPos.rows, 0);
assert.strictEqual(cursorPos.cols, 2);

rli.on('line', common.mustCall((line) => {
assert.strictEqual(line, '🐕💻');
}));
fi.emit('data', '\n');
rli.close();
}

// Two astral characters right
{
const fi = new FakeInput();
const rli = new readline.Interface({
input: fi,
output: fi,
prompt: '',
terminal: terminal
});
fi.emit('data', '💻');

// move left one character/code point
fi.emit('keypress', '.', { name: 'right' });
let cursorPos = rli._getCursorPos();
assert.strictEqual(cursorPos.rows, 0);
assert.strictEqual(cursorPos.cols, 2);

fi.emit('data', '🐕');
cursorPos = rli._getCursorPos();
assert.strictEqual(cursorPos.rows, 0);
assert.strictEqual(cursorPos.cols, 4);

rli.on('line', common.mustCall((line) => {
assert.strictEqual(line, '💻🐕');
}));
fi.emit('data', '\n');
rli.close();
}

{
// `wordLeft` and `wordRight`
const fi = new FakeInput();
Expand Down Expand Up @@ -791,6 +879,32 @@ function isWarned(emitter) {
rli.close();
}

// deleteLeft astral character
{
const fi = new FakeInput();
const rli = new readline.Interface({
input: fi,
output: fi,
prompt: '',
terminal: terminal
});
fi.emit('data', '💻');
let cursorPos = rli._getCursorPos();
assert.strictEqual(cursorPos.rows, 0);
assert.strictEqual(cursorPos.cols, 2);

// Delete left character
fi.emit('keypress', '.', { ctrl: true, name: 'h' });
cursorPos = rli._getCursorPos();
assert.strictEqual(cursorPos.rows, 0);
assert.strictEqual(cursorPos.cols, 0);
rli.on('line', common.mustCall((line) => {
assert.strictEqual(line, '');
}));
fi.emit('data', '\n');
rli.close();
}

// deleteRight
{
const fi = new FakeInput();
Expand Down Expand Up @@ -820,6 +934,34 @@ function isWarned(emitter) {
rli.close();
}

// deleteRight astral character
{
const fi = new FakeInput();
const rli = new readline.Interface({
input: fi,
output: fi,
prompt: '',
terminal: terminal
});
fi.emit('data', '💻');

// Go to the start of the line
fi.emit('keypress', '.', { ctrl: true, name: 'a' });
let cursorPos = rli._getCursorPos();
assert.strictEqual(cursorPos.rows, 0);
assert.strictEqual(cursorPos.cols, 0);

// Delete right character
fi.emit('keypress', '.', { ctrl: true, name: 'd' });
cursorPos = rli._getCursorPos();
assert.strictEqual(cursorPos.rows, 0);
assert.strictEqual(cursorPos.cols, 0);
rli.on('line', common.mustCall((line) => {
assert.strictEqual(line, '');
}));
fi.emit('data', '\n');
rli.close();
}

// deleteLineLeft
{
Expand Down