summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEvan Lucas <evanlucas@me.com>2016-06-02 20:31:23 -0500
committerEvan Lucas <evanlucas@me.com>2016-06-25 12:10:28 -0500
commit3f5623dd462398d7af0814eadf5f8f43a610330e (patch)
treec2201c52b1fbb171fc551c779af19001dfcc052f
parent1d75987ff0780624dcceae42fdfe182a133cb2d7 (diff)
downloadandroid-node-v8-3f5623dd462398d7af0814eadf5f8f43a610330e.tar.gz
android-node-v8-3f5623dd462398d7af0814eadf5f8f43a610330e.tar.bz2
android-node-v8-3f5623dd462398d7af0814eadf5f8f43a610330e.zip
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: https://github.com/nodejs/node/pull/7125 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
-rw-r--r--doc/api/readline.md8
-rw-r--r--lib/readline.js6
-rw-r--r--lib/repl.js7
-rw-r--r--test/parallel/test-readline-interface.js29
4 files changed, 41 insertions, 9 deletions
diff --git a/doc/api/readline.md b/doc/api/readline.md
index 448b109e4d..3855409083 100644
--- a/doc/api/readline.md
+++ b/doc/api/readline.md
@@ -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.
@@ -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) => {
diff --git a/lib/readline.js b/lib/readline.js
index 957bb81758..402923e925 100644
--- a/lib/readline.js
+++ b/lib/readline.js
@@ -44,6 +44,7 @@ function Interface(input, output, completer, terminal) {
EventEmitter.call(this);
var historySize;
+ let prompt = '> ';
if (arguments.length === 1) {
// an options object was given
@@ -51,6 +52,9 @@ function Interface(input, output, completer, terminal) {
completer = input.completer;
terminal = input.terminal;
historySize = input.historySize;
+ if (input.prompt !== undefined) {
+ prompt = input.prompt;
+ }
input = input.input;
}
@@ -87,7 +91,7 @@ function Interface(input, output, completer, terminal) {
};
}
- this.setPrompt('> ');
+ this.setPrompt(prompt);
this.terminal = !!terminal;
diff --git a/lib/repl.js b/lib/repl.js
index db5754ec04..01a595984d 100644
--- a/lib/repl.js
+++ b/lib/repl.js
@@ -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);
@@ -408,8 +407,6 @@ function REPLServer(prompt,
};
}
- self.setPrompt(self._prompt);
-
self.on('close', function() {
self.emit('exit');
});
diff --git a/test/parallel/test-readline-interface.js b/test/parallel/test-readline-interface.js
index 57e8152d14..c8cb4c9674 100644
--- a/test/parallel/test-readline-interface.js
+++ b/test/parallel/test-readline-interface.js
@@ -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);
@@ -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, '$ ');
+ }
});