Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

readline: allow passing prompt to constructor #7125

Merged
merged 1 commit into from
Jun 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions doc/api/readline.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ added: v0.1.98
the history set this value to `0`. Defaults to `30`. This option makes sense
only if `terminal` is set to `true` by the user or by an internal `output`
check, otherwise the history caching mechanism is not initialized at all.
* `prompt` - the prompt string to use. Default: `'> '`

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

```js
const readline = require('readline');
const rl = readline.createInterface(process.stdin, process.stdout);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: 'OHAI> '
});

rl.setPrompt('OHAI> ');
rl.prompt();

rl.on('line', (line) => {
Expand Down
6 changes: 5 additions & 1 deletion lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,17 @@ function Interface(input, output, completer, terminal) {

EventEmitter.call(this);
var historySize;
let prompt = '> ';

if (arguments.length === 1) {
// an options object was given
output = input.output;
completer = input.completer;
terminal = input.terminal;
historySize = input.historySize;
if (input.prompt !== undefined) {
prompt = input.prompt;
}
input = input.input;
}

Expand Down Expand Up @@ -87,7 +91,7 @@ function Interface(input, output, completer, terminal) {
};
}

this.setPrompt('> ');
this.setPrompt(prompt);

this.terminal = !!terminal;

Expand Down
7 changes: 2 additions & 5 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,11 +385,10 @@ function REPLServer(prompt,
output: self.outputStream,
completer: complete,
terminal: options.terminal,
historySize: options.historySize
historySize: options.historySize,
prompt
});

self.setPrompt(prompt !== undefined ? prompt : '> ');

this.commands = Object.create(null);
defineDefaultCommands(this);

Expand All @@ -408,8 +407,6 @@ function REPLServer(prompt,
};
}

self.setPrompt(self._prompt);

self.on('close', function() {
self.emit('exit');
});
Expand Down
29 changes: 28 additions & 1 deletion test/parallel/test-readline-interface.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// Flags: --expose_internals
'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');
const readline = require('readline');
const internalReadline = require('internal/readline');
const EventEmitter = require('events').EventEmitter;
const inherits = require('util').inherits;
const Writable = require('stream').Writable;
const Readable = require('stream').Readable;

function FakeInput() {
EventEmitter.call(this);
Expand Down Expand Up @@ -396,4 +398,29 @@ function isWarned(emitter) {
});
});

{
const expected = terminal
? ['\u001b[1G', '\u001b[0J', '$ ', '\u001b[3G']
: ['$ '];

let counter = 0;
const output = new Writable({
write: common.mustCall((chunk, enc, cb) => {
assert.strictEqual(chunk.toString(), expected[counter++]);
cb();
rl.close();
}, expected.length)
});

const rl = readline.createInterface({
input: new Readable({ read: () => {} }),
output: output,
prompt: '$ ',
terminal: terminal
});

rl.prompt();

assert.strictEqual(rl._prompt, '$ ');
}
});