2013-08-29 19:26:22 +02:00
# readlineSync
2013-08-29 12:55:23 +02:00
2013-08-29 19:26:22 +02:00
Synchronous [Readline ](http://nodejs.org/api/readline.html ) for interactively running.
2014-08-10 11:16:08 +02:00
The interface is used with `process.stdin` and `process.stdout` in order to accept user input.
2013-08-29 12:55:23 +02:00
## Example
```js
var readlineSync = require('readline-sync');
2013-08-29 19:26:22 +02:00
var answer = readlineSync.question('What is your favorite food? :');
2013-08-29 12:55:23 +02:00
console.log('Oh, so your favorite food is ' + answer);
```
## Installation
```
2014-07-12 01:16:11 +02:00
npm install readline-sync
2013-08-29 12:55:23 +02:00
```
## Usage
2013-08-29 19:26:22 +02:00
### setPrompt
```js
2014-07-12 01:56:21 +02:00
currentValue = readlineSync.setPrompt([prompt])
2013-08-29 19:26:22 +02:00
```
2014-07-12 01:56:21 +02:00
Sets the prompt, for example when you run `node` on the command line, you see `> ` , which is node's prompt.
2014-07-13 07:24:37 +02:00
`prompt` may be string, or may not be (e.g. number, Date, Object, etc.). This is converted to string (i.e. `toString` method is called) before it is displayed every time.
2014-08-10 11:16:08 +02:00
For example: `[foo-directory]#` like a bash
2014-07-12 01:56:21 +02:00
```js
2014-08-10 11:16:08 +02:00
// Object that has toString method.
readlineSync.setPrompt({toString: function() {
return '[' + require('path').basename(process.cwd()) + ']# ';
}})
2014-07-12 01:56:21 +02:00
```
2013-08-29 19:26:22 +02:00
### prompt
```js
2014-07-12 00:37:25 +02:00
line = readlineSync.prompt([options])
2013-08-29 19:26:22 +02:00
```
2014-07-12 00:37:25 +02:00
Readies readline for input from the user, putting the current `setPrompt` options on a new line, giving the user a new spot to write.
2014-07-13 07:24:37 +02:00
If `{noEchoBack: true}` is specified to `options` , echo back is avoided. It is used to hide the secret text (e.g. password) which is typed by user on screen.
2013-08-29 19:26:22 +02:00
### question
```js
2014-07-12 01:56:21 +02:00
line = readlineSync.question([query[, options]])
2013-08-29 19:26:22 +02:00
```
2014-07-12 00:37:25 +02:00
Displays the `query` to the user, and then returns the user's response after it has been typed.
2014-07-13 07:24:37 +02:00
`query` may be string, or may not be (e.g. number, Date, Object, etc.). This is converted to string (i.e. `toString` method is called) before it is displayed.
If `{noEchoBack: true}` is specified to `options` , echo back is avoided. It is used to hide the secret text (e.g. password) which is typed by user on screen.
2013-08-29 19:26:22 +02:00
### setEncoding
2013-08-29 12:55:23 +02:00
```js
2014-07-12 01:56:21 +02:00
currentValue = readlineSync.setEncoding([encoding])
2013-08-29 12:55:23 +02:00
```
2014-07-13 13:45:56 +02:00
Set the encoding method of input (user's response) and output (`prompt` and `question` ). Defaults to 'utf8'.
2013-08-29 12:55:23 +02:00
2014-07-11 23:25:59 +02:00
### setPrint
```js
2014-07-13 13:45:56 +02:00
readlineSync.setPrint([funcPrint])
2014-07-11 23:25:59 +02:00
```
2014-07-13 13:45:56 +02:00
The specified Function is called when any output (`prompt` and `question` ). Defaults to `undefined` .
The Function is given two arguments the output text and `encoding` .
2014-07-11 23:25:59 +02:00
![sample ](cl_01.png )
2014-07-13 13:45:56 +02:00
For example, this is used to pass plain texts to Logger, when texts are colored.
2014-07-11 23:25:59 +02:00
```js
2014-07-13 13:45:56 +02:00
var readlineSync = require('readline-sync'),
2014-07-11 23:25:59 +02:00
user, pw, cmd;
require('colors');
readlineSync.setPrint(function(display, encoding) {
logger.log(display.stripColors); // remove control characters
});
console.log('Your account required.'.grey);
user = readlineSync.question('USER NAME'.white.inverse + ': ');
2014-07-12 00:37:25 +02:00
pw = readlineSync.question('PASSWORD'.white.inverse + ': ', {noEchoBack: true});
2014-07-11 23:25:59 +02:00
// Authorization ...
console.log(('Welcome, ' + user + '!').green.bold);
readlineSync.setPrompt('> '.bold.red);
cmd = readlineSync.prompt();
```
2014-07-14 06:28:39 +02:00
## Note
2014-08-21 09:23:39 +02:00
### Platforms
The your Node and OS may not support interactively reading from stdin. The stdin interfaces are different by platforms.
2013-12-18 03:51:32 +01:00
If in those platforms, an error is thrown.
```js
try {
answer = readlineSync.question('What is your favorite food? :');
} catch (e) {
console.error(e);
process.exit(1);
}
```
2013-12-17 18:18:39 +01:00
2014-08-21 09:23:39 +02:00
### Reading by shell
readlineSync tries reading from stdin by shell if it is needed. And, it use "piping via files" for synchronous running.
As everyone knows, "piping via files" is no good. It blocks event loop and a process. It may make your script be slow.
Why did I choose it? :
+ The best solution is [child_process.execSync ](https://github.com/joyent/node/blob/master/doc/api/child_process.markdown#child_processexecsynccommand-options ) in core modules of Node. But it is not supported by current version.
+ The good modules (native addon) for synchronous execution exist. But node-gyp can't compile those in some platforms or Node versions.
2014-08-21 09:45:17 +02:00
+ I think that the security is important more than the speed. Some modules have problem about security. (Those don't protect data.) I think that the speed is not needed usually, because readlineSync is used while user types keys.
2014-08-21 09:23:39 +02:00
Someday, I may rewrite readlineSync to use child_process.execSync, or safety module.
2013-08-29 12:55:23 +02:00
## Release History
2014-07-13 06:55:17 +02:00
* 2014-07-13 v0.4.3 fixed #6: Crypto input data.
2014-07-12 01:56:21 +02:00
* 2014-07-12 v0.4.2 `setPrompt()` and `setEncoding()` return current value.
2014-07-12 01:16:11 +02:00
* 2014-07-12 v0.4.1 `setPrompt()` and `question()` accept the value which is not string too (e.g. number, Date, Object, etc.).
2014-07-12 00:37:25 +02:00
* 2014-07-12 v0.4.0 Add `options.noEchoBack` .
* 2014-07-12 v0.3.0 Add `setPrint()` .
2014-06-27 03:06:29 +02:00
* 2014-06-27 v0.2.3 Add alternative reading via shell on the environment which don't support interactively reading.
2013-12-18 03:51:32 +01:00
* 2013-12-18 v0.2.2 Error handle for the environment which don't support interactively reading from stdin.
2013-08-29 19:26:22 +02:00
* 2013-08-30 v0.2.0 Rewrite exporting methods.
2013-08-29 12:55:23 +02:00
* 2013-08-29 v0.1.0 Initial release.