From 526e11c7e2687627bfa4bfb55f6540d04e36252c Mon Sep 17 00:00:00 2001 From: anseki Date: Sat, 12 Jul 2014 06:25:59 +0900 Subject: [PATCH] Add setPrint(). --- README.md | 32 ++++++++++++++++++++++++++++++++ cl_01.png | Bin 0 -> 3920 bytes lib/readline-sync.js | 9 +++++++-- package.json | 2 +- 4 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 cl_01.png 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 0000000000000000000000000000000000000000..bdf9cd33f00bed7f6e118e72720a56a9137f7ff2 GIT binary patch literal 3920 zcmZ8kc{J2-)c>kXQ-shg@b3gaSm>B7>of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C4wC8>bRnvXlt8%$}#Lt|dEP;k4L7p`Qc0 z@(UgT4$*oaaJh}Qw|(QuV`M)SEF(a{jB>X3ReX?`4XPu*QYb?GM*QV=LU5b5QNft- zqm7ZN1ms-<{!=A;vvbZIATetR&@=ARj& z>C&9{@r?E74D;C|ih`q~IH|8QB2NV~tVz-V5Ovk($j+g-mdpm$U$?!cYo?SxO*IPV zJo%xo`(0Jw&u{#Dx;yqU(talIU>`C2r8tUW<>O*HdC>A@$N+}WRth)1p{rUMNw;oR zFo^r_erZO-K8yGQ$5oDt%bWp=tF)v|3^|4?W?nf`h<}odIgcLxyf2 zI=g1+Nd3=}ba+f>y~b+q`)~Y=`^Inl5A$V%Pj=NCR03~s0{Eqh6piU}|B|W1zxFmFc3qtb1n4t_aj7VM^P_na(8C&OmtwBp F{~w;cK9c|d literal 0 HcmV?d00001 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"