Add setPrint().
This commit is contained in:
parent
dbb2b48c9a
commit
526e11c7e2
4 changed files with 40 additions and 3 deletions
32
README.md
32
README.md
|
@ -51,6 +51,37 @@ readlineSync.setEncoding(encoding)
|
|||
|
||||
Set the encoding method of input (user's response) and output (`prompt`). Defaults to 'utf8'.
|
||||
|
||||
### setPrint
|
||||
|
||||
```js
|
||||
readlineSync.setPrint(funcPrint)
|
||||
```
|
||||
|
||||
The specified Function is called when any texts are outputed (`prompt` and `question`). Two arguments the text which is outputed and `encoding` are passed. Defaults to `undefined`.
|
||||
|
||||
![sample](cl_01.png)
|
||||
|
||||
For example, this is used to pass plain texts to Logger, when prompt texts are colored.
|
||||
|
||||
```js
|
||||
var readlineSync = require('readline-sync');
|
||||
user, pw, cmd;
|
||||
require('colors');
|
||||
|
||||
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 + ': ', true);
|
||||
// Authorization ...
|
||||
console.log(('Welcome, ' + user + '!').green.bold);
|
||||
|
||||
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.
|
||||
If in those platforms, an error is thrown.
|
||||
|
@ -65,6 +96,7 @@ try {
|
|||
```
|
||||
|
||||
## Release History
|
||||
* 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.
|
||||
|
|
BIN
cl_01.png
Normal file
BIN
cl_01.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 3.8 KiB |
|
@ -15,12 +15,15 @@ var promptText = '> ',
|
|||
stdin = process.stdin,
|
||||
stdout = process.stdout,
|
||||
buffer = new Buffer(BUF_SIZE),
|
||||
useShell = true, tempdir;
|
||||
useShell = true, print, tempdir;
|
||||
|
||||
function _readlineSync(display) {
|
||||
var input = '', rsize, err;
|
||||
|
||||
if (display) { stdout.write(display, encoding); }
|
||||
if (display) {
|
||||
if (typeof print === 'function') { print(display, encoding); }
|
||||
stdout.write(display, encoding);
|
||||
}
|
||||
|
||||
stdin.resume();
|
||||
while (true) {
|
||||
|
@ -116,6 +119,8 @@ function getTempfile(name) {
|
|||
// for dev
|
||||
exports.useShellSet = function(use) { useShell = use; };
|
||||
|
||||
exports.setPrint = function(fnc) { print = fnc; };
|
||||
|
||||
exports.setPrompt = function(newPrompt) {
|
||||
if (typeof newPrompt === 'string') {
|
||||
promptText = newPrompt;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"name": "readline-sync",
|
||||
"description": "Synchronous Readline",
|
||||
"version": "0.2.5",
|
||||
"version": "0.3.0",
|
||||
"homepage": "https://github.com/anseki/readline-sync",
|
||||
"author": {
|
||||
"name": "anseki"
|
||||
|
|
Loading…
Reference in a new issue