diff --git a/README.md b/README.md index 444461c..77605ea 100644 --- a/README.md +++ b/README.md @@ -7,12 +7,14 @@ The interface is used with `process.stdin` and `process.stdout` in order to acce ```js var readlineSync = require('readline-sync'); + var userName = readlineSync.question('May I have your name? :'); // Wait for user's response. var favFood = readlineSync.question('Hi ' + userName + '! What is your favorite food? :'); + console.log('Oh, ' + userName + ' likes ' + favFood + '!'); ``` -```shell +``` May I have your name? :AnSeki Hi AnSeki! What is your favorite food? :chocolate Oh, AnSeki likes chocolate! @@ -29,7 +31,7 @@ npm install readline-sync ### question ```js -line = readlineSync.question([query[, options]]) +answer = readlineSync.question([query[, options]]) ``` Displays the `query` to the user, and then returns the user's response after it has been typed. @@ -40,7 +42,7 @@ The `query` may be string, or may not be (e.g. number, Date, Object, etc.). This ### prompt ```js -line = readlineSync.prompt([options]) +input = readlineSync.prompt([options]) ``` Displays the current prompt (See `setPrompt` method) to the user, and then returns the user's response after it has been typed. @@ -49,16 +51,16 @@ You can specify `options`. (see [Options](#options)) ### setPrompt ```js -currentPrompt = readlineSync.setPrompt([prompt]) +currentPrompt = readlineSync.setPrompt([newPrompt]) ``` Sets the prompt, for example when you run `node` on the command line, you see `> `, which is node's prompt. (See `prompt` method) -The `prompt` may be string, or may not be (e.g. number, Date, Object, etc.). This is converted to string (i.e. `toString` method is called) before it is displayed every time. +The `newPrompt` may be string, or may not be (e.g. number, Date, Object, etc.). This is converted to string (i.e. `toString` method is called) before it is displayed every time. For example, `[foo-directory]#` like a bash shell that show the current directory. ```js -// Object that has toString method. +// Simple Object that has toString method. readlineSync.setPrompt({ toString: function() { return '[' + require('path').basename(process.cwd()) + ']# '; // Get and show current directory. @@ -69,7 +71,7 @@ readlineSync.setPrompt({ ### setEncoding ```js -currentEncoding = readlineSync.setEncoding([encoding]) +currentEncoding = readlineSync.setEncoding([newEncoding]) ``` Set the encoding method of input (user's response) and output (`prompt` method and `question` method). Defaults to 'utf8'. @@ -77,11 +79,11 @@ Set the encoding method of input (user's response) and output (`prompt` method a ### setPrint ```js -readlineSync.setPrint([funcPrint]) +readlineSync.setPrint([callback]) ``` -The specified `funcPrint` Function is called when any outputs (`prompt` method and `question` method). Defaults to `undefined`. -The `funcPrint` is given two arguments the output text and `encoding`. +The specified `callback` Function is called when any outputs (`prompt` method and `question` method). Defaults to `undefined`. +The `callback` is given two arguments the output text and `encoding`. ![sample](cl_01.png) @@ -89,7 +91,7 @@ For example, this is used to pass plain texts to the Logger, when texts are colo ```js var readlineSync = require('readline-sync'), - user, pw, cmd; + user, pw, command; require('colors'); readlineSync.setPrint(function(display, encoding) { @@ -103,7 +105,7 @@ pw = readlineSync.question('PASSWORD'.white.inverse + ': ', {noEchoBack: true}); console.log(('Welcome, ' + user + '!').green.bold); readlineSync.setPrompt('> '.bold.red); -cmd = readlineSync.prompt(); +command = readlineSync.prompt(); ``` ## Options diff --git a/lib/read.bat b/lib/read.bat index c0135b6..9ad1e98 100644 --- a/lib/read.bat +++ b/lib/read.bat @@ -1,11 +1,30 @@ @echo off setlocal if "%1"=="noechoback" ( - set /p LINE=NUL - echo; >CON + call :exprog ) else ( set /p LINE=CON ) set /p ="'%LINE%'"NUL 2>&1 + +:: Win <7 and CON'); +while (oExec.Status === 0) { WScript.Sleep(100); } diff --git a/lib/readline-sync.js b/lib/readline-sync.js index 26ba5dc..566d9a7 100644 --- a/lib/readline-sync.js +++ b/lib/readline-sync.js @@ -13,12 +13,14 @@ var ALGORITHM_CIPHER = 'aes-256-cbc', ALGORITHM_HASH = 'sha256', - promptText = '> ', - encoding = 'utf8', fs = require('fs'), + childProc = require('child_process'), stdin = process.stdin, stdout = process.stdout, buffer = new Buffer(BUF_SIZE), + + promptText = '> ', + encoding = 'utf8', useShell = true, print, tempdir, salt = 0; function _readlineSync(display, options) { @@ -101,6 +103,7 @@ function _readlineShell(noEchoBack) { ' %Q%' + ALGORITHM_CIPHER + '%Q% %Q%' + password + '%Q%' + ' >%Q%' + pathStdout + '%Q%' + ' & (echo !ERRORLEVEL!)>%Q%' + pathStatus + '%Q% & (echo 1)>%Q%' + pathDone + '%Q%']; + process.env.Q = '"'; } else { shellPath = '/bin/sh'; args = ['-c', @@ -113,7 +116,10 @@ function _readlineShell(noEchoBack) { } stdin.pause(); // re-start in child process - require('child_process').execFile(shellPath, args, {env: {Q: '"'}}); + childProc.spawn(shellPath, args, { + env: process.env, + stdio: [stdin] // ScriptPW needs piped stdin + }); while (fs.readFileSync(pathDone, {encoding: encoding}).trim() !== '1') {} if (fs.readFileSync(pathStatus, {encoding: encoding}).trim() === '0') { diff --git a/package.json b/package.json index 09ad721..b48f095 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "readline-sync", - "version": "0.5.3", + "version": "0.5.4", "title": "readlineSync", "description": "Synchronous Readline", "keywords": [