Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions lib/internal/readline/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,15 @@ 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');
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');
Expand Down Expand Up @@ -252,6 +254,7 @@ function InterfaceConstructor(input, output, completer, terminal) {
this.completer = completer;

this.setPrompt(prompt);
this[kPromptInvoked] = false;

this.terminal = !!terminal;

Expand Down Expand Up @@ -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]();
Expand Down Expand Up @@ -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;
Expand All @@ -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++) {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down
Loading