Skip to content
Permalink
Browse files

readline: small refactoring

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 committed Dec 17, 2019
1 parent 09ca8be commit 92dcf3e4aeceffa419fd0d67d02331ae50fd9916
Showing with 7 additions and 8 deletions.
  1. +7 −8 lib/readline.js
@@ -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;
}
@@ -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,
@@ -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;

0 comments on commit 92dcf3e

Please sign in to comment.
You can’t perform that action at this time.