Skip to content

Commit

Permalink
readline: small refactoring
Browse files Browse the repository at this point in the history
This just removes some redundant work and some other small things.

PR-URL: #31006
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
BridgeAR authored and targos committed Apr 28, 2020
1 parent 936c85c commit 3946cad
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions lib/readline.js
Expand Up @@ -720,16 +720,16 @@ Interface.prototype._historyPrev = function() {
Interface.prototype._getDisplayPos = function(str) {
let offset = 0;
const col = this.columns;
let row = 0;
let rows = 0;
str = stripVTControlCharacters(str);
for (let i = 0, len = str.length; i < len; i++) {
const code = str.codePointAt(i);
if (code >= kUTF16SurrogateThreshold) { // Surrogates.
i++;
}
if (code === 0x0a) { // new line \n
// row must be incremented by 1 even if offset = 0 or col = +Infinity
row += MathCeil(offset / col) || 1;
// rows must be incremented by 1 even if offset = 0 or col = +Infinity
rows += MathCeil(offset / col) || 1;
offset = 0;
continue;
}
Expand All @@ -744,17 +744,16 @@ Interface.prototype._getDisplayPos = function(str) {
}
}
const cols = offset % col;
const rows = row + (offset - cols) / col;
return { cols: cols, rows: rows };
rows += (offset - cols) / col;
return { cols, rows };
};


// Returns current cursor's position and line
Interface.prototype.getCursorPos = function() {
const columns = this.columns;
const strBeforeCursor = this._prompt + this.line.substring(0, this.cursor);
const dispPos = this._getDisplayPos(
stripVTControlCharacters(strBeforeCursor));
const dispPos = this._getDisplayPos(strBeforeCursor);
let cols = dispPos.cols;
let rows = dispPos.rows;
// If the cursor is on a full-width character which steps over the line,
Expand All @@ -765,7 +764,7 @@ Interface.prototype.getCursorPos = function() {
rows++;
cols = 0;
}
return { cols: cols, rows: rows };
return { cols, rows };
};
Interface.prototype._getCursorPos = Interface.prototype.getCursorPos;

Expand Down

0 comments on commit 3946cad

Please sign in to comment.