Skip to content

Commit 10af9e6

Browse files
committed
fixup! simplify implementation
1 parent eb931ad commit 10af9e6

3 files changed

Lines changed: 33 additions & 51 deletions

File tree

doc/api/repl.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,8 +605,8 @@ The `replServer.displayPrompt()` method readies the REPL instance for input
605605
from the user, printing the configured `prompt` to a new line in the `output`
606606
and resuming the `input` to accept new input.
607607

608-
When multi-line input is being entered, the "|" character is printed rather
609-
than the 'prompt'.
608+
When multi-line input is being entered, a pipe `'|'` is printed rather than the
609+
'prompt'.
610610

611611
When `preserveCursor` is `true`, the cursor placement will not be reset to `0`.
612612

lib/internal/readline/interface.js

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,47 +1080,33 @@ class Interface extends InterfaceConstructor {
10801080
let rows = 0;
10811081
str = stripVTControlCharacters(str);
10821082

1083-
// Split the string into lines for better handling of multiline scenarios
1084-
const lines = StringPrototypeSplit(str, '\n');
1085-
1086-
for (let i = 0; i < lines.length; i++) {
1087-
const line = lines[i];
1088-
1089-
// Only add prefix offset for continuation lines in user input (not prompts)
1090-
if (i > 0 && this[kIsMultiline]) {
1091-
offset += kMultilinePrompt.description.length;
1083+
for (const char of new SafeStringIterator(str)) {
1084+
if (char === '\n') {
1085+
// Rows must be incremented by 1 even if offset = 0 or col = +Infinity.
1086+
rows += MathCeil(offset / col) || 1;
1087+
// Only add prefix offset for continuation lines in user input (not prompts)
1088+
offset = this[kIsMultiline] ? kMultilinePrompt.description.length : 0;
1089+
continue;
10921090
}
1093-
1094-
// Process each character in the line
1095-
for (const char of new SafeStringIterator(line)) {
1096-
// Tabs must be aligned by an offset of the tab size
1097-
if (char === '\t') {
1098-
offset += this.tabSize - (offset % this.tabSize);
1099-
continue;
1100-
}
1101-
1102-
const width = getStringWidth(char, false /* stripVTControlCharacters */);
1103-
if (width === 0 || width === 1) {
1104-
offset += width;
1105-
} else {
1106-
// width === 2
1107-
if ((offset + 1) % col === 0) {
1108-
offset++;
1109-
}
1110-
offset += 2;
1111-
}
1091+
// Tabs must be aligned by an offset of the tab size.
1092+
if (char === '\t') {
1093+
offset += this.tabSize - (offset % this.tabSize);
1094+
continue;
11121095
}
1113-
1114-
// Add rows for completed lines
1115-
if (i < lines.length - 1) {
1116-
// For all lines except the last one
1117-
rows += MathCeil(offset / col) || 1;
1118-
offset = 0;
1096+
const width = getStringWidth(char, false /* stripVTControlCharacters */);
1097+
if (width === 0 || width === 1) {
1098+
offset += width;
1099+
} else {
1100+
// width === 2
1101+
if ((offset + 1) % col === 0) {
1102+
offset++;
1103+
}
1104+
offset += 2;
11191105
}
11201106
}
11211107

11221108
const cols = offset % col;
1123-
rows += MathFloor(offset / col);
1109+
rows += (offset - cols) / col;
11241110

11251111
return { cols, rows };
11261112
}
@@ -1498,6 +1484,7 @@ module.exports = {
14981484
kHistoryNext,
14991485
kHistoryPrev,
15001486
kInsertString,
1487+
kIsMultiline,
15011488
kLine,
15021489
kLine_buffer,
15031490
kMoveCursor,

lib/internal/repl/utils.js

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ const {
2828
ERR_INSPECTOR_NOT_AVAILABLE,
2929
} = require('internal/errors').codes;
3030

31+
const {
32+
kIsMultiline,
33+
} = require('internal/readline/interface');
34+
3135
const {
3236
clearLine,
3337
clearScreenDown,
@@ -159,8 +163,6 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
159163

160164
let escaped = null;
161165

162-
let justExecutedMultilineCommand = false;
163-
164166
function getPreviewPos() {
165167
const displayPos = repl._getDisplayPos(`${repl.getPrompt()}${repl.line}`);
166168
const cursorPos = repl.line.length !== repl.cursor ?
@@ -183,11 +185,6 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
183185
clearLine(repl.output);
184186
moveCursor(repl.output, 0, -rows);
185187
inputPreview = null;
186-
// If pressing enter on some history line,
187-
// The next preview should not be generated
188-
if ((key.name === 'return' || key.name === 'enter') && !key.meta && repl.historyIndex !== -1) {
189-
justExecutedMultilineCommand = true;
190-
}
191188
}
192189
if (completionPreview !== null) {
193190
// Prevent cursor moves if not necessary!
@@ -372,8 +369,11 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
372369
}
373370

374371
const showPreview = (showCompletion = true) => {
375-
// Prevent duplicated previews after a refresh.
376-
if (inputPreview !== null || !repl.isCompletionEnabled || !process.features.inspector) {
372+
// Prevent duplicated previews after a refresh or in a multiline command.
373+
if (inputPreview !== null ||
374+
repl[kIsMultiline] ||
375+
!repl.isCompletionEnabled ||
376+
!process.features.inspector) {
377377
return;
378378
}
379379

@@ -384,11 +384,6 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
384384
return;
385385
}
386386

387-
if (justExecutedMultilineCommand) {
388-
justExecutedMultilineCommand = false;
389-
return;
390-
}
391-
392387
hasCompletions = false;
393388

394389
// Add the autocompletion preview.

0 commit comments

Comments
 (0)