diff --git a/README.md b/README.md index 6cc05b5..1542abb 100644 --- a/README.md +++ b/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. diff --git a/cl_01.png b/cl_01.png new file mode 100644 index 0000000..bdf9cd3 Binary files /dev/null and b/cl_01.png differ diff --git a/lib/readline-sync.js b/lib/readline-sync.js index a44e6e6..873c452 100644 --- a/lib/readline-sync.js +++ b/lib/readline-sync.js @@ -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; diff --git a/package.json b/package.json index 817ae6a..a80b937 100644 --- a/package.json +++ b/package.json @@ -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"