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
|
```js
|
||||||
var readlineSync = require('readline-sync');
|
var readlineSync = require('readline-sync');
|
||||||
|
|
||||||
var userName = readlineSync.question('May I have your name? :'); // Wait for user's response.
|
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? :');
|
var favFood = readlineSync.question('Hi ' + userName + '! What is your favorite food? :');
|
||||||
|
|
||||||
console.log('Oh, ' + userName + ' likes ' + favFood + '!');
|
console.log('Oh, ' + userName + ' likes ' + favFood + '!');
|
||||||
```
|
```
|
||||||
|
|
||||||
```shell
|
```
|
||||||
May I have your name? :AnSeki
|
May I have your name? :AnSeki
|
||||||
Hi AnSeki! What is your favorite food? :chocolate
|
Hi AnSeki! What is your favorite food? :chocolate
|
||||||
Oh, AnSeki likes chocolate!
|
Oh, AnSeki likes chocolate!
|
||||||
|
@ -29,7 +31,7 @@ npm install readline-sync
|
||||||
### question
|
### question
|
||||||
|
|
||||||
```js
|
```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.
|
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
|
### prompt
|
||||||
|
|
||||||
```js
|
```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.
|
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
|
### setPrompt
|
||||||
|
|
||||||
```js
|
```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)
|
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.
|
For example, `[foo-directory]#` like a bash shell that show the current directory.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
// Object that has toString method.
|
// Simple Object that has toString method.
|
||||||
readlineSync.setPrompt({
|
readlineSync.setPrompt({
|
||||||
toString: function() {
|
toString: function() {
|
||||||
return '[' + require('path').basename(process.cwd()) + ']# '; // Get and show current directory.
|
return '[' + require('path').basename(process.cwd()) + ']# '; // Get and show current directory.
|
||||||
|
@ -69,7 +71,7 @@ readlineSync.setPrompt({
|
||||||
### setEncoding
|
### setEncoding
|
||||||
|
|
||||||
```js
|
```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'.
|
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
|
### setPrint
|
||||||
|
|
||||||
```js
|
```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 specified `callback` 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 `callback` is given two arguments the output text and `encoding`.
|
||||||
|
|
||||||
![sample](cl_01.png)
|
![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
|
```js
|
||||||
var readlineSync = require('readline-sync'),
|
var readlineSync = require('readline-sync'),
|
||||||
user, pw, cmd;
|
user, pw, command;
|
||||||
require('colors');
|
require('colors');
|
||||||
|
|
||||||
readlineSync.setPrint(function(display, encoding) {
|
readlineSync.setPrint(function(display, encoding) {
|
||||||
|
@ -103,7 +105,7 @@ pw = readlineSync.question('PASSWORD'.white.inverse + ': ', {noEchoBack: true});
|
||||||
console.log(('Welcome, ' + user + '!').green.bold);
|
console.log(('Welcome, ' + user + '!').green.bold);
|
||||||
|
|
||||||
readlineSync.setPrompt('> '.bold.red);
|
readlineSync.setPrompt('> '.bold.red);
|
||||||
cmd = readlineSync.prompt();
|
command = readlineSync.prompt();
|
||||||
```
|
```
|
||||||
|
|
||||||
## Options
|
## Options
|
||||||
|
|
23
lib/read.bat
23
lib/read.bat
|
@ -1,11 +1,30 @@
|
||||||
@echo off
|
@echo off
|
||||||
setlocal
|
setlocal
|
||||||
if "%1"=="noechoback" (
|
if "%1"=="noechoback" (
|
||||||
set /p LINE=<CON >NUL
|
call :exprog
|
||||||
echo; >CON
|
|
||||||
) else (
|
) else (
|
||||||
set /p LINE=<CON >CON
|
set /p LINE=<CON >CON
|
||||||
)
|
)
|
||||||
set /p ="'%LINE%'"<NUL
|
set /p ="'%LINE%'"<NUL
|
||||||
endlocal
|
endlocal
|
||||||
exit /b 0
|
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_CIPHER = 'aes-256-cbc',
|
||||||
ALGORITHM_HASH = 'sha256',
|
ALGORITHM_HASH = 'sha256',
|
||||||
|
|
||||||
promptText = '> ',
|
|
||||||
encoding = 'utf8',
|
|
||||||
fs = require('fs'),
|
fs = require('fs'),
|
||||||
|
childProc = require('child_process'),
|
||||||
stdin = process.stdin,
|
stdin = process.stdin,
|
||||||
stdout = process.stdout,
|
stdout = process.stdout,
|
||||||
buffer = new Buffer(BUF_SIZE),
|
buffer = new Buffer(BUF_SIZE),
|
||||||
|
|
||||||
|
promptText = '> ',
|
||||||
|
encoding = 'utf8',
|
||||||
useShell = true, print, tempdir, salt = 0;
|
useShell = true, print, tempdir, salt = 0;
|
||||||
|
|
||||||
function _readlineSync(display, options) {
|
function _readlineSync(display, options) {
|
||||||
|
@ -101,6 +103,7 @@ function _readlineShell(noEchoBack) {
|
||||||
' %Q%' + ALGORITHM_CIPHER + '%Q% %Q%' + password + '%Q%' +
|
' %Q%' + ALGORITHM_CIPHER + '%Q% %Q%' + password + '%Q%' +
|
||||||
' >%Q%' + pathStdout + '%Q%' +
|
' >%Q%' + pathStdout + '%Q%' +
|
||||||
' & (echo !ERRORLEVEL!)>%Q%' + pathStatus + '%Q% & (echo 1)>%Q%' + pathDone + '%Q%'];
|
' & (echo !ERRORLEVEL!)>%Q%' + pathStatus + '%Q% & (echo 1)>%Q%' + pathDone + '%Q%'];
|
||||||
|
process.env.Q = '"';
|
||||||
} else {
|
} else {
|
||||||
shellPath = '/bin/sh';
|
shellPath = '/bin/sh';
|
||||||
args = ['-c',
|
args = ['-c',
|
||||||
|
@ -113,7 +116,10 @@ function _readlineShell(noEchoBack) {
|
||||||
}
|
}
|
||||||
|
|
||||||
stdin.pause(); // re-start in child process
|
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') {}
|
while (fs.readFileSync(pathDone, {encoding: encoding}).trim() !== '1') {}
|
||||||
if (fs.readFileSync(pathStatus, {encoding: encoding}).trim() === '0') {
|
if (fs.readFileSync(pathStatus, {encoding: encoding}).trim() === '0') {
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "readline-sync",
|
"name": "readline-sync",
|
||||||
"version": "0.5.3",
|
"version": "0.5.4",
|
||||||
"title": "readlineSync",
|
"title": "readlineSync",
|
||||||
"description": "Synchronous Readline",
|
"description": "Synchronous Readline",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
|
|
Loading…
Reference in a new issue