Skip to content

Commit 6b1fc63

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 <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Conflicts: test/parallel/test-readline-interface.js
1 parent e30f32f commit 6b1fc63

File tree

4 files changed

+45
-13
lines changed

4 files changed

+45
-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
const readline = require('readline');
470-
const rl = readline.createInterface(process.stdin, process.stdout);
471+
const rl = 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
var historySize;
48+
let prompt = '> ';
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: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
'use strict';
2-
require('../common');
3-
var assert = require('assert');
4-
var readline = require('readline');
5-
var EventEmitter = require('events').EventEmitter;
6-
var inherits = require('util').inherits;
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const readline = require('readline');
5+
const EventEmitter = require('events').EventEmitter;
6+
const inherits = require('util').inherits;
7+
const Writable = require('stream').Writable;
8+
const Readable = require('stream').Readable;
79

810
function FakeInput() {
911
EventEmitter.call(this);
@@ -400,4 +402,29 @@ function isWarned(emitter) {
400402
});
401403
});
402404

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

0 commit comments

Comments
 (0)