Skip to content

Commit 494e89a

Browse files
repl: support customizable multiline subprompt
Add a option to the REPL to allow custom prompts for multiline input, replacing the default
1 parent dc03ff2 commit 494e89a

File tree

4 files changed

+23
-29
lines changed

4 files changed

+23
-29
lines changed

lib/internal/readline/interface.js

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ function InterfaceConstructor(input, output, completer, terminal) {
172172
let crlfDelay;
173173
let prompt = '> ';
174174
let signal;
175+
const userMultilinePrompt = input?.multilinePrompt ?? '| ';
175176

176177
if (input?.input) {
177178
// An options object was given
@@ -228,7 +229,6 @@ function InterfaceConstructor(input, output, completer, terminal) {
228229
}
229230

230231
const self = this;
231-
232232
this.line = '';
233233
this[kIsMultiline] = false;
234234
this[kSubstringSearch] = null;
@@ -237,7 +237,9 @@ function InterfaceConstructor(input, output, completer, terminal) {
237237
this[kUndoStack] = [];
238238
this[kRedoStack] = [];
239239
this[kPreviousCursorCols] = -1;
240-
this[kMultilinePrompt] ||= { description: '| ' };
240+
this[kMultilinePrompt] = {
241+
description: userMultilinePrompt,
242+
};
241243

242244
// The kill ring is a global list of blocks of text that were previously
243245
// killed (deleted). If its size exceeds kMaxLengthOfKillRing, the oldest
@@ -412,23 +414,6 @@ class Interface extends InterfaceConstructor {
412414
});
413415
}
414416

415-
/**
416-
* Sets the multiline prompt.
417-
* @param {string} prompt
418-
* @returns {void}
419-
*/
420-
setMultilinePrompt(prompt) {
421-
this[kMultilinePrompt].description = prompt;
422-
}
423-
424-
/**
425-
* Returns the current multiline prompt.
426-
* @returns {string}
427-
*/
428-
getMultilinePrompt() {
429-
return this[kMultilinePrompt].description;
430-
}
431-
432417
[kSetRawMode](mode) {
433418
const wasInRawMode = this.input.isRaw;
434419

lib/internal/repl.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ function createRepl(env, opts, cb) {
2222
ignoreUndefined: false,
2323
useGlobal: true,
2424
breakEvalOnSigint: true,
25-
multilinePrompt: opts?.multilinePrompt ?? '| ',
2625
...opts,
2726
};
2827

lib/repl.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -800,10 +800,22 @@ class REPLServer extends Interface {
800800

801801
self.clearBufferedCommand();
802802

803-
function completer(text, cb) {
804-
ReflectApply(complete, self,
805-
[text, self.editorMode ? self.completeOnEditorMode(cb) : cb]);
806-
}
803+
function completer(text, cb) {
804+
ReflectApply(complete, self,
805+
[text, self.editorMode ? self.completeOnEditorMode(cb) : cb]);
806+
}
807+
808+
// All the parameters in the object are defining the "input" param of the
809+
// InterfaceConstructor.
810+
ReflectApply(Interface, this, [{
811+
input: options.input,
812+
output: options.output,
813+
completer: options.completer || completer,
814+
terminal: options.terminal,
815+
historySize: options.historySize,
816+
prompt,
817+
multilinePrompt: options.multilinePrompt,
818+
}]);
807819

808820
self.resetContext();
809821

@@ -1192,7 +1204,7 @@ class REPLServer extends Interface {
11921204
displayPrompt(preserveCursor) {
11931205
let prompt = this._initialPrompt;
11941206
if (this[kBufferedCommandSymbol].length) {
1195-
this[kMultilinePrompt].description = '| ';
1207+
// this[kMultilinePrompt].description = '| ';
11961208
prompt = this[kMultilinePrompt].description;
11971209
}
11981210

test/parallel/test-repl-multiline-prompt.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@ function runPromptTest(promptStr, { useColors }) {
2222
input: inputStream,
2323
output: outputStream,
2424
terminal: true,
25-
useColors
25+
useColors,
26+
multilinePrompt: promptStr,
2627
});
2728

28-
// Set the custom multiline prompt
29-
r.setMultilinePrompt(promptStr);
30-
3129
r.on('exit', common.mustCall(() => {
3230
const lines = output.split('\n');
3331

0 commit comments

Comments
 (0)