Skip to content

Commit d1baae3

Browse 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

5 files changed

Lines changed: 32 additions & 8 deletions

File tree

‎doc/api/readline.md‎

Lines changed: 9 additions & 0 deletions
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

‎lib/internal/repl/utils.js‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
144144
letescaped=null;
145145

146146
functiongetPreviewPos(){
147-
constdisplayPos=repl._getDisplayPos(`${repl._prompt}${repl.line}`);
147+
constdisplayPos=repl._getDisplayPos(`${repl.getPrompt()}${repl.line}`);
148148
constcursorPos=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-
consttotalLine=`${repl._prompt}${repl.line}${completionPreview}`;
180+
consttotalLine=`${repl.getPrompt()}${repl.line}${completionPreview}`;
181181
constnewPos=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-
consttotalLine=`${repl._prompt}${repl.line}${suffix}`;
266+
consttotalLine=`${repl.getPrompt()}${repl.line}${suffix}`;
267267
constnewPos=repl._getDisplayPos(totalLine);
268268
constrows=newPos.rows-cursorPos.rows-(newPos.cols===0 ? 1 : 0);
269269
moveCursor(repl.output,0,-rows);
@@ -611,7 +611,7 @@ function setupReverseSearch(repl) {
611611
letrows=0;
612612
if(lastMatch!==-1){
613613
constline=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
}elseif(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-
constprompt=repl._prompt;
634+
constprompt=repl.getPrompt();
635635
constcursorLine=`${prompt}${outputLine.slice(0,cursor)}`;
636636
constcursorPos=repl._getDisplayPos(cursorLine);
637637
constoutputPos=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
}

‎lib/readline.js‎

Lines 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+
returnthis._prompt;
296+
};
297+
298+
294299
Interface.prototype._setRawMode=function(mode){
295300
constwasInRawMode=this.input.isRaw;
296301

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

Lines 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+
constexpectedPrompts=['$ ','> '];
904+
const[rli]=getInterface({ terminal });
905+
for(constpromptofexpectedPrompts){
906+
rli.setPrompt(prompt);
907+
assert.strictEqual(rli.getPrompt(),prompt);
908+
}
909+
}
910+
901911
{
902912
constexpected=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‎

Lines 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
constr4=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)