Skip to content

Commit c521249

Browse files
evanlucasFishrock123
authored andcommitted
readline: allow passing prompt to constructor
Previously, one would have to call setPrompt after calling rl.createInterface. Now, the prompt string can be set by passing the prompt property. PR-URL: #7125 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]>
1 parent f4a067b commit c521249

File tree

4 files changed

+46
-13
lines changed

4 files changed

+46
-13
lines changed

‎doc/api/readline.md‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ added: v0.1.98
357357
the history set this value to `0`. Defaults to `30`. This option makes sense
358358
only if `terminal` is set to `true` by the user or by an internal `output`
359359
check, otherwise the history caching mechanism is not initialized at all.
360+
*`prompt` - the prompt string to use. Default: `'> '`
360361

361362
The `readline.createInterface()` method creates a new `readline.Interface`
362363
instance.
@@ -467,9 +468,12 @@ implement a small command-line interface:
467468

468469
```js
469470
constreadline=require('readline');
470-
constrl=readline.createInterface(process.stdin, process.stdout);
471+
constrl=readline.createInterface({
472+
input:process.stdin,
473+
output:process.stdout,
474+
prompt:'OHAI> '
475+
});
471476

472-
rl.setPrompt('OHAI> ');
473477
rl.prompt();
474478

475479
rl.on('line', (line) =>{

‎lib/readline.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,17 @@ function Interface(input, output, completer, terminal){
4545

4646
EventEmitter.call(this);
4747
varhistorySize;
48+
letprompt='> ';
4849

4950
if(arguments.length===1){
5051
// an options object was given
5152
output=input.output;
5253
completer=input.completer;
5354
terminal=input.terminal;
5455
historySize=input.historySize;
56+
if(input.prompt!==undefined){
57+
prompt=input.prompt;
58+
}
5559
input=input.input;
5660
}
5761

@@ -88,7 +92,7 @@ function Interface(input, output, completer, terminal){
8892
};
8993
}
9094

91-
this.setPrompt('> ');
95+
this.setPrompt(prompt);
9296

9397
this.terminal=!!terminal;
9498

‎lib/repl.js‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -385,11 +385,10 @@ function REPLServer(prompt,
385385
output: self.outputStream,
386386
completer: complete,
387387
terminal: options.terminal,
388-
historySize: options.historySize
388+
historySize: options.historySize,
389+
prompt
389390
});
390391

391-
self.setPrompt(prompt!==undefined ? prompt : '> ');
392-
393392
this.commands=Object.create(null);
394393
defineDefaultCommands(this);
395394

@@ -408,8 +407,6 @@ function REPLServer(prompt,
408407
};
409408
}
410409

411-
self.setPrompt(self._prompt);
412-
413410
self.on('close',function(){
414411
self.emit('exit');
415412
});

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

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
'use strict';
2-
require('../common');
3-
varassert=require('assert');
4-
varreadline=require('readline');
5-
varEventEmitter=require('events').EventEmitter;
6-
varinherits=require('util').inherits;
2+
constcommon=require('../common');
3+
constassert=require('assert');
4+
constreadline=require('readline');
5+
constinternalReadline=require('internal/readline');
6+
constEventEmitter=require('events').EventEmitter;
7+
constinherits=require('util').inherits;
8+
constWritable=require('stream').Writable;
9+
constReadable=require('stream').Readable;
710

811
functionFakeInput(){
912
EventEmitter.call(this);
@@ -400,4 +403,29 @@ function isWarned(emitter){
400403
});
401404
});
402405

406+
{
407+
constexpected=terminal
408+
? ['\u001b[1G','\u001b[0J','$ ','\u001b[3G']
409+
: ['$ '];
410+
411+
letcounter=0;
412+
constoutput=newWritable({
413+
write: common.mustCall((chunk,enc,cb)=>{
414+
assert.strictEqual(chunk.toString(),expected[counter++]);
415+
cb();
416+
rl.close();
417+
},expected.length)
418+
});
419+
420+
constrl=readline.createInterface({
421+
input: newReadable({read: ()=>{}}),
422+
output: output,
423+
prompt: '$ ',
424+
terminal: terminal
425+
});
426+
427+
rl.prompt();
428+
429+
assert.strictEqual(rl._prompt,'$ ');
430+
}
403431
});

0 commit comments

Comments
(0)