Skip to content

Commit 686a3bc

Browse files
BridgeARMylesBorins
authored andcommitted
readline,repl: support tabs properly
PR-URL: #31112 Fixes: #25272 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent e3491d7 commit 686a3bc

File tree

2 files changed

+21
-24
lines changed

2 files changed

+21
-24
lines changed

lib/readline.js

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -490,9 +490,6 @@ Interface.prototype._insertString = function(c) {
490490
} else {
491491
this._writeToOutput(c);
492492
}
493-
494-
// A hack to get the line refreshed if it's needed
495-
this._moveCursor(0);
496493
}
497494
};
498495

@@ -731,6 +728,12 @@ Interface.prototype._getDisplayPos = function(str) {
731728
offset = 0;
732729
continue;
733730
}
731+
// Tabs must be aligned by an offset of 8.
732+
// TODO(BridgeAR): Make the tab size configurable.
733+
if (char === '\t') {
734+
offset += 8 - (offset % 8);
735+
continue;
736+
}
734737
const width = getStringWidth(char);
735738
if (width === 0 || width === 1) {
736739
offset += width;
@@ -768,33 +771,27 @@ Interface.prototype._getCursorPos = Interface.prototype.getCursorPos;
768771

769772

770773
// This function moves cursor dx places to the right
771-
// (-dx for left) and refreshes the line if it is needed
774+
// (-dx for left) and refreshes the line if it is needed.
772775
Interface.prototype._moveCursor = function(dx) {
773-
const oldcursor = this.cursor;
776+
if (dx === 0) {
777+
return;
778+
}
774779
const oldPos = this.getCursorPos();
775780
this.cursor += dx;
776781

777-
// bounds check
778-
if (this.cursor < 0) this.cursor = 0;
779-
else if (this.cursor > this.line.length) this.cursor = this.line.length;
782+
// Bounds check
783+
if (this.cursor < 0) {
784+
this.cursor = 0;
785+
} else if (this.cursor > this.line.length) {
786+
this.cursor = this.line.length;
787+
}
780788

781789
const newPos = this.getCursorPos();
782790

783-
// Check if cursors are in the same line
791+
// Check if cursor stayed on the line.
784792
if (oldPos.rows === newPos.rows) {
785-
const diffCursor = this.cursor - oldcursor;
786-
let diffWidth;
787-
if (diffCursor < 0) {
788-
diffWidth = -getStringWidth(
789-
this.line.substring(this.cursor, oldcursor)
790-
);
791-
} else if (diffCursor > 0) {
792-
diffWidth = getStringWidth(
793-
this.line.substring(this.cursor, oldcursor)
794-
);
795-
}
793+
const diffWidth = newPos.cols - oldPos.cols;
796794
moveCursor(this.output, diffWidth, 0);
797-
this.prevRows = newPos.rows;
798795
} else {
799796
this._refreshLine();
800797
}

test/parallel/test-repl-preview.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ async function tests(options) {
9393
'\x1B[33mtrue\x1B[39m',
9494
'\x1B[1G\x1B[0Jrepl > \x1B[8G'],
9595
[' \t { a: true};', [2, 5], '\x1B[33mtrue\x1B[39m',
96-
' \t { a: tru\x1B[90me\x1B[39m\x1B[19G\x1B[0Ke}',
97-
'\x1B[90m{ a: true }\x1B[39m\x1B[8C\x1B[1A\x1B[1B\x1B[2K\x1B[1A;',
98-
'\x1B[90mtrue\x1B[39m\x1B[16C\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
96+
' \t { a: tru\x1B[90me\x1B[39m\x1B[26G\x1B[0Ke}',
97+
'\x1B[90m{ a: true }\x1B[39m\x1B[16C\x1B[1A\x1B[1B\x1B[2K\x1B[1A;',
98+
'\x1B[90mtrue\x1B[39m\x1B[24C\x1B[1A\x1B[1B\x1B[2K\x1B[1A\r',
9999
'\x1B[33mtrue\x1B[39m',
100100
'\x1B[1G\x1B[0Jrepl > \x1B[8G']
101101
];

0 commit comments

Comments
 (0)