Skip to content

Commit 3f5623d

Browse files
committed
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>
1 parent 1d75987 commit 3f5623d

4 files changed

Lines changed: 41 additions & 9 deletions

File tree

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
@@ -44,13 +44,17 @@ function Interface(input, output, completer, terminal) {
4444

4545
EventEmitter.call(this);
4646
var historySize;
47+
let prompt = '> ';
4748

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

@@ -87,7 +91,7 @@ function Interface(input, output, completer, terminal) {
8791
};
8892
}
8993

90-
this.setPrompt('> ');
94+
this.setPrompt(prompt);
9195

9296
this.terminal = !!terminal;
9397

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: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
// Flags: --expose_internals
22
'use strict';
3-
require('../common');
3+
const common = require('../common');
44
const assert = require('assert');
55
const readline = require('readline');
66
const internalReadline = require('internal/readline');
77
const EventEmitter = require('events').EventEmitter;
88
const inherits = require('util').inherits;
9+
const Writable = require('stream').Writable;
10+
const Readable = require('stream').Readable;
911

1012
function FakeInput() {
1113
EventEmitter.call(this);
@@ -396,4 +398,29 @@ function isWarned(emitter) {
396398
});
397399
});
398400

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

0 commit comments

Comments
 (0)