setPrompt() and question() accept the value which is not string too (e.g. number, Date, Object, etc.).

This commit is contained in:
anseki 2014-07-12 08:16:11 +09:00
parent 055e4b3060
commit cabbc1ffea
3 changed files with 15 additions and 10 deletions

View file

@ -14,7 +14,7 @@ console.log('Oh, so your favorite food is ' + answer);
## Installation ## Installation
``` ```
npm install -g readline-sync npm install readline-sync
``` ```
## Usage ## Usage
@ -100,6 +100,7 @@ try {
+ If `options.noEchoBack` is used, the text that input by user is saved to temporary file (e.g. `/tmp/readline-sync.stdout`). This file is removed immediately after reading is done, but you have to be careful about it because this text is *plain*. Removing the file might fail, or the file might be peeped before it is removed. + If `options.noEchoBack` is used, the text that input by user is saved to temporary file (e.g. `/tmp/readline-sync.stdout`). This file is removed immediately after reading is done, but you have to be careful about it because this text is *plain*. Removing the file might fail, or the file might be peeped before it is removed.
## Release History ## Release History
* 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 v0.4.0 Add `options.noEchoBack`. * 2014-07-12 v0.4.0 Add `options.noEchoBack`.
* 2014-07-12 v0.3.0 Add `setPrint()`. * 2014-07-12 v0.3.0 Add `setPrint()`.
* 2014-06-27 v0.2.3 Add alternative reading via shell on the environment which don't support interactively reading. * 2014-06-27 v0.2.3 Add alternative reading via shell on the environment which don't support interactively reading.

View file

@ -20,16 +20,16 @@ var promptText = '> ',
function _readlineSync(display, options) { function _readlineSync(display, options) {
var input = '', rsize, err; var input = '', rsize, err;
if (display) { if (display !== '') {
if (typeof print === 'function') { print(display, encoding); } if (typeof print === 'function') { print(display, encoding); }
stdout.write(display, encoding); stdout.write(display + '', encoding);
} }
if (options && options.noEchoBack) { // Try reading via shell if (options && options.noEchoBack) { // Try reading via shell
input = _readlineShell(true); input = _readlineShell(true);
if (typeof input !== 'string') { if (typeof input !== 'string') {
if (display) { stdout.write('\n', encoding); } // Return from prompt line. if (display !== '') { stdout.write('\n', encoding); } // Return from prompt line.
throw new Error('Can\'t read via shell'); throw new Error('Can\'t read via shell');
} }
@ -57,7 +57,7 @@ function _readlineSync(display, options) {
err.errno = e.errno; err.errno = e.errno;
err.code = e.code; err.code = e.code;
} }
if (display) { stdout.write('\n', encoding); } // Return from prompt line. if (display !== '') { stdout.write('\n', encoding); } // Return from prompt line.
throw err || e; throw err || e;
} }
@ -136,9 +136,9 @@ exports.useShellSet = function(use) { useShell = use; };
exports.setPrint = function(fnc) { print = fnc; }; exports.setPrint = function(fnc) { print = fnc; };
exports.setPrompt = function(newPrompt) { exports.setPrompt = function(newPrompt) {
if (typeof newPrompt === 'string') { /* jshint eqnull:true */
promptText = newPrompt; promptText = newPrompt != null ? newPrompt : '';
} /* jshint eqnull:false */
}; };
exports.setEncoding = function(newEncoding) { exports.setEncoding = function(newEncoding) {
@ -152,5 +152,9 @@ exports.prompt = function(options) {
}; };
exports.question = function(query, options) { exports.question = function(query, options) {
return _readlineSync(typeof query === 'string' ? query : '', options); return _readlineSync(
/* jshint eqnull:true */
query != null ? query : '',
/* jshint eqnull:false */
options);
}; };

View file

@ -1,7 +1,7 @@
{ {
"name": "readline-sync", "name": "readline-sync",
"description": "Synchronous Readline", "description": "Synchronous Readline",
"version": "0.4.0", "version": "0.4.1",
"homepage": "https://github.com/anseki/readline-sync", "homepage": "https://github.com/anseki/readline-sync",
"author": { "author": {
"name": "anseki" "name": "anseki"