Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d1baae3

Browse filesBrowse files
mattiasrungecodebytere
authored 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> PR-URL: #33675 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
1 parent baa87c1 commit d1baae3
Copy full SHA for d1baae3

File tree

Expand file treeCollapse file tree

5 files changed

+32
-8
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

5 files changed

+32
-8
lines changed
Open diff view settings
Collapse file

‎doc/api/readline.md‎

Copy file name to clipboardExpand all lines: doc/api/readline.md
+9Lines changed: 9 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,15 @@ added: v0.1.98
283283
The `rl.setPrompt()` method sets the prompt that will be written to `output`
284284
whenever `rl.prompt()` is called.
285285

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

‎lib/internal/repl/utils.js‎

Copy file name to clipboardExpand all lines: lib/internal/repl/utils.js
+6-6Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
144144
let escaped = null;
145145

146146
function getPreviewPos() {
147-
const displayPos = repl._getDisplayPos(`${repl._prompt}${repl.line}`);
147+
const displayPos = repl._getDisplayPos(`${repl.getPrompt()}${repl.line}`);
148148
const cursorPos = repl.line.length !== repl.cursor ?
149149
repl.getCursorPos() :
150150
displayPos;
@@ -177,7 +177,7 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
177177
rows = pos.displayPos.rows - pos.cursorPos.rows;
178178
moveCursor(repl.output, 0, rows);
179179
}
180-
const totalLine = `${repl._prompt}${repl.line}${completionPreview}`;
180+
const totalLine = `${repl.getPrompt()}${repl.line}${completionPreview}`;
181181
const newPos = repl._getDisplayPos(totalLine);
182182
// Minimize work for the terminal. It is enough to clear the right part of
183183
// the current line in case the preview is visible on a single line.
@@ -263,7 +263,7 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
263263
}
264264
repl.output.write(result);
265265
cursorTo(repl.output, cursorPos.cols);
266-
const totalLine = `${repl._prompt}${repl.line}${suffix}`;
266+
const totalLine = `${repl.getPrompt()}${repl.line}${suffix}`;
267267
const newPos = repl._getDisplayPos(totalLine);
268268
const rows = newPos.rows - cursorPos.rows - (newPos.cols === 0 ? 1 : 0);
269269
moveCursor(repl.output, 0, -rows);
@@ -611,7 +611,7 @@ function setupReverseSearch(repl) {
611611
let rows = 0;
612612
if (lastMatch !== -1) {
613613
const line = repl.history[lastMatch].slice(0, lastCursor);
614-
rows = repl._getDisplayPos(`${repl._prompt}${line}`).rows;
614+
rows = repl._getDisplayPos(`${repl.getPrompt()}${line}`).rows;
615615
cursorTo(repl.output, promptPos.cols);
616616
} else if (isInReverseSearch && repl.line !== '') {
617617
rows = repl.getCursorPos().rows;
@@ -631,7 +631,7 @@ function setupReverseSearch(repl) {
631631

632632
// To know exactly how many rows we have to move the cursor back we need the
633633
// cursor rows, the output rows and the input rows.
634-
const prompt = repl._prompt;
634+
const prompt = repl.getPrompt();
635635
const cursorLine = `${prompt}${outputLine.slice(0, cursor)}`;
636636
const cursorPos = repl._getDisplayPos(cursorLine);
637637
const outputPos = repl._getDisplayPos(`${prompt}${outputLine}`);
@@ -682,7 +682,7 @@ function setupReverseSearch(repl) {
682682
if (!isInReverseSearch) {
683683
if (key.ctrl && checkAndSetDirectionKey(key.name)) {
684684
historyIndex = repl.historyIndex;
685-
promptPos = repl._getDisplayPos(`${repl._prompt}`);
685+
promptPos = repl._getDisplayPos(`${repl.getPrompt()}`);
686686
print(repl.line, `${labels[dir]}_`);
687687
isInReverseSearch = true;
688688
}
Collapse file

‎lib/readline.js‎

Copy file name to clipboardExpand all lines: lib/readline.js
+5Lines changed: 5 additions & 0 deletions
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

Collapse file

‎test/parallel/test-readline-interface.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-readline-interface.js
+11-1Lines changed: 11 additions & 1 deletion
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
{
Collapse file

‎test/parallel/test-repl-options.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-repl-options.js
+1-1Lines changed: 1 addition & 1 deletion
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.