Initial commit
This commit is contained in:
commit
1b877061ff
5 changed files with 117 additions and 0 deletions
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
node_modules
|
||||
npm-debug.log
|
||||
tmp
|
22
LICENSE-MIT
Normal file
22
LICENSE-MIT
Normal file
|
@ -0,0 +1,22 @@
|
|||
Copyright (c) 2013 anseki
|
||||
|
||||
Permission is hereby granted, free of charge, to any person
|
||||
obtaining a copy of this software and associated documentation
|
||||
files (the "Software"), to deal in the Software without
|
||||
restriction, including without limitation the rights to use,
|
||||
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the
|
||||
Software is furnished to do so, subject to the following
|
||||
conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
OTHER DEALINGS IN THE SOFTWARE.
|
30
README.md
Normal file
30
README.md
Normal file
|
@ -0,0 +1,30 @@
|
|||
# readline-sync
|
||||
|
||||
Synchronous [Readline.question](http://nodejs.org/api/readline.html#readline_rl_question_query_callback) for interactively running.
|
||||
|
||||
## Example
|
||||
|
||||
```js
|
||||
var readlineSync = require('readline-sync');
|
||||
|
||||
var answer = readlineSync('What is your favorite food? :');
|
||||
console.log('Oh, so your favorite food is ' + answer);
|
||||
```
|
||||
|
||||
## Installation
|
||||
|
||||
```
|
||||
npm install -g readline-sync
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
answer = readlineSync([prompt[, encoding]])
|
||||
```
|
||||
|
||||
`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'.
|
||||
|
||||
## Release History
|
||||
* 2013-08-29 v0.1.0 Initial release.
|
28
lib/readline-sync.js
Normal file
28
lib/readline-sync.js
Normal file
|
@ -0,0 +1,28 @@
|
|||
/*
|
||||
* readlineSync
|
||||
* https://github.com/anseki/readline-sync
|
||||
*
|
||||
* Copyright (c) 2013 anseki
|
||||
* Licensed under the MIT license.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
module.exports = function(prompt, encoding) {
|
||||
var input = '', BUF_SIZE = 256, rsize,
|
||||
fs = require('fs'),
|
||||
stdin = process.stdin,
|
||||
buffer = new Buffer(BUF_SIZE);
|
||||
encoding = encoding || 'utf8';
|
||||
|
||||
if (prompt) { process.stdout.write(prompt, encoding); }
|
||||
|
||||
stdin.resume();
|
||||
while ((rsize = fs.readSync(stdin.fd, buffer, 0, BUF_SIZE)) > 0) {
|
||||
input += buffer.toString(encoding, 0, rsize);
|
||||
if (/[\r\n]$/.test(input)) { break; }
|
||||
}
|
||||
stdin.pause();
|
||||
|
||||
return input.trim();
|
||||
};
|
34
package.json
Normal file
34
package.json
Normal file
|
@ -0,0 +1,34 @@
|
|||
{
|
||||
"name": "readline-sync",
|
||||
"description": "Synchronous Readline.question",
|
||||
"version": "0.1.0",
|
||||
"homepage": "https://github.com/anseki/readline-sync",
|
||||
"author": {
|
||||
"name": "anseki"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/anseki/readline-sync.git"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/anseki/readline-sync/issues"
|
||||
},
|
||||
"licenses": [
|
||||
{
|
||||
"type": "MIT",
|
||||
"url": "https://github.com/anseki/readline-sync/blob/master/LICENSE-MIT"
|
||||
}
|
||||
],
|
||||
"main": "./lib/readline-sync.js",
|
||||
"engines": {
|
||||
"node": ">= 0.8.0"
|
||||
},
|
||||
"keywords": [
|
||||
"readline",
|
||||
"question",
|
||||
"synchronous",
|
||||
"prompt",
|
||||
"wait",
|
||||
"block"
|
||||
]
|
||||
}
|
Loading…
Reference in a new issue