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
|
||||
|
||||
```js
|
||||
var readlineSync = require('readline-sync');
|
||||
|
||||
var answer = readlineSync('What is your favorite food? :');
|
||||
var answer = readlineSync.question('What is your favorite food? :');
|
||||
console.log('Oh, so your favorite food is ' + answer);
|
||||
```
|
||||
|
||||
|
@ -19,12 +19,38 @@ npm install -g readline-sync
|
|||
|
||||
## Usage
|
||||
|
||||
### setPrompt
|
||||
|
||||
```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.
|
||||
`encoding` specify encoding method of input (user's response) and output (`prompt`). Defaults to 'utf8'.
|
||||
Sets the prompt, for example when you run `node` on the command line, you see `> `, which is node's prompt.
|
||||
|
||||
### 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
|
||||
* 2013-08-30 v0.2.0 Rewrite exporting methods.
|
||||
* 2013-08-29 v0.1.0 Initial release.
|
||||
|
|
|
@ -8,14 +8,18 @@
|
|||
|
||||
'use strict';
|
||||
|
||||
module.exports = function(prompt, encoding) {
|
||||
var input = '', BUF_SIZE = 256, rsize,
|
||||
var promptText = '> ',
|
||||
encoding = 'utf8',
|
||||
BUF_SIZE = 256,
|
||||
fs = require('fs'),
|
||||
stdin = process.stdin,
|
||||
stdout = process.stdout,
|
||||
buffer = new Buffer(BUF_SIZE);
|
||||
encoding = encoding || 'utf8';
|
||||
|
||||
if (prompt) { process.stdout.write(prompt, encoding); }
|
||||
var _readlineSync = function(display) {
|
||||
var input = '', rsize;
|
||||
|
||||
if (display) { stdout.write(display, encoding); }
|
||||
|
||||
stdin.resume();
|
||||
while ((rsize = fs.readSync(stdin.fd, buffer, 0, BUF_SIZE)) > 0) {
|
||||
|
@ -26,3 +30,23 @@ module.exports = function(prompt, encoding) {
|
|||
|
||||
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",
|
||||
"description": "Synchronous Readline.question",
|
||||
"version": "0.1.0",
|
||||
"description": "Synchronous Readline",
|
||||
"version": "0.2.0",
|
||||
"homepage": "https://github.com/anseki/readline-sync",
|
||||
"author": {
|
||||
"name": "anseki"
|
||||
|
@ -25,9 +25,9 @@
|
|||
},
|
||||
"keywords": [
|
||||
"readline",
|
||||
"question",
|
||||
"synchronous",
|
||||
"prompt",
|
||||
"question",
|
||||
"wait",
|
||||
"block"
|
||||
]
|
||||
|
|
Loading…
Reference in a new issue