Skip to content

Commit ca760aa

Browse files
committed
fs: readline
Fix: #12606
1 parent 60f19bc commit ca760aa

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

lib/internal/readline/interface.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,15 @@ const kOnLine = Symbol('_onLine');
125125
const kSetLine = Symbol('_setLine');
126126
const kPreviousKey = Symbol('_previousKey');
127127
const kPrompt = Symbol('_prompt');
128+
const kPromptInvoked = Symbol('_promptInvoked');
128129
const kPushToKillRing = Symbol('_pushToKillRing');
129130
const kPushToUndoStack = Symbol('_pushToUndoStack');
130131
const kQuestionCallback = Symbol('_questionCallback');
131132
const kLastCommandErrored = Symbol('_lastCommandErrored');
132133
const kQuestionReject = Symbol('_questionReject');
133134
const kRedo = Symbol('_redo');
134135
const kRedoStack = Symbol('_redoStack');
136+
const kEffectivePrompt = Symbol('_effectivePrompt');
135137
const kRefreshLine = Symbol('_refreshLine');
136138
const kSawKeyPress = Symbol('_sawKeyPress');
137139
const kSawReturnAt = Symbol('_sawReturnAt');
@@ -252,6 +254,7 @@ function InterfaceConstructor(input, output, completer, terminal) {
252254
this.completer = completer;
253255

254256
this.setPrompt(prompt);
257+
this[kPromptInvoked] = false;
255258

256259
this.terminal = !!terminal;
257260

@@ -428,6 +431,7 @@ class Interface extends InterfaceConstructor {
428431
*/
429432
prompt(preserveCursor) {
430433
if (this.paused) this.resume();
434+
this[kPromptInvoked] = true;
431435
if (this.terminal && process.env.TERM !== 'dumb') {
432436
if (!preserveCursor) this.cursor = 0;
433437
this[kRefreshLine]();
@@ -490,9 +494,14 @@ class Interface extends InterfaceConstructor {
490494
return this.historyManager.addHistory(this[kIsMultiline], this[kLastCommandErrored]);
491495
}
492496

497+
[kEffectivePrompt]() {
498+
return this[kPromptInvoked] ? this[kPrompt] : '';
499+
}
500+
493501
[kRefreshLine]() {
494502
// line length
495-
const line = this[kPrompt] + this.line;
503+
const promptPrefix = this[kEffectivePrompt]();
504+
const line = promptPrefix + this.line;
496505
const dispPos = this[kGetDisplayPos](line);
497506
const lineCols = dispPos.cols;
498507
const lineRows = dispPos.rows;
@@ -514,7 +523,7 @@ class Interface extends InterfaceConstructor {
514523
if (this[kIsMultiline]) {
515524
const lines = StringPrototypeSplit(this.line, '\n');
516525
// Write first line with normal prompt
517-
this[kWriteToOutput](this[kPrompt] + lines[0]);
526+
this[kWriteToOutput](promptPrefix + lines[0]);
518527

519528
// For continuation lines, add the "|" prefix
520529
for (let i = 1; i < lines.length; i++) {
@@ -1020,7 +1029,9 @@ class Interface extends InterfaceConstructor {
10201029
}
10211030

10221031
if (needsRewriteFirstLine) {
1023-
this[kWriteToOutput](`${this[kPrompt]}${beforeCursor}\n${kMultilinePrompt.description}`);
1032+
this[kWriteToOutput](
1033+
`${this[kEffectivePrompt]()}${beforeCursor}\n${kMultilinePrompt.description}`,
1034+
);
10241035
} else {
10251036
this[kWriteToOutput](kMultilinePrompt.description);
10261037
}
@@ -1221,7 +1232,8 @@ class Interface extends InterfaceConstructor {
12211232
* }}
12221233
*/
12231234
getCursorPos() {
1224-
const strBeforeCursor = this[kPrompt] + StringPrototypeSlice(this.line, 0, this.cursor);
1235+
const strBeforeCursor = this[kEffectivePrompt]() +
1236+
StringPrototypeSlice(this.line, 0, this.cursor);
12251237

12261238
return this[kGetDisplayPos](strBeforeCursor);
12271239
}

0 commit comments

Comments
 (0)