diff --git a/lib/internal/readline/interface.js b/lib/internal/readline/interface.js index 08f7aaa9e3e7e8..1b98d1550109f6 100644 --- a/lib/internal/readline/interface.js +++ b/lib/internal/readline/interface.js @@ -125,6 +125,7 @@ const kOnLine = Symbol('_onLine'); const kSetLine = Symbol('_setLine'); const kPreviousKey = Symbol('_previousKey'); const kPrompt = Symbol('_prompt'); +const kPromptInvoked = Symbol('_promptInvoked'); const kPushToKillRing = Symbol('_pushToKillRing'); const kPushToUndoStack = Symbol('_pushToUndoStack'); const kQuestionCallback = Symbol('_questionCallback'); @@ -132,6 +133,7 @@ const kLastCommandErrored = Symbol('_lastCommandErrored'); const kQuestionReject = Symbol('_questionReject'); const kRedo = Symbol('_redo'); const kRedoStack = Symbol('_redoStack'); +const kEffectivePrompt = Symbol('_effectivePrompt'); const kRefreshLine = Symbol('_refreshLine'); const kSawKeyPress = Symbol('_sawKeyPress'); const kSawReturnAt = Symbol('_sawReturnAt'); @@ -252,6 +254,7 @@ function InterfaceConstructor(input, output, completer, terminal) { this.completer = completer; this.setPrompt(prompt); + this[kPromptInvoked] = false; this.terminal = !!terminal; @@ -428,6 +431,7 @@ class Interface extends InterfaceConstructor { */ prompt(preserveCursor) { if (this.paused) this.resume(); + this[kPromptInvoked] = true; if (this.terminal && process.env.TERM !== 'dumb') { if (!preserveCursor) this.cursor = 0; this[kRefreshLine](); @@ -490,9 +494,14 @@ class Interface extends InterfaceConstructor { return this.historyManager.addHistory(this[kIsMultiline], this[kLastCommandErrored]); } + [kEffectivePrompt]() { + return this[kPromptInvoked] ? this[kPrompt] : ''; + } + [kRefreshLine]() { // line length - const line = this[kPrompt] + this.line; + const promptPrefix = this[kEffectivePrompt](); + const line = promptPrefix + this.line; const dispPos = this[kGetDisplayPos](line); const lineCols = dispPos.cols; const lineRows = dispPos.rows; @@ -514,7 +523,7 @@ class Interface extends InterfaceConstructor { if (this[kIsMultiline]) { const lines = StringPrototypeSplit(this.line, '\n'); // Write first line with normal prompt - this[kWriteToOutput](this[kPrompt] + lines[0]); + this[kWriteToOutput](promptPrefix + lines[0]); // For continuation lines, add the "|" prefix for (let i = 1; i < lines.length; i++) { @@ -1020,7 +1029,9 @@ class Interface extends InterfaceConstructor { } if (needsRewriteFirstLine) { - this[kWriteToOutput](`${this[kPrompt]}${beforeCursor}\n${kMultilinePrompt.description}`); + this[kWriteToOutput]( + `${this[kEffectivePrompt]()}${beforeCursor}\n${kMultilinePrompt.description}`, + ); } else { this[kWriteToOutput](kMultilinePrompt.description); } @@ -1221,7 +1232,8 @@ class Interface extends InterfaceConstructor { * }} */ getCursorPos() { - const strBeforeCursor = this[kPrompt] + StringPrototypeSlice(this.line, 0, this.cursor); + const strBeforeCursor = this[kEffectivePrompt]() + + StringPrototypeSlice(this.line, 0, this.cursor); return this[kGetDisplayPos](strBeforeCursor); }