diff --git a/README.md b/README.md index 6c3862a..03b3ad7 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,7 @@ readlineSync.setPrompt({ toString: function() { return '[' + require('path').basename(process.cwd()) + ']# '; // Get and show current directory. } -}) +}); ``` ### setPrint diff --git a/lib/readline-sync.js b/lib/readline-sync.js index af0884e..be94c6d 100644 --- a/lib/readline-sync.js +++ b/lib/readline-sync.js @@ -23,7 +23,7 @@ var prompt: '> ', noEchoBack: false, mask: '*', - limit: '', + limit: [], caseSensitive: false, noTrim: false, encoding: 'utf8', @@ -36,7 +36,7 @@ var extHostPath, extHostArgs, tempdir, salt = 0; /* - display: other (to string) + display: string keyIn: boolean noEchoBack: boolean mask: string @@ -45,8 +45,8 @@ var noTrim: boolean encoding, bufferSize, print */ -function _readlineSync(options) { - var input = '', displaySave = (options.display += ''), +function _readlineSync(options) {return options; + var input = '', displaySave = options.display, silent = !options.display && options.keyIn && options.noEchoBack && !options.mask; function tryExt() { @@ -414,7 +414,6 @@ function margeOptions() { // _readlineSync defaultOptions // string case 'mask': // * * - case 'limit': // * * case 'encoding': // * * /* jshint eqnull:true */ options[optionName] = value != null ? value + '' : ''; @@ -436,9 +435,19 @@ function margeOptions() { case 'print': // * * options[optionName] = typeof value === 'function' ? value : void 0; break; + // array + case 'limit': // * * readlineExt +/* jshint eqnull:true */ + options[optionName] = value != null ? + flattenArray(value, function(value) { + return typeof value === 'string' || typeof value === 'number' || + value instanceof RegExp; + }) : []; +/* jshint eqnull:false */ + break; // other case 'prompt': // * - case 'display': // * + case 'display': // * readlineExt /* jshint eqnull:true */ options[optionName] = value != null ? value : ''; /* jshint eqnull:false */ @@ -466,30 +475,37 @@ function flattenArray(array, validate) { exports._useExtSet = function(use) { useExt = use; }; exports.prompt = function(options) { - var readOptions = margeOptions(true, options); - readOptions.display = readOptions.prompt; - return _readlineSync(readOptions); + var readOptions = margeOptions(true, options), + limit = readOptions.limit, res; + readOptions.display = readOptions.prompt + ''; + readOptions.limit = ''; // for readlineExt + res = _readlineSync(readOptions); + return res; }; exports.question = function(query, options) { var readOptions = margeOptions(margeOptions(true, options), { display: query - }); - return _readlineSync(readOptions); + }), + limit = readOptions.limit, res; + readOptions.display += ''; + readOptions.limit = ''; // for readlineExt + res = _readlineSync(readOptions); + return res; }; exports.keyIn = function(query, options) { - var limit = options ? - flattenArray(options.limit, function(value) - { return typeof value === 'string' || typeof value === 'number'; }) - .join('').replace(/\n/g, '').replace(/[^A-Za-z0-9_ ]/g, '\\$&') : '', - readOptions = margeOptions(margeOptions(true, options), { + var readOptions = margeOptions(margeOptions(true, options), { display: query, keyIn: true, - limit: limit, noTrim: true - }); - return _readlineSync(readOptions); + }), res; + readOptions.display += ''; + readOptions.limit = readOptions.limit.filter(function(value) + { return typeof value === 'string' || typeof value === 'number'; }) + .join('').replace(/\n/g, '').replace(/[^A-Za-z0-9_ ]/g, '\\$&'); + res = _readlineSync(readOptions); + return res; }; exports.setDefault = function(options) {