From 1b877061ffcbafb097fc76818683e83bc2121a2f Mon Sep 17 00:00:00 2001 From: anseki Date: Thu, 29 Aug 2013 19:55:23 +0900 Subject: [PATCH] Initial commit --- .gitignore | 3 +++ LICENSE-MIT | 22 ++++++++++++++++++++++ README.md | 30 ++++++++++++++++++++++++++++++ lib/readline-sync.js | 28 ++++++++++++++++++++++++++++ package.json | 34 ++++++++++++++++++++++++++++++++++ 5 files changed, 117 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE-MIT create mode 100644 README.md create mode 100644 lib/readline-sync.js create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b785247 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +node_modules +npm-debug.log +tmp diff --git a/LICENSE-MIT b/LICENSE-MIT new file mode 100644 index 0000000..7f1db80 --- /dev/null +++ b/LICENSE-MIT @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..9bbca61 --- /dev/null +++ b/README.md @@ -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. diff --git a/lib/readline-sync.js b/lib/readline-sync.js new file mode 100644 index 0000000..79f99e2 --- /dev/null +++ b/lib/readline-sync.js @@ -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(); +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..54c67ca --- /dev/null +++ b/package.json @@ -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" + ] +} \ No newline at end of file