2012-02-27 20:09:34 +01:00
|
|
|
# Readline
|
2011-04-20 18:04:42 +02:00
|
|
|
|
2012-03-28 10:13:26 +02:00
|
|
|
Stability: 2 - Unstable
|
2012-03-03 00:14:03 +01:00
|
|
|
|
2011-08-08 00:56:04 +02:00
|
|
|
To use this module, do `require('readline')`. Readline allows reading of a
|
2012-03-27 00:21:25 +02:00
|
|
|
stream (such as `process.stdin`) on a line-by-line basis.
|
2011-04-20 18:04:42 +02:00
|
|
|
|
2011-08-08 00:56:04 +02:00
|
|
|
Note that once you've invoked this module, your node program will not
|
2012-04-17 20:44:54 +02:00
|
|
|
terminate until you've closed the interface. Here's how to allow your
|
|
|
|
program to gracefully exit:
|
2011-04-20 18:04:42 +02:00
|
|
|
|
2011-08-08 00:56:04 +02:00
|
|
|
var rl = require('readline');
|
2011-04-20 18:04:42 +02:00
|
|
|
|
2012-03-27 00:21:25 +02:00
|
|
|
var i = rl.createInterface({
|
|
|
|
input: process.stdin,
|
|
|
|
output: process.stdout
|
|
|
|
});
|
|
|
|
|
|
|
|
i.question("What do you think of node.js? ", function(answer) {
|
2011-08-08 00:56:04 +02:00
|
|
|
// TODO: Log the answer in a database
|
2012-03-27 00:21:25 +02:00
|
|
|
console.log("Thank you for your valuable feedback:", answer);
|
2011-04-20 18:04:42 +02:00
|
|
|
|
2012-04-17 20:44:54 +02:00
|
|
|
i.close();
|
2011-08-08 00:56:04 +02:00
|
|
|
});
|
2011-04-20 18:04:42 +02:00
|
|
|
|
2012-03-27 00:21:25 +02:00
|
|
|
## rl.createInterface(options)
|
|
|
|
|
|
|
|
Creates a readline `Interface` instance. Accepts an "options" Object that takes
|
|
|
|
the following values:
|
|
|
|
|
|
|
|
- `input` - the readable stream to listen to (Required).
|
|
|
|
|
|
|
|
- `output` - the writable stream to write readline data to (Required).
|
|
|
|
|
|
|
|
- `completer` - an optional function that is used for Tab autocompletion. See
|
|
|
|
below for an example of using this.
|
|
|
|
|
2012-04-17 20:17:54 +02:00
|
|
|
- `terminal` - pass `true` if the `input` and `output` streams should be
|
|
|
|
treated like a TTY, and have ANSI/VT100 escape codes written to it.
|
|
|
|
Defaults to checking `isTTY` on the `output` stream upon instantiation.
|
2012-03-27 00:21:25 +02:00
|
|
|
|
|
|
|
The `completer` function is given a the current line entered by the user, and
|
|
|
|
is supposed to return an Array with 2 entries:
|
2011-04-20 18:04:42 +02:00
|
|
|
|
2012-03-27 00:21:25 +02:00
|
|
|
1. An Array with matching entries for the completion.
|
|
|
|
|
|
|
|
2. The substring that was used for the matching.
|
|
|
|
|
|
|
|
Which ends up looking something like:
|
|
|
|
`[[substr1, substr2, ...], originalsubstring]`.
|
2011-04-20 18:04:42 +02:00
|
|
|
|
2012-04-16 21:03:30 +02:00
|
|
|
Example:
|
|
|
|
|
|
|
|
function completer(line) {
|
|
|
|
var completions = '.help .error .exit .quit .q'.split(' ')
|
|
|
|
var hits = completions.filter(function(c) { return c.indexOf(line) == 0 })
|
|
|
|
// show all completions if none found
|
|
|
|
return [hits.length ? hits : completions, line]
|
|
|
|
}
|
|
|
|
|
2011-10-11 21:14:00 +02:00
|
|
|
Also `completer` can be run in async mode if it accepts two arguments:
|
|
|
|
|
2012-03-27 00:21:25 +02:00
|
|
|
function completer(linePartial, callback) {
|
|
|
|
callback(null, [['123'], linePartial]);
|
|
|
|
}
|
2011-10-11 21:14:00 +02:00
|
|
|
|
2011-08-08 00:56:04 +02:00
|
|
|
`createInterface` is commonly used with `process.stdin` and
|
|
|
|
`process.stdout` in order to accept user input:
|
2011-04-20 18:04:42 +02:00
|
|
|
|
2012-03-27 00:21:25 +02:00
|
|
|
var readline = require('readline');
|
|
|
|
var rl = readline.createInterface({
|
|
|
|
input: process.stdin,
|
|
|
|
output: process.stdout
|
|
|
|
});
|
|
|
|
|
2012-04-17 20:17:54 +02:00
|
|
|
Once you have a readline instance, you most commonly listen for the
|
|
|
|
`"line"` event.
|
2012-03-27 00:21:25 +02:00
|
|
|
|
2012-04-17 20:17:54 +02:00
|
|
|
If `terminal` is `true` for this instance then the `output` stream will get
|
|
|
|
the best compatability if it defines an `output.columns` property, and fires
|
2012-03-27 00:21:25 +02:00
|
|
|
a `"resize"` event on the `output` if/when the columns ever change
|
|
|
|
(`process.stdout` does this automatically when it is a TTY).
|
2011-04-20 18:04:42 +02:00
|
|
|
|
2012-02-27 20:09:34 +01:00
|
|
|
## Class: Interface
|
|
|
|
|
2012-03-27 00:21:25 +02:00
|
|
|
The class that represents a readline interface with an input and output
|
2012-02-27 20:09:34 +01:00
|
|
|
stream.
|
|
|
|
|
2011-08-08 00:56:04 +02:00
|
|
|
### rl.setPrompt(prompt, length)
|
2011-04-20 18:04:42 +02:00
|
|
|
|
2011-08-08 00:56:04 +02:00
|
|
|
Sets the prompt, for example when you run `node` on the command line, you see
|
|
|
|
`> `, which is node's prompt.
|
2011-04-20 18:04:42 +02:00
|
|
|
|
2012-04-21 05:06:00 +02:00
|
|
|
### rl.prompt([preserveCursor])
|
2011-08-08 00:56:04 +02:00
|
|
|
|
|
|
|
Readies readline for input from the user, putting the current `setPrompt`
|
2012-04-21 05:06:00 +02:00
|
|
|
options on a new line, giving the user a new spot to write. Set `preserveCursor`
|
|
|
|
to `true` to prevent the cursor placement being reset to `0`.
|
2011-08-08 00:56:04 +02:00
|
|
|
|
2012-04-17 20:17:54 +02:00
|
|
|
This will also resume the `input` stream used with `createInterface` if it has
|
2012-02-15 15:08:26 +01:00
|
|
|
been paused.
|
|
|
|
|
2011-08-08 00:56:04 +02:00
|
|
|
### rl.question(query, callback)
|
|
|
|
|
|
|
|
Prepends the prompt with `query` and invokes `callback` with the user's
|
2012-04-17 20:17:54 +02:00
|
|
|
response. Displays the query to the user, and then invokes `callback`
|
|
|
|
with the user's response after it has been typed.
|
2011-04-20 18:04:42 +02:00
|
|
|
|
2012-04-17 20:17:54 +02:00
|
|
|
This will also resume the `input` stream used with `createInterface` if
|
|
|
|
it has been paused.
|
2012-02-15 15:08:26 +01:00
|
|
|
|
2011-04-20 18:04:42 +02:00
|
|
|
Example usage:
|
|
|
|
|
2011-08-08 00:56:04 +02:00
|
|
|
interface.question('What is your favorite food?', function(answer) {
|
|
|
|
console.log('Oh, so your favorite food is ' + answer);
|
|
|
|
});
|
|
|
|
|
|
|
|
### rl.pause()
|
|
|
|
|
2012-03-27 00:21:25 +02:00
|
|
|
Pauses the readline `input` stream, allowing it to be resumed later if needed.
|
2011-08-08 00:56:04 +02:00
|
|
|
|
|
|
|
### rl.resume()
|
|
|
|
|
2012-03-27 00:21:25 +02:00
|
|
|
Resumes the readline `input` stream.
|
2011-08-08 00:56:04 +02:00
|
|
|
|
2012-04-17 20:44:54 +02:00
|
|
|
### rl.close()
|
|
|
|
|
|
|
|
Closes the `Interface` instance, relinquishing control on the `input` and
|
|
|
|
`output` streams. The "close" event will also be emitted.
|
|
|
|
|
2012-04-21 05:06:00 +02:00
|
|
|
### rl.write(data, [key])
|
2011-08-08 00:56:04 +02:00
|
|
|
|
2012-04-21 05:06:00 +02:00
|
|
|
Writes `data` to `output` stream. `key` is an object literal to represent a key
|
|
|
|
sequence; available if the terminal is a TTY.
|
2012-02-15 15:08:26 +01:00
|
|
|
|
2012-03-27 00:21:25 +02:00
|
|
|
This will also resume the `input` stream if it has been paused.
|
2011-08-08 00:56:04 +02:00
|
|
|
|
2012-04-21 05:06:00 +02:00
|
|
|
Example:
|
|
|
|
|
|
|
|
rl.write('Delete me!');
|
|
|
|
// Simulate ctrl+u to delete the line written previously
|
|
|
|
rl.write(null, {ctrl: true, name: 'u'});
|
|
|
|
|
2012-04-10 07:59:13 +02:00
|
|
|
## Events
|
|
|
|
|
2011-08-08 00:56:04 +02:00
|
|
|
### Event: 'line'
|
|
|
|
|
|
|
|
`function (line) {}`
|
|
|
|
|
2012-04-17 20:17:54 +02:00
|
|
|
Emitted whenever the `input` stream receives a `\n`, usually received when the
|
2011-08-08 00:56:04 +02:00
|
|
|
user hits enter, or return. This is a good hook to listen for user input.
|
|
|
|
|
|
|
|
Example of listening for `line`:
|
|
|
|
|
|
|
|
rl.on('line', function (cmd) {
|
|
|
|
console.log('You just typed: '+cmd);
|
|
|
|
});
|
|
|
|
|
2012-02-15 15:08:26 +01:00
|
|
|
### Event: 'pause'
|
2011-08-08 00:56:04 +02:00
|
|
|
|
|
|
|
`function () {}`
|
2011-04-20 18:04:42 +02:00
|
|
|
|
2012-04-17 20:44:54 +02:00
|
|
|
Emitted whenever the `input` stream is paused.
|
2011-04-20 18:04:42 +02:00
|
|
|
|
2012-04-17 20:17:54 +02:00
|
|
|
Also emitted whenever the `input` stream is not paused and receives the
|
|
|
|
`SIGCONT` event. (See events `SIGTSTP` and `SIGCONT`)
|
2011-04-20 18:04:42 +02:00
|
|
|
|
2012-02-15 15:08:26 +01:00
|
|
|
Example of listening for `pause`:
|
|
|
|
|
|
|
|
rl.on('pause', function() {
|
|
|
|
console.log('Readline paused.');
|
|
|
|
});
|
|
|
|
|
|
|
|
### Event: 'resume'
|
|
|
|
|
|
|
|
`function () {}`
|
|
|
|
|
2012-04-17 20:17:54 +02:00
|
|
|
Emitted whenever the `input` stream is resumed.
|
2012-02-15 15:08:26 +01:00
|
|
|
|
|
|
|
Example of listening for `resume`:
|
|
|
|
|
|
|
|
rl.on('resume', function() {
|
|
|
|
console.log('Readline resumed.');
|
2011-08-08 00:56:04 +02:00
|
|
|
});
|
2011-04-20 18:04:42 +02:00
|
|
|
|
2012-04-17 20:44:54 +02:00
|
|
|
### Event: 'close'
|
2012-03-27 21:41:42 +02:00
|
|
|
|
|
|
|
`function () {}`
|
|
|
|
|
2012-04-17 20:44:54 +02:00
|
|
|
Emitted when `close()` is called.
|
|
|
|
|
|
|
|
Also emitted when the `input` stream receives its "end" event. The `Interface`
|
|
|
|
instance should be considered "finished" once this is emitted. For example, when
|
|
|
|
the `input` stream receives `^D`, respectively known as `EOT`.
|
|
|
|
|
|
|
|
This event is also called if there is no `SIGINT` event listener present when
|
|
|
|
the `input` stream receives a `^C`, respectively known as `SIGINT`.
|
2012-03-27 21:41:42 +02:00
|
|
|
|
2012-02-15 15:08:26 +01:00
|
|
|
### Event: 'SIGINT'
|
|
|
|
|
|
|
|
`function () {}`
|
|
|
|
|
2012-04-17 20:17:54 +02:00
|
|
|
Emitted whenever the `input` stream receives a `^C`, respectively known as
|
|
|
|
`SIGINT`. If there is no `SIGINT` event listener present when the `input`
|
|
|
|
stream receives a `SIGINT`, `pause` will be triggered.
|
2012-02-15 15:08:26 +01:00
|
|
|
|
|
|
|
Example of listening for `SIGINT`:
|
|
|
|
|
|
|
|
rl.on('SIGINT', function() {
|
|
|
|
rl.question('Are you sure you want to exit?', function(answer) {
|
|
|
|
if (answer.match(/^y(es)?$/i)) rl.pause();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
### Event: 'SIGTSTP'
|
|
|
|
|
|
|
|
`function () {}`
|
|
|
|
|
2012-02-17 14:53:24 +01:00
|
|
|
**This does not work on Windows.**
|
|
|
|
|
2012-04-17 20:17:54 +02:00
|
|
|
Emitted whenever the `input` stream receives a `^Z`, respectively known as
|
|
|
|
`SIGTSTP`. If there is no `SIGTSTP` event listener present when the `input`
|
|
|
|
stream receives a `SIGTSTP`, the program will be sent to the background.
|
2012-02-15 15:08:26 +01:00
|
|
|
|
|
|
|
When the program is resumed with `fg`, the `pause` and `SIGCONT` events will be
|
|
|
|
emitted. You can use either to resume the stream.
|
|
|
|
|
|
|
|
The `pause` and `SIGCONT` events will not be triggered if the stream was paused
|
|
|
|
before the program was sent to the background.
|
|
|
|
|
|
|
|
Example of listening for `SIGTSTP`:
|
|
|
|
|
|
|
|
rl.on('SIGTSTP', function() {
|
|
|
|
// This will override SIGTSTP and prevent the program from going to the
|
|
|
|
// background.
|
|
|
|
console.log('Caught SIGTSTP.');
|
|
|
|
});
|
|
|
|
|
|
|
|
### Event: 'SIGCONT'
|
|
|
|
|
|
|
|
`function () {}`
|
|
|
|
|
2012-02-17 14:53:24 +01:00
|
|
|
**This does not work on Windows.**
|
|
|
|
|
2012-04-17 20:17:54 +02:00
|
|
|
Emitted whenever the `input` stream is sent to the background with `^Z`,
|
|
|
|
respectively known as `SIGTSTP`, and then continued with `fg(1)`. This event
|
|
|
|
only emits if the stream was not paused before sending the program to the
|
2012-02-15 15:08:26 +01:00
|
|
|
background.
|
|
|
|
|
|
|
|
Example of listening for `SIGCONT`:
|
|
|
|
|
|
|
|
rl.on('SIGCONT', function() {
|
|
|
|
// `prompt` will automatically resume the stream
|
|
|
|
rl.prompt();
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2012-04-10 07:59:13 +02:00
|
|
|
## Example: Tiny CLI
|
|
|
|
|
2011-08-08 00:56:04 +02:00
|
|
|
Here's an example of how to use all these together to craft a tiny command
|
|
|
|
line interface:
|
2011-04-20 18:04:42 +02:00
|
|
|
|
2011-08-08 00:56:04 +02:00
|
|
|
var readline = require('readline'),
|
2012-02-15 15:08:26 +01:00
|
|
|
rl = readline.createInterface(process.stdin, process.stdout);
|
|
|
|
|
|
|
|
rl.setPrompt('OHAI> ');
|
|
|
|
rl.prompt();
|
2011-04-20 18:04:42 +02:00
|
|
|
|
2011-08-08 00:56:04 +02:00
|
|
|
rl.on('line', function(line) {
|
|
|
|
switch(line.trim()) {
|
|
|
|
case 'hello':
|
|
|
|
console.log('world!');
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
console.log('Say what? I might have heard `' + line.trim() + '`');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rl.prompt();
|
2012-04-17 20:44:54 +02:00
|
|
|
}).on('close', function() {
|
2011-08-08 00:56:04 +02:00
|
|
|
console.log('Have a great day!');
|
|
|
|
process.exit(0);
|
|
|
|
});
|
2011-04-20 18:04:42 +02:00
|
|
|
|