Skip to content

Commit f315b21

Browse files
mattiasrungecjihrig
andcommitted
readline: add getPrompt to get the current prompt
Since there is a setPrompt() there should be a getPrompt(). There are use-cases where it is needed to know what the current prompt is. Adding a getPrompt() negates the need to store the set prompt externally or read the internal _prompt which would be bad practice. Co-authored-by: Colin Ihrig <cjihrig@gmail.com>
1 parent 9fd6122 commit f315b21

File tree

5 files changed

+32
-8
lines changed

5 files changed

+32
-8
lines changed

doc/api/readline.md

+9
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,15 @@ added: v0.1.98
281281
The `rl.setPrompt()` method sets the prompt that will be written to `output`
282282
whenever `rl.prompt()` is called.
283283

284+
### `rl.getPrompt()`
285+
<!-- YAML
286+
added: REPLACEME
287+
-->
288+
289+
* Returns: {string} the current prompt string
290+
291+
The `rl.getPrompt()` method returns the current prompt used by `rl.prompt()`.
292+
284293
### `rl.write(data[, key])`
285294
<!-- YAML
286295
added: v0.1.98

lib/internal/repl/utils.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
147147
let escaped = null;
148148

149149
function getPreviewPos() {
150-
const displayPos = repl._getDisplayPos(`${repl._prompt}${repl.line}`);
150+
const displayPos = repl._getDisplayPos(`${repl.getPrompt()}${repl.line}`);
151151
const cursorPos = repl.line.length !== repl.cursor ?
152152
repl.getCursorPos() :
153153
displayPos;
@@ -180,7 +180,7 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
180180
rows = pos.displayPos.rows - pos.cursorPos.rows;
181181
moveCursor(repl.output, 0, rows);
182182
}
183-
const totalLine = `${repl._prompt}${repl.line}${completionPreview}`;
183+
const totalLine = `${repl.getPrompt()}${repl.line}${completionPreview}`;
184184
const newPos = repl._getDisplayPos(totalLine);
185185
// Minimize work for the terminal. It is enough to clear the right part of
186186
// the current line in case the preview is visible on a single line.
@@ -266,7 +266,7 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
266266
}
267267
repl.output.write(result);
268268
cursorTo(repl.output, cursorPos.cols);
269-
const totalLine = `${repl._prompt}${repl.line}${suffix}`;
269+
const totalLine = `${repl.getPrompt()}${repl.line}${suffix}`;
270270
const newPos = repl._getDisplayPos(totalLine);
271271
const rows = newPos.rows - cursorPos.rows - (newPos.cols === 0 ? 1 : 0);
272272
moveCursor(repl.output, 0, -rows);
@@ -614,7 +614,7 @@ function setupReverseSearch(repl) {
614614
let rows = 0;
615615
if (lastMatch !== -1) {
616616
const line = repl.history[lastMatch].slice(0, lastCursor);
617-
rows = repl._getDisplayPos(`${repl._prompt}${line}`).rows;
617+
rows = repl._getDisplayPos(`${repl.getPrompt()}${line}`).rows;
618618
cursorTo(repl.output, promptPos.cols);
619619
} else if (isInReverseSearch && repl.line !== '') {
620620
rows = repl.getCursorPos().rows;
@@ -634,7 +634,7 @@ function setupReverseSearch(repl) {
634634

635635
// To know exactly how many rows we have to move the cursor back we need the
636636
// cursor rows, the output rows and the input rows.
637-
const prompt = repl._prompt;
637+
const prompt = repl.getPrompt();
638638
const cursorLine = `${prompt}${outputLine.slice(0, cursor)}`;
639639
const cursorPos = repl._getDisplayPos(cursorLine);
640640
const outputPos = repl._getDisplayPos(`${prompt}${outputLine}`);
@@ -685,7 +685,7 @@ function setupReverseSearch(repl) {
685685
if (!isInReverseSearch) {
686686
if (key.ctrl && checkAndSetDirectionKey(key.name)) {
687687
historyIndex = repl.historyIndex;
688-
promptPos = repl._getDisplayPos(`${repl._prompt}`);
688+
promptPos = repl._getDisplayPos(`${repl.getPrompt()}`);
689689
print(repl.line, `${labels[dir]}_`);
690690
isInReverseSearch = true;
691691
}

lib/readline.js

+5
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,11 @@ Interface.prototype.setPrompt = function(prompt) {
291291
};
292292

293293

294+
Interface.prototype.getPrompt = function() {
295+
return this._prompt;
296+
};
297+
298+
294299
Interface.prototype._setRawMode = function(mode) {
295300
const wasInRawMode = this.input.isRaw;
296301

test/parallel/test-readline-interface.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,16 @@ for (let i = 0; i < 12; i++) {
898898
});
899899
}
900900

901+
// Calling the getPrompt method
902+
{
903+
const expectedPrompts = ['$ ', '> '];
904+
const [rli] = getInterface({ terminal });
905+
for (const prompt of expectedPrompts) {
906+
rli.setPrompt(prompt);
907+
assert.strictEqual(rli.getPrompt(), prompt);
908+
}
909+
}
910+
901911
{
902912
const expected = terminal ?
903913
['\u001b[1G', '\u001b[0J', '$ ', '\u001b[3G'] :
@@ -920,7 +930,7 @@ for (let i = 0; i < 12; i++) {
920930

921931
rl.prompt();
922932

923-
assert.strictEqual(rl._prompt, '$ ');
933+
assert.strictEqual(rl.getPrompt(), '$ ');
924934
}
925935

926936
{

test/parallel/test-repl-options.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ assert.throws(r3, {
104104
// 4, Verify that defaults are used when no arguments are provided
105105
const r4 = repl.start();
106106

107-
assert.strictEqual(r4._prompt, '> ');
107+
assert.strictEqual(r4.getPrompt(), '> ');
108108
assert.strictEqual(r4.input, process.stdin);
109109
assert.strictEqual(r4.output, process.stdout);
110110
assert.strictEqual(r4.terminal, !!r4.output.isTTY);

0 commit comments

Comments
 (0)