From 055e4b306016a19b7317655201c98d1e26f0d822 Mon Sep 17 00:00:00 2001 From: anseki Date: Sat, 12 Jul 2014 07:37:25 +0900 Subject: [PATCH] Add `options.noEchoBack`. --- README.md | 21 ++++++----- lib/read.bat | 7 +++- lib/read.sh | 9 ++++- lib/readline-sync.js | 84 ++++++++++++++++++++++++++------------------ package.json | 2 +- 5 files changed, 77 insertions(+), 46 deletions(-) diff --git a/README.md b/README.md index 1542abb..bd88079 100644 --- a/README.md +++ b/README.md @@ -30,18 +30,20 @@ Sets the prompt, for example when you run `node` on the command line, you see `> ### prompt ```js -line = readlineSync.prompt() +line = readlineSync.prompt([options]) ``` -Readies readline for input from the user, putting the current `setPrompt` options on a new line, giving the user a new spot to write. +Readies readline for input from the user, putting the current `setPrompt` options on a new line, giving the user a new spot to write. +If `{noEchoBack: true}` is specified to `options`, echo back is avoided. It is used to hide the password which is typed by user on screen. *See [Note](#note) for security.* ### question ```js -line = readlineSync.question(query) +line = readlineSync.question(query[, options]) ``` -Displays the `query` to the user, and then returns the user's response after it has been typed. +Displays the `query` to the user, and then returns the user's response after it has been typed. +If `{noEchoBack: true}` is specified to `options`, echo back is avoided. It is used to hide the password which is typed by user on screen. *See [Note](#note) for security.* ### setEncoding @@ -74,7 +76,7 @@ readlineSync.setPrint(function(display, encoding) { console.log('Your account required.'.grey); user = readlineSync.question('USER NAME'.white.inverse + ': '); -pw = readlineSync.question('PASSWORD'.white.inverse + ': ', true); +pw = readlineSync.question('PASSWORD'.white.inverse + ': ', {noEchoBack: true}); // Authorization ... console.log(('Welcome, ' + user + '!').green.bold); @@ -82,8 +84,8 @@ readlineSync.setPrompt('> '.bold.red); cmd = readlineSync.prompt(); ``` -## Note -The your Node and OS may not support interactively reading from stdin. The stdin interfaces are different by platforms. +## Note ++ The your Node and OS may not support interactively reading from stdin. The stdin interfaces are different by platforms. If in those platforms, an error is thrown. ```js @@ -95,8 +97,11 @@ 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. + ## Release History - * 2014-07-12 v0.3.0 Add setPrint(). + * 2014-07-12 v0.4.0 Add `options.noEchoBack`. + * 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. * 2013-12-18 v0.2.2 Error handle for the environment which don't support interactively reading from stdin. * 2013-08-30 v0.2.0 Rewrite exporting methods. diff --git a/lib/read.bat b/lib/read.bat index 9d2b9e6..7116485 100644 --- a/lib/read.bat +++ b/lib/read.bat @@ -1,6 +1,11 @@ @echo off setlocal -set /p LINE=CON +if "%1"=="noechoback" ( + set /p LINE=NUL + echo; >CON +) else ( + set /p LINE=CON +) set /p DUM="%LINE%"/dev/tty +else + read LINE %Q%' + pathStdout + + args = ['/V:ON', '/S', '/C', + '%Q%' + __dirname + '\\read.bat%Q%' + optEchoBack + ' >%Q%' + pathStdout + '%Q% & (echo !ERRORLEVEL!)>%Q%' + pathStatus + '%Q% & (echo 1)>%Q%' + pathDone + '%Q%']; } else { shellPath = '/bin/sh'; - args = ['-c', '(' + shellPath + ' "' + __dirname + '/read.sh") >"' + pathStdout + + args = ['-c', '(' + shellPath + ' "' + __dirname + '/read.sh"' + optEchoBack + ') >"' + pathStdout + '"; echo $? >"' + pathStatus + '"; echo 1 >"' + pathDone + '"']; } @@ -133,10 +147,10 @@ exports.setEncoding = function(newEncoding) { } }; -exports.prompt = function() { - return _readlineSync(promptText); +exports.prompt = function(options) { + return _readlineSync(promptText, options); }; -exports.question = function(query) { - return _readlineSync(typeof query === 'string' ? query : ''); +exports.question = function(query, options) { + return _readlineSync(typeof query === 'string' ? query : '', options); }; diff --git a/package.json b/package.json index a80b937..83f35d6 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "readline-sync", "description": "Synchronous Readline", - "version": "0.3.0", + "version": "0.4.0", "homepage": "https://github.com/anseki/readline-sync", "author": { "name": "anseki"