Rewrite exporting methods
This commit is contained in:
parent
1b877061ff
commit
f2455207cd
3 changed files with 67 additions and 17 deletions
40
README.md
40
README.md
|
@ -1,13 +1,13 @@
|
||||||
# readline-sync
|
# readlineSync
|
||||||
|
|
||||||
Synchronous [Readline.question](http://nodejs.org/api/readline.html#readline_rl_question_query_callback) for interactively running.
|
Synchronous [Readline](http://nodejs.org/api/readline.html) for interactively running.
|
||||||
|
The interface is used with process.stdin and process.stdout in order to accept user input.
|
||||||
|
|
||||||
## Example
|
## Example
|
||||||
|
|
||||||
```js
|
```js
|
||||||
var readlineSync = require('readline-sync');
|
var readlineSync = require('readline-sync');
|
||||||
|
var answer = readlineSync.question('What is your favorite food? :');
|
||||||
var answer = readlineSync('What is your favorite food? :');
|
|
||||||
console.log('Oh, so your favorite food is ' + answer);
|
console.log('Oh, so your favorite food is ' + answer);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -19,12 +19,38 @@ npm install -g readline-sync
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
|
### setPrompt
|
||||||
|
|
||||||
```js
|
```js
|
||||||
answer = readlineSync([prompt[, encoding]])
|
readlineSync.setPrompt(prompt)
|
||||||
```
|
```
|
||||||
|
|
||||||
`require('readline-sync')` returns a Function. Displays the `prompt` to the user if it is given, and then returns the user's response after it has been typed.
|
Sets the prompt, for example when you run `node` on the command line, you see `> `, which is node's prompt.
|
||||||
`encoding` specify encoding method of input (user's response) and output (`prompt`). Defaults to 'utf8'.
|
|
||||||
|
### prompt
|
||||||
|
|
||||||
|
```js
|
||||||
|
line = readlineSync.prompt()
|
||||||
|
```
|
||||||
|
|
||||||
|
Readies readline for input from the user, putting the current `setPrompt` options on a new line, giving the user a new spot to write.
|
||||||
|
|
||||||
|
### question
|
||||||
|
|
||||||
|
```js
|
||||||
|
line = readlineSync.question(query)
|
||||||
|
```
|
||||||
|
|
||||||
|
Displays the `query` to the user, and then returns the user's response after it has been typed.
|
||||||
|
|
||||||
|
### setEncoding
|
||||||
|
|
||||||
|
```js
|
||||||
|
readlineSync.setPrompt(encoding)
|
||||||
|
```
|
||||||
|
|
||||||
|
Set the encoding method of input (user's response) and output (`prompt`). Defaults to 'utf8'.
|
||||||
|
|
||||||
## Release History
|
## Release History
|
||||||
|
* 2013-08-30 v0.2.0 Rewrite exporting methods.
|
||||||
* 2013-08-29 v0.1.0 Initial release.
|
* 2013-08-29 v0.1.0 Initial release.
|
||||||
|
|
|
@ -8,14 +8,18 @@
|
||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
module.exports = function(prompt, encoding) {
|
var promptText = '> ',
|
||||||
var input = '', BUF_SIZE = 256, rsize,
|
encoding = 'utf8',
|
||||||
fs = require('fs'),
|
BUF_SIZE = 256,
|
||||||
stdin = process.stdin,
|
fs = require('fs'),
|
||||||
buffer = new Buffer(BUF_SIZE);
|
stdin = process.stdin,
|
||||||
encoding = encoding || 'utf8';
|
stdout = process.stdout,
|
||||||
|
buffer = new Buffer(BUF_SIZE);
|
||||||
|
|
||||||
if (prompt) { process.stdout.write(prompt, encoding); }
|
var _readlineSync = function(display) {
|
||||||
|
var input = '', rsize;
|
||||||
|
|
||||||
|
if (display) { stdout.write(display, encoding); }
|
||||||
|
|
||||||
stdin.resume();
|
stdin.resume();
|
||||||
while ((rsize = fs.readSync(stdin.fd, buffer, 0, BUF_SIZE)) > 0) {
|
while ((rsize = fs.readSync(stdin.fd, buffer, 0, BUF_SIZE)) > 0) {
|
||||||
|
@ -26,3 +30,23 @@ module.exports = function(prompt, encoding) {
|
||||||
|
|
||||||
return input.trim();
|
return input.trim();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
exports.setPrompt = function(newPrompt) {
|
||||||
|
if (typeof newPrompt === 'string') {
|
||||||
|
promptText = newPrompt;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.setEncoding = function(newEncoding) {
|
||||||
|
if (typeof newEncoding === 'string') {
|
||||||
|
encoding = newEncoding;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.prompt = function() {
|
||||||
|
return _readlineSync(promptText);
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.question = function(query) {
|
||||||
|
return _readlineSync(typeof query === 'string' ? query : '');
|
||||||
|
};
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"name": "readline-sync",
|
"name": "readline-sync",
|
||||||
"description": "Synchronous Readline.question",
|
"description": "Synchronous Readline",
|
||||||
"version": "0.1.0",
|
"version": "0.2.0",
|
||||||
"homepage": "https://github.com/anseki/readline-sync",
|
"homepage": "https://github.com/anseki/readline-sync",
|
||||||
"author": {
|
"author": {
|
||||||
"name": "anseki"
|
"name": "anseki"
|
||||||
|
@ -25,9 +25,9 @@
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"readline",
|
"readline",
|
||||||
"question",
|
|
||||||
"synchronous",
|
"synchronous",
|
||||||
"prompt",
|
"prompt",
|
||||||
|
"question",
|
||||||
"wait",
|
"wait",
|
||||||
"block"
|
"block"
|
||||||
]
|
]
|
||||||
|
|
Loading…
Reference in a new issue