Change: read by using PowerShell or ScriptPW in DOS.
This commit is contained in:
parent
ca90461c57
commit
995f22b4bf
5 changed files with 49 additions and 18 deletions
26
README.md
26
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
|
||||
|
|
23
lib/read.bat
23
lib/read.bat
|
@ -1,11 +1,30 @@
|
|||
@echo off
|
||||
setlocal
|
||||
if "%1"=="noechoback" (
|
||||
set /p LINE=<CON >NUL
|
||||
echo; >CON
|
||||
call :exprog
|
||||
) else (
|
||||
set /p LINE=<CON >CON
|
||||
)
|
||||
set /p ="'%LINE%'"<NUL
|
||||
endlocal
|
||||
exit /b 0
|
||||
|
||||
:exprog
|
||||
|
||||
:: where /q powershell
|
||||
:: Win <Vista and <Server2008 don't have `WHERE`.
|
||||
powershell /? >NUL 2>&1
|
||||
|
||||
:: Win <7 and <Server2008R2 don't have PowerShell as default.
|
||||
:: Win XP and Server2003 have `ScriptPW` (`scriptpw.dll`).
|
||||
:: In the systems that don't have both, an error is thrown.
|
||||
if errorlevel 1 (
|
||||
set "EXCOMMAND=cscript //nologo "%~dp0read.cs.js""
|
||||
) else (
|
||||
set "EXCOMMAND=powershell -Command "$text = read-host -AsSecureString; ^
|
||||
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR^($text^); ^
|
||||
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto^($BSTR^)""
|
||||
)
|
||||
:: echo %EXCOMMAND%
|
||||
for /f "usebackq delims=" %%i in (`%EXCOMMAND%`) do set "LINE=%%i"
|
||||
exit /b
|
||||
|
|
4
lib/read.cs.js
Normal file
4
lib/read.cs.js
Normal file
|
@ -0,0 +1,4 @@
|
|||
var oExec;
|
||||
WScript.StdOut.Write(WScript.CreateObject('ScriptPW.Password').GetPassword());
|
||||
oExec = WScript.CreateObject('WScript.Shell').Exec('cmd /c echo; >CON');
|
||||
while (oExec.Status === 0) { WScript.Sleep(100); }
|
|
@ -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') {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "readline-sync",
|
||||
"version": "0.5.3",
|
||||
"version": "0.5.4",
|
||||
"title": "readlineSync",
|
||||
"description": "Synchronous Readline",
|
||||
"keywords": [
|
||||
|
|
Loading…
Reference in a new issue