Add setMask.

This commit is contained in:
anseki 2015-03-20 20:34:18 +09:00
parent ecd6846f7c
commit 95d2e9f088
5 changed files with 71 additions and 45 deletions

View file

@ -55,7 +55,8 @@ currentPrompt = readlineSync.setPrompt([newPrompt])
Set the prompt, for example when you run `node` on the command line, you see `> `, which is node's prompt. The default is `'> '`. (See `prompt` method)
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.
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
@ -67,53 +68,85 @@ readlineSync.setPrompt({
})
```
### setEncoding
```js
currentEncoding = readlineSync.setEncoding([newEncoding])
```
Set the encoding method of input (user's response) and output (`prompt` method and `question` method). The default is `'utf8'`.
### setPrint
```js
readlineSync.setPrint([callback])
```
The specified `callback` Function is called when any outputs (`prompt` method and `question` method).
The `callback` is given two arguments the output text and `encoding`.
The specified `callback` Function is called when any outputs (`prompt` method and `question` method) are done. The `callback` is given two arguments the output text and `encoding`.
For example:
* This is used to pass plain texts to the Logger, when texts are colored.
![sample](cl_01.png)
For example, this is used to pass plain texts to the Logger, when texts are colored.
```js
var readlineSync = require('readline-sync'),
chalk = require('chalk'),
user, pw, command;
readlineSync.setPrint(function(display, encoding) {
logger.log(chalk.stripColor(display)); // Remove control characters.
});
console.log(chalk.black.bold.bgYellow(' Your Account '));
user = readlineSync.question(chalk.gray.underline(' USER NAME ') + ' :');
pw = readlineSync.question(chalk.gray.underline(' PASSWORD ') + ' :', {noEchoBack: true});
// Authorization ...
console.log(chalk.green('Welcome, ' + user + '!'));
readlineSync.setPrompt(chalk.red.bold('> '));
command = readlineSync.prompt();
```
* When you do the redirecting that like `node yourscript.js > foo.log` to record into a file, this is used to output conversation to the file. That is, the conversation isn't outputted to `foo.log` without this code.
```js
var readlineSync = require('readline-sync');
readlineSync.setPrint(function(display, encoding) {
console.log(display); // Output to STDOUT (foo.log)
});
```
### setMask
```js
currentMask = readlineSync.setMask([newMask])
```
Set the mask character that is shown instead of the secret text (e.g. password). (See `noEchoBack` option.) The default is `'*'`. If you want to show nothing, specify `''`. (But it's not user friendly.)
*Note:* The some platforms might use `'*'` or `''` always.
For example:
```js
var readlineSync = require('readline-sync'),
user, pw, command;
require('colors');
chalk = require('chalk'),
secret;
readlineSync.setPrint(function(display, encoding) {
logger.log(display.stripColors); // Remove control characters.
});
console.log('Your account required.'.grey);
user = readlineSync.question('USER NAME'.white.inverse + ': ');
pw = readlineSync.question('PASSWORD'.white.inverse + ': ', {noEchoBack: true});
// Authorization ...
console.log(('Welcome, ' + user + '!').green.bold);
readlineSync.setPrompt('> '.bold.red);
command = readlineSync.prompt();
readlineSync.setMask(chalk.magenta('\u2665'));
secret = readlineSync.question('Please whisper sweet words :', {noEchoBack: true});
```
![sample](cl_02.png)
### setBufferSize
```js
currentBufferSize = readlineSync.setBufferSize([newBufferSize])
```
When readlineSync reads from TTY directly (without reading by shell), a size `newBufferSize` buffer is used. Even if the user's response exceeds it, it's usually no problem, because the buffer is used repeatedly. But, some platforms's TTY may not accept user's response that is too long. And set an enough size. The default is `1024`.
When readlineSync reads from TTY directly (without reading by shell), a size `newBufferSize` buffer is used. Even if the user's response exceeds it, it's usually no problem, because the buffer is used repeatedly. But, some platforms's TTY might not accept user's response that exceeds it. And set an enough size. The default is `1024`.
### setEncoding
```js
currentEncoding = readlineSync.setEncoding([newEncoding])
```
Set the encoding method of input (user's response) and output (`prompt` method and `question` method). The default is `'utf8'`.
## Options
@ -124,7 +157,8 @@ An `options` Object can be specified to `prompt` method and `question` method. T
Type: Boolean
Default: `false`
If `true` is specified, echo back is avoided. It is used to hide the secret text (e.g. password) which is typed by user on screen.
If `true` is specified, echo back is avoided. It is used to hide the secret text (e.g. password) which is typed by user on screen. (See `setMask` method)
For example:
```js
@ -135,7 +169,7 @@ console.log('Login ...');
The typed text is not shown on screen.
```
PASSWORD :
PASSWORD :********
Login ...
```

BIN
cl_01.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.8 KiB

After

Width:  |  Height:  |  Size: 3.9 KiB

BIN
cl_02.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

View file

@ -34,7 +34,6 @@ function _readlineSync(options) { // options.display is string
var input = '', isEditable, displayInput;
function tryShell() {
console.warn('<tryShell>');
var res = _readlineShell(options);
if (res.error) { throw res.error; }
return res.input;
@ -66,29 +65,24 @@ function _readlineSync(options) { // options.display is string
if (process.stdin.isTTY) {
fdR = process.stdin.fd;
ttyR = process.stdin._handle;
console.warn('OPENED: STDIN');
} else {
try {
// The stream by fs.openSync('\\\\.\\CON', 'r') can't switch to raw mode.
// 'CONIN$' might fail on XP, 2000, 7 (x86).
fdR = getFsB().open('CONIN$', constants.O_RDWR, parseInt('0666', 8));
ttyR = new TTY(fdR, true);
console.warn('OPENED: CONIN$');
} catch (e) {}
}
if (process.stdout.isTTY) {
fdW = process.stdout.fd;
console.warn('OPENED: STDOUT');
} else {
try {
fdW = fs.openSync('\\\\.\\CON', 'w');
console.warn('OPENED: \\\\.\\CON(W)');
} catch (e) {}
if (typeof fdW !== 'number') { // Retry
try {
fdW = getFsB().open('CONOUT$', constants.O_RDWR, parseInt('0666', 8));
console.warn('OPENED: CONOUT$');
} catch (e) {}
}
}
@ -98,24 +92,20 @@ function _readlineSync(options) { // options.display is string
try {
fdR = fs.openSync('/dev/tty', 'r'); // device file, not process.stdin
ttyR = process.stdin._handle;
console.warn('OPENED: /dev/tty(R) and STDIN handle');
} catch (e) {}
} else {
// Node v0.12 read() fails.
try {
fdR = fs.openSync('/dev/tty', 'r');
ttyR = new TTY(fdR, false);
console.warn('OPENED: /dev/tty(R)');
} catch (e) {}
}
if (process.stdout.isTTY) {
fdW = process.stdout.fd;
console.warn('OPENED: STDOUT');
} else {
try {
fdW = fs.openSync('/dev/tty', 'w');
console.warn('OPENED: /dev/tty(W)');
} catch (e) {}
}
}
@ -147,9 +137,6 @@ function _readlineSync(options) { // options.display is string
}
buffer = new Buffer((reqSize = options.keyIn ? 1 : bufSize));
console.warn('<<isEditable>>: ' + isEditable);
console.warn('--------');
while (true) {
readSize = 0;
@ -162,7 +149,6 @@ function _readlineSync(options) { // options.display is string
return;
}
console.warn('readSize: ' + readSize);
if (readSize === 0) { break; }
chunk = buffer.toString(encoding, 0, readSize);
// other ctrl-chars
@ -176,7 +162,6 @@ function _readlineSync(options) { // options.display is string
}
input += chunk;
console.warn('<<input>>: ' + input);
if (/[\r\n]$/.test(input) ||
options.keyIn && input.length >= reqSize) { break; }
}
@ -358,6 +343,13 @@ exports.setEncoding = function(newEncoding) {
return encoding;
};
exports.setMask = function(newMask) {
if (typeof newMask === 'string') {
mask = newMask;
}
return mask;
};
exports.setBufferSize = function(newBufSize) {
newBufSize = parseInt(newBufSize, 10);
if (!isNaN(newBufSize) && typeof newBufSize === 'number') {

View file

@ -1,6 +1,6 @@
{
"name": "readline-sync",
"version": "0.7.9",
"version": "0.8.0",
"title": "readlineSync",
"description": "Synchronous Readline for interactively running to have a conversation with the user via a console(TTY).",
"keywords": [