limit trueValue falseValue accept function

This commit is contained in:
anseki 2015-04-11 12:11:52 +09:00
parent fd186300a1
commit d94e61388f
3 changed files with 275 additions and 88 deletions

View file

@ -2,7 +2,7 @@
Synchronous [Readline](http://nodejs.org/api/readline.html) for interactively running to have a conversation with the user via a console(TTY). Synchronous [Readline](http://nodejs.org/api/readline.html) for interactively running to have a conversation with the user via a console(TTY).
readlineSync tries to read and write a console, even when the input or output is redirected like `your-script <foo.txt >bar.log`. readlineSync tries to make your script have a conversation with the user via a console, even when the input/output is redirected like `your-script <foo.dat >bar.log`.
## Example ## Example
@ -11,19 +11,30 @@ var readlineSync = require('readline-sync');
var userName = readlineSync.question('May I have your name? :'); // Wait for user's response. var userName = readlineSync.question('May I have your name? :'); // Wait for user's response.
var favFood = readlineSync.question('Hi ' + userName + '! What is your favorite food? :'); var favFood = readlineSync.question('Hi ' + userName + '! What is your favorite food? :');
console.log('Oh, ' + userName + ' likes ' + favFood + '!'); console.log('Oh, ' + userName + ' likes ' + favFood + '!');
``` ```
```console
May I have your name? :CookieMonster
Hi CookieMonster! What is your favorite food? :tofu
Oh, CookieMonster likes tofu!
``` ```
May I have your name? :AnSeki
Hi AnSeki! What is your favorite food? :chocolate ```js
Oh, AnSeki likes chocolate! var readlineSync = require('readline-sync');
// The user does not have to press an Enter key.
if (readlineSync.keyInYN('Do you want this module?')) {
// 'Y' key was pressed.
installModule();
} else {
searchAnother();
}
``` ```
## Installation ## Installation
``` ```shell
npm install readline-sync npm install readline-sync
``` ```
@ -184,7 +195,7 @@ console.log('Login ...');
The typed text is not shown on screen. The typed text is not shown on screen.
``` ```console
PASSWORD :******** PASSWORD :********
Login ... Login ...
``` ```
@ -216,7 +227,7 @@ if (readlineSync.keyIn('Are you sure? :', {limit: 'yn'}) === 'y') { // Accept 'y
Type: Boolean Type: Boolean
Default: `false` Default: `false`
By default, the matching is case-insensitive when `limit` option (see [limit](#limit) option) is specified (i.e. `a` equals `A`). If `true` is specified, the matching is case-sensitive (i.e. `a` and `A` are different). By default, the matching is case-insensitive when `limit` option (see [limit](#limit) option) is specified (i.e. `a` equals `A`). If `true` is specified, the matching is case-sensitive (i.e. `a` is different from `A`).
### noTrim ### noTrim

View file

@ -4,7 +4,7 @@
# Copyright (c) 2015 anseki # Copyright (c) 2015 anseki
# Licensed under the MIT license. # Licensed under the MIT license.
# Use perl for compatibility of sed/awk of GNU / POSIX, BSD. (tr too, maybe) # Use perl for compatibility of sed/awk of GNU / POSIX, BSD. (and tr)
# Hide "\n" from shell by "\fNL" # Hide "\n" from shell by "\fNL"
decode_arg() { decode_arg() {

View file

@ -33,10 +33,10 @@ var
trueValue: [], trueValue: [],
falseValue: [] falseValue: []
}, },
useExt = false,
fdR = 'none', fdW, ttyR, isRawMode = false, fdR = 'none', fdW, ttyR, isRawMode = false,
extHostPath, extHostArgs, tempdir, salt = 0; extHostPath, extHostArgs, tempdir, salt = 0,
_DBG_useExt = false, _DBG_checkOptions = false, _DBG_checkMethod = false;
/* /*
display: string display: string
@ -59,6 +59,8 @@ function _readlineSync(options) {
return res.input; return res.input;
} }
if (_DBG_checkOptions) { _DBG_checkOptions(options); }
(function() { // open TTY (function() { // open TTY
var fsB, constants; var fsB, constants;
@ -136,7 +138,7 @@ function _readlineSync(options) {
return true; return true;
} }
if (useExt || !ttyR || if (_DBG_useExt || !ttyR ||
typeof fdW !== 'number' && (options.display || !isCooked)) { typeof fdW !== 'number' && (options.display || !isCooked)) {
input = tryExt(); input = tryExt();
return; return;
@ -235,6 +237,7 @@ function readlineExt(options) {
if (childProc.execFileSync) { if (childProc.execFileSync) {
hostArgs = getHostArgs(options); hostArgs = getHostArgs(options);
if (_DBG_checkMethod) { _DBG_checkMethod('execFileSync', hostArgs); }
try { try {
res.input = childProc.execFileSync(extHostPath, hostArgs, execOptions); res.input = childProc.execFileSync(extHostPath, hostArgs, execOptions);
} catch (e) { // non-zero exit code } catch (e) { // non-zero exit code
@ -258,7 +261,7 @@ function readlineExt(options) {
return res; return res;
} }
// piping via files (node v0.10-) // piping via files (for Node v0.10-)
function _execFileSync(options, execOptions) { function _execFileSync(options, execOptions) {
function getTempfile(name) { function getTempfile(name) {
@ -322,6 +325,7 @@ function _execFileSync(options, execOptions) {
' >"' + pathStdout + '"' + ' >"' + pathStdout + '"' +
'; echo 1 >"' + pathDone + '"']; '; echo 1 >"' + pathDone + '"'];
} }
if (_DBG_checkMethod) { _DBG_checkMethod('_execFileSync', hostArgs); }
try { try {
childProc.spawn(shellPath, shellArgs, execOptions); childProc.spawn(shellPath, shellArgs, execOptions);
} catch (e) { } catch (e) {
@ -357,7 +361,7 @@ function _execFileSync(options, execOptions) {
} }
function getHostArgs(options) { function getHostArgs(options) {
// To send any text to crazy Windows shell safely. // Send any text to crazy Windows shell safely.
function encodeArg(arg) { function encodeArg(arg) {
return arg.replace(/[^\w\u0080-\uFFFF]/g, function(chr) { return arg.replace(/[^\w\u0080-\uFFFF]/g, function(chr) {
return '#' + chr.charCodeAt(0) + ';'; return '#' + chr.charCodeAt(0) + ';';
@ -388,19 +392,20 @@ function getHostArgs(options) {
function flattenArray(array, validator) { function flattenArray(array, validator) {
var flatArray = []; var flatArray = [];
function parseArray(array) { function _flattenArray(array) {
/* jshint eqnull:true */ /* jshint eqnull:true */
if (array == null) { return; } if (array == null) { return; }
/* jshint eqnull:false */ /* jshint eqnull:false */
else if (Array.isArray(array)) { array.forEach(parseArray); } else if (Array.isArray(array)) { array.forEach(_flattenArray); }
else if (!validator || validator(array)) { flatArray.push(array); } else if (!validator || validator(array)) { flatArray.push(array); }
} }
parseArray(array); _flattenArray(array);
return flatArray; return flatArray;
} }
// margeOptions(options1, options2 ... ) // margeOptions(options1, options2 ... )
// margeOptions(true, options1, options2 ... ) // from defaultOptions // margeOptions(true, options1, options2 ... )
// arg1=true : Start from defaultOptions and pick elements of that.
function margeOptions() { function margeOptions() {
var optionsList = Array.prototype.slice.call(arguments), var optionsList = Array.prototype.slice.call(arguments),
optionNames, fromDefault; optionNames, fromDefault;
@ -419,11 +424,13 @@ function margeOptions() {
/* jshint eqnull:false */ /* jshint eqnull:false */
// ======== DEPRECATED ======== // ======== DEPRECATED ========
if (optionsPart.hasOwnProperty('noEchoBack')) { if (optionsPart.hasOwnProperty('noEchoBack') &&
!optionsPart.hasOwnProperty('hideEchoBack')) {
optionsPart.hideEchoBack = optionsPart.noEchoBack; optionsPart.hideEchoBack = optionsPart.noEchoBack;
delete optionsPart.noEchoBack; delete optionsPart.noEchoBack;
} }
if (optionsPart.hasOwnProperty('noTrim')) { if (optionsPart.hasOwnProperty('noTrim') &&
!optionsPart.hasOwnProperty('keepWhitespace')) {
optionsPart.keepWhitespace = optionsPart.noTrim; optionsPart.keepWhitespace = optionsPart.noTrim;
delete optionsPart.noTrim; delete optionsPart.noTrim;
} }
@ -450,7 +457,7 @@ function margeOptions() {
// ================ number // ================ number
case 'bufferSize': // * * case 'bufferSize': // * *
if (!isNaN(value = parseInt(value, 10)) && typeof value === 'number') if (!isNaN(value = parseInt(value, 10)) && typeof value === 'number')
{ options[optionName] = value; } { options[optionName] = value; } // limited updating (number is needed)
break; break;
// ================ boolean // ================ boolean
case 'hideEchoBack': // * * case 'hideEchoBack': // * *
@ -467,14 +474,11 @@ function margeOptions() {
case 'limit': // * * readlineExt case 'limit': // * * readlineExt
case 'trueValue': // * case 'trueValue': // *
case 'falseValue': // * case 'falseValue': // *
/* jshint eqnull:true */ options[optionName] = flattenArray(value, function(value) {
value = value != null ? var type = typeof value;
flattenArray(value, function(value) { return type === 'string' || type === 'number' ||
return typeof value === 'string' || typeof value === 'number' || type === 'function' || value instanceof RegExp;
value instanceof RegExp; }).map(function(value) {
}) : [];
/* jshint eqnull:false */
options[optionName] = value.map(function(value) {
return typeof value === 'string' ? value.replace(/[\r\n]/g, '') : value; return typeof value === 'string' ? value.replace(/[\r\n]/g, '') : value;
}); });
break; break;
@ -493,11 +497,11 @@ function margeOptions() {
function isMatched(res, comps, caseSensitive) { function isMatched(res, comps, caseSensitive) {
return comps.some(function(comp) { return comps.some(function(comp) {
if (typeof comp === 'number') { comp += ''; } var type = typeof comp;
return (typeof comp === 'string' ? ( if (type === 'number') { comp += ''; }
caseSensitive ? return (type === 'string' ?
res === comp : res.toLowerCase() === comp.toLowerCase() (caseSensitive ? res === comp : res.toLowerCase() === comp.toLowerCase()) :
) : type === 'function' ? comp(res) :
comp instanceof RegExp ? comp.test(res) : false); comp instanceof RegExp ? comp.test(res) : false);
}); });
} }
@ -511,8 +515,51 @@ function replacePlaceholder(text, generator) {
}); });
} }
function array2charlist(array, caseSensitive, collectSymbols) {
var values, group = [], groupClass = -1, charCode = 0, symbols = '', suppressed;
function addGroup(groups, group) {
if (group.length > 3) { // ellipsis
groups.push(group[0] + '...' + group[group.length - 1]);
suppressed = true;
} else if (group.length) {
groups = groups.concat(group);
}
return groups;
}
values = array.reduce(function(chars, value)
{ return chars.concat((value + '').split('')); }, [])
.reduce(function(groups, curChar) {
var curGroupClass, curCharCode;
if (!caseSensitive) { curChar = curChar.toUpperCase(); }
curGroupClass = /^\d$/.test(curChar) ? 1 :
/^[A-Z]$/.test(curChar) ? 2 : /^[a-z]$/.test(curChar) ? 3 : 0;
if (collectSymbols && curGroupClass === 0) {
symbols += curChar;
} else {
curCharCode = curChar.charCodeAt(0);
if (curGroupClass && curGroupClass === groupClass &&
curCharCode === charCode + 1) {
group.push(curChar);
} else {
groups = addGroup(groups, group);
group = [curChar];
groupClass = curGroupClass;
}
charCode = curCharCode;
}
return groups;
}, []);
values = addGroup(values, group); // last group
if (symbols) { values.push(symbols); suppressed = true; }
return {values: values, suppressed: suppressed};
}
function joinChunks(chunks, suppressed)
{ return chunks.join(chunks.length > 2 ? ', ' : suppressed ? ' / ' : '/'); }
function placeholderInMessage(param, options) { function placeholderInMessage(param, options) {
var text, values, group = [], groupClass = -1, charCode = 0, suppressed; var text, values, resCharlist = {};
switch (param) { switch (param) {
case 'hideEchoBack': case 'hideEchoBack':
case 'mask': case 'mask':
@ -533,42 +580,15 @@ function placeholderInMessage(param, options) {
case 'falseValue': case 'falseValue':
values = options[options.hasOwnProperty(param + 'Src') ? param + 'Src' : param]; values = options[options.hasOwnProperty(param + 'Src') ? param + 'Src' : param];
if (options.keyIn) { // suppress if (options.keyIn) { // suppress
values = values.reduce(function(chars, value) { resCharlist = array2charlist(values, options.caseSensitive);
return chars.concat((value + '').split('')); values = resCharlist.values;
}, [])
.reduce(function(groups, curChar) {
var curGroupClass, curCharCode;
if (!options.caseSensitive) { curChar = curChar.toUpperCase(); }
curGroupClass = /^\d$/.test(curChar) ? 1 :
/^[A-Z]$/.test(curChar) ? 2 : /^[a-z]$/.test(curChar) ? 3 : 0;
curCharCode = curChar.charCodeAt(0);
if (curGroupClass && curGroupClass === groupClass &&
curCharCode === charCode + 1) {
group.push(curChar);
} else { } else {
if (group.length > 3) { // ellipsis values = values.filter(function(value) {
groups.push(group[0] + ' ... ' + group[group.length - 1]); var type = typeof value;
suppressed = true; return type === 'string' || type === 'number';
} else { });
groups = groups.concat(group);
} }
group = [curChar]; text = joinChunks(values, resCharlist.suppressed);
groupClass = curGroupClass;
}
charCode = curCharCode;
return groups;
}, []);
if (group.length > 3) { // ellipsis
values.push(group[0] + ' ... ' + group[group.length - 1]);
suppressed = true;
} else {
values = values.concat(group);
}
} else {
values = values.filter(function(value)
{ return typeof value === 'string' || typeof value === 'number'; });
}
text = values.join(values.length > 2 ? ', ' : suppressed ? ' / ' : '/');
break; break;
case 'limitCount': case 'limitCount':
case 'limitCountNotZero': case 'limitCountNotZero':
@ -579,6 +599,17 @@ function placeholderInMessage(param, options) {
return text; return text;
} }
function placeholderCharlist(param) {
var matches = /^(.)-(.)$/.exec(param), text = '', from, to, code, step;
if (!matches) { return; }
from = matches[1].charCodeAt(0);
to = matches[2].charCodeAt(0);
step = from < to ? 1 : -1;
for (code = from; code !== to + step; code += step)
{ text += String.fromCharCode(code); }
return text;
}
function readlineWithOptions(options) { function readlineWithOptions(options) {
var res, var res,
generator = function(param) { return placeholderInMessage(param, options); }; generator = function(param) { return placeholderInMessage(param, options); };
@ -608,7 +639,9 @@ function toBool(res, options) {
} }
// for dev // for dev
exports._useExtSet = function(use) { useExt = use; }; exports._DBG_set_useExt = function(val) { _DBG_useExt = val; };
exports._DBG_set_checkOptions = function(val) { _DBG_checkOptions = val; };
exports._DBG_set_checkMethod = function(val) { _DBG_checkMethod = val; };
exports.setDefault = function(options) { exports.setDefault = function(options) {
defaultOptions = margeOptions(true, options); defaultOptions = margeOptions(true, options);
@ -638,27 +671,19 @@ exports.keyIn = function(query, options) {
}), res; }), res;
// char list // char list
readOptions.limitSrc = readOptions.limit.filter(function(value) readOptions.limitSrc = readOptions.limit.filter(function(value) {
{ return typeof value === 'string' || typeof value === 'number'; }) var type = typeof value;
.map(function(text) { // placeholders return type === 'string' || type === 'number';
return replacePlaceholder(text + '', function(param) { // char list })
var matches = /^(.)-(.)$/.exec(param), text = '', from, to, code, step; .map(function(text) { return replacePlaceholder(text + '', placeholderCharlist); });
if (!matches) { return; }
from = matches[1].charCodeAt(0);
to = matches[2].charCodeAt(0);
step = from < to ? 1 : -1;
for (code = from; code !== to + step; code += step)
{ text += String.fromCharCode(code); }
return text;
});
});
// pattern // pattern
readOptions.limit = readOptions.limitSrc.join('').replace(/[^A-Za-z0-9_ ]/g, '\\$&'); readOptions.limit = readOptions.limitSrc.join('').replace(/[^A-Za-z0-9_ ]/g, '\\$&');
['trueValue', 'falseValue'].forEach(function(optionName) { ['trueValue', 'falseValue'].forEach(function(optionName) {
var comps = []; var comps = [];
readOptions[optionName].forEach(function(comp) { readOptions[optionName].forEach(function(comp) {
if (typeof comp === 'string' || typeof comp === 'number') { var type = typeof comp;
if (type === 'string' || type === 'number') {
comps = comps.concat((comp + '').split('')); comps = comps.concat((comp + '').split(''));
} else if (comp instanceof RegExp) { } else if (comp instanceof RegExp) {
comps.push(comp); comps.push(comp);
@ -671,10 +696,161 @@ exports.keyIn = function(query, options) {
function(param) { return placeholderInMessage(param, readOptions); }); function(param) { return placeholderInMessage(param, readOptions); });
res = _readlineSync(readOptions); res = _readlineSync(readOptions);
return toBool(res, readOptions); return toBool(res, readOptions);
}; };
// ------------------------------------
exports.questionEMail = function(query, options) {
/* jshint eqnull:true */
if (query == null) { query = 'Input e-mail address :'; }
/* jshint eqnull:false */
return exports.question(query, margeOptions({
// -------- default
hideEchoBack: false,
// http://www.w3.org/TR/html5/forms.html#valid-e-mail-address
limit: /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/,
limitMessage: 'Input valid e-mail address, please.',
trueValue: null,
falseValue: null
}, options, {
// -------- forced
keepWhitespace: false,
}));
};
exports.questionNewPassword = function(query, options) {
var readOptions = margeOptions({
// -------- default
hideEchoBack: true,
mask: '*',
limitMessage: 'It can include: ${charlist}, the length able to be: ${length}',
caseSensitive: true,
trueValue: null,
falseValue: null,
confirm: 'Reinput same one to confirm it :'
}, options, {/* forced limit */}),
// added: charlist, min, max, confirm
charlist, min, max, resCharlist, res1, res2, limit1, limitMessage1;
/* jshint eqnull:true */
if (query == null) { query = 'Input new password :'; }
/* jshint eqnull:false */
charlist = options && options.charlist ? options.charlist + '' : '${!-~}';
charlist = replacePlaceholder(charlist, placeholderCharlist);
if (options) { min = options.min; max = options.max; }
if (isNaN(min = parseInt(min, 10)) || typeof min !== 'number') { min = 12; }
if (isNaN(max = parseInt(max, 10)) || typeof max !== 'number') { max = 24; }
limit1 = new RegExp(
'^[' + charlist.replace(/[^A-Za-z0-9_ ]/g, '\\$&') + ']{' + min + ',' + max + '}$');
if (readOptions.limitMessage) {
resCharlist = array2charlist([charlist], readOptions.caseSensitive, true);
resCharlist.text = joinChunks(resCharlist.values, resCharlist.suppressed);
limitMessage1 = replacePlaceholder(readOptions.limitMessage,
function(param) {
return param === 'charlist' ? resCharlist.text :
param === 'length' ? min + '...' + max : null;
});
}
while (!res2) {
readOptions.limit = limit1;
readOptions.limitMessage = limitMessage1;
res1 = exports.question(query, readOptions);
readOptions.limit = [res1, ''];
readOptions.limitMessage = 'Two passwords don\'t match.' +
' Hit only Enter key if you want to retry from first password.';
res2 = exports.question(options.confirm, readOptions);
}
return res1;
};
function _keyInYN(query, options, limit) {
var readOptions = margeOptions(options, {
// -------- forced
hideEchoBack: false,
limit: limit,
caseSensitive: false,
trueValue: 'y',
falseValue: 'n'
}), res;
/* jshint eqnull:true */
if (query == null) { query = 'Are you sure? :'; }
/* jshint eqnull:false */
if ((query += '') && options.keyGuide !== false)
{ query = query.replace(/\s*:?\s*$/, '') + ' [Y/N] :'; }
res = exports.keyIn(query, readOptions);
if (typeof res !== 'boolean') { res = ''; }
return res;
}
exports.keyInYN = function(query, options) { return _keyInYN(query, options); };
exports.keyInYNStrict = function(query, options)
{ return _keyInYN(query, options, 'yn'); };
exports.keyInPause = function(query, options) {
var readOptions = margeOptions(options, {
// -------- forced
hideEchoBack: true,
mask: ''
});
/* jshint eqnull:true */
if (query == null) { query = 'Continue...'; }
/* jshint eqnull:false */
if ((query += '') && options.keyGuide !== false)
{ query = query.replace(/\s+$/, '') + ' (Hit any key)'; }
exports.keyIn(query, readOptions);
return;
};
exports.keyInSelect = function(query, items, options) {
var readOptions = margeOptions({
// -------- default
hideEchoBack: false,
}, options, {
// -------- forced
caseSensitive: false,
trueValue: null,
falseValue: null
}), res, keylist = '', key2i = {}, charCode = 49 /* '1' */, display = '\n';
if (!Array.isArray(items) || items.length > 35)
{ throw '`items` must be Array (max length: 35).'; }
items.forEach(function(item, i) {
var key = String.fromCharCode(charCode);
keylist += key;
key2i[key] = i;
display += '[' + key + '] ' + item.trim() + '\n';
charCode = charCode === 57 /* '9' */ ? 65 /* 'A' */ : charCode + 1;
});
if (options.cancel !== false) {
keylist += '0';
key2i['0'] = -1;
display += '[' + '0' + '] CANCEL\n';
}
readOptions.limit = keylist;
display += '\n';
/* jshint eqnull:true */
if (query == null) { query = 'Choose one from list :'; }
/* jshint eqnull:false */
if ((query += '')) {
if (options.keyGuide !== false)
{ query = query.replace(/\s*:?\s*$/, '') + ' [${limit}] :'; }
display += query;
}
res = exports.keyIn(display, readOptions);
return key2i[res.toUpperCase()];
};
// ======== DEPRECATED ======== // ======== DEPRECATED ========
function _setOption(optionName, args) { function _setOption(optionName, args) {
var options; var options;