Add displayOnly

This commit is contained in:
anseki 2015-04-13 15:55:33 +09:00
parent d94e61388f
commit bdd72b0037
5 changed files with 219 additions and 99 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 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`. readlineSync tries to let 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

View file

@ -13,7 +13,7 @@ var
PS_MSG = 'Microsoft Windows PowerShell is required.' + PS_MSG = 'Microsoft Windows PowerShell is required.' +
' https://technet.microsoft.com/en-us/library/hh847837.aspx', ' https://technet.microsoft.com/en-us/library/hh847837.aspx',
input, fso, tty, input = '', fso, tty,
options = (function(conf) { options = (function(conf) {
var options = {}, arg, args =// Array.prototype.slice.call(WScript.Arguments), var options = {}, arg, args =// Array.prototype.slice.call(WScript.Arguments),
(function() { (function() {
@ -53,6 +53,7 @@ var
return options; return options;
})({ })({
display: 'string', display: 'string',
displayOnly: 'boolean',
keyIn: 'boolean', keyIn: 'boolean',
hideEchoBack: 'boolean', hideEchoBack: 'boolean',
mask: 'string' mask: 'string'
@ -60,10 +61,10 @@ var
if (!options.hideEchoBack && !options.keyIn) { if (!options.hideEchoBack && !options.keyIn) {
if (options.display) { writeTTY(options.display); } if (options.display) { writeTTY(options.display); }
input = readByFSO(); if (!options.displayOnly) { input = readByFSO(); }
} else if (options.hideEchoBack && !options.keyIn && !options.mask) { } else if (options.hideEchoBack && !options.keyIn && !options.mask) {
if (options.display) { writeTTY(options.display); } if (options.display) { writeTTY(options.display); }
input = readByPW(); if (!options.displayOnly) { input = readByPW(); }
} else { } else {
WScript.StdErr.WriteLine(PS_MSG); WScript.StdErr.WriteLine(PS_MSG);
WScript.Quit(1); WScript.Quit(1);

View file

@ -6,6 +6,7 @@
Param( Param(
[string] $display, [string] $display,
[switch] $displayOnly,
[switch] $keyIn, [switch] $keyIn,
[switch] $hideEchoBack, [switch] $hideEchoBack,
[string] $mask, [string] $mask,
@ -25,7 +26,7 @@ function decodeArg ($arg) {
} }
$options = @{} $options = @{}
foreach ($arg in @('display', 'keyIn', 'hideEchoBack', 'mask', 'limit', 'caseSensitive')) { foreach ($arg in @('display', 'displayOnly', 'keyIn', 'hideEchoBack', 'mask', 'limit', 'caseSensitive')) {
$options.Add($arg, (Get-Variable $arg -ValueOnly)) $options.Add($arg, (Get-Variable $arg -ValueOnly))
} }
$argList = New-Object string[] $options.Keys.Count $argList = New-Object string[] $options.Keys.Count
@ -63,6 +64,7 @@ function writeTTY ($text) {
if ($options.display) { if ($options.display) {
writeTTY $options.display writeTTY $options.display
} }
if ($options.displayOnly) { return "''" }
if (-not $options.keyIn -and $options.hideEchoBack -and $options.mask -eq '*') { if (-not $options.keyIn -and $options.hideEchoBack -and $options.mask -eq '*') {
$inputTTY = execWithTTY ('$text = Read-Host -AsSecureString;' + $inputTTY = execWithTTY ('$text = Read-Host -AsSecureString;' +

View file

@ -16,6 +16,7 @@ while [ $# -ge 1 ]; do
arg="$(printf '%s' "$1" | grep -E '^-+[^-]+$' | tr '[A-Z]' '[a-z]' | tr -d '-')" arg="$(printf '%s' "$1" | grep -E '^-+[^-]+$' | tr '[A-Z]' '[a-z]' | tr -d '-')"
case "$arg" in case "$arg" in
'display') shift; options_display="$(decode_arg "$1")";; 'display') shift; options_display="$(decode_arg "$1")";;
'displayOnly') options_displayOnly=true;;
'keyin') options_keyIn=true;; 'keyin') options_keyIn=true;;
'hideechoback') options_hideEchoBack=true;; 'hideechoback') options_hideEchoBack=true;;
'mask') shift; options_mask="$(decode_arg "$1")";; 'mask') shift; options_mask="$(decode_arg "$1")";;
@ -60,6 +61,10 @@ replace_allchars() { (
if [ -n "$options_display" ]; then if [ -n "$options_display" ]; then
write_tty "$options_display" write_tty "$options_display"
fi fi
if [ "$options_displayOnly" = true ]; then
printf "'%s'" ''
exit 0
fi
if [ "$is_cooked" = true ]; then if [ "$is_cooked" = true ]; then
stty --file=/dev/tty cooked 2>/dev/null || \ stty --file=/dev/tty cooked 2>/dev/null || \

View file

@ -25,21 +25,24 @@ var
mask: '*', mask: '*',
limit: [], limit: [],
limitMessage: 'Input another, please.${( [)limit(])}', limitMessage: 'Input another, please.${( [)limit(])}',
trueValue: [],
falseValue: [],
caseSensitive: false, caseSensitive: false,
keepWhitespace: false, keepWhitespace: false,
encoding: 'utf8', encoding: 'utf8',
bufferSize: 1024, bufferSize: 1024,
print: void 0, print: void 0,
trueValue: [], history: true
falseValue: []
}, },
fdR = 'none', fdW, ttyR, isRawMode = false, fdR = 'none', fdW, ttyR, isRawMode = false,
extHostPath, extHostArgs, tempdir, salt = 0, extHostPath, extHostArgs, tempdir, salt = 0,
lastInput = '', inputHistory = [],
_DBG_useExt = false, _DBG_checkOptions = false, _DBG_checkMethod = false; _DBG_useExt = false, _DBG_checkOptions = false, _DBG_checkMethod = false;
/* /*
display: string display: string
displayOnly: boolean
keyIn: boolean keyIn: boolean
hideEchoBack: boolean hideEchoBack: boolean
mask: string mask: string
@ -148,6 +151,7 @@ function _readlineSync(options) {
fs.writeSync(fdW, options.display); fs.writeSync(fdW, options.display);
options.display = ''; options.display = '';
} }
if (options.displayOnly) { return; }
if (!setRawMode(!isCooked)) { if (!setRawMode(!isCooked)) {
input = tryExt(); input = tryExt();
@ -173,8 +177,7 @@ function _readlineSync(options) {
} }
chunk = readSize > 0 ? buffer.toString(options.encoding, 0, readSize) : '\n'; chunk = readSize > 0 ? buffer.toString(options.encoding, 0, readSize) : '\n';
if (chunk && if (chunk && typeof(line = (chunk.match(/^(.*?)[\r\n]/) || [])[1]) === 'string') {
typeof(line = (chunk.match(/^(.*?)[\r\n]/) || [])[1]) === 'string') {
chunk = line; chunk = line;
atEol = true; atEol = true;
} }
@ -202,13 +205,15 @@ function _readlineSync(options) {
setRawMode(false); setRawMode(false);
})(); })();
if (options.print && !silent) { // must at least write '\n' if (options.print && !silent) {
options.print(displaySave + (options.hideEchoBack ? options.print(displaySave + (options.displayOnly ? '' :
(new Array(input.length + 1)).join(options.mask) : input) + '\n', (options.hideEchoBack ? (new Array(input.length + 1)).join(options.mask)
: input) + '\n'), // must at least write '\n'
options.encoding); options.encoding);
} }
return (options.keepWhitespace || options.keyIn ? input : input.trim()); return options.displayOnly ? '' :
(lastInput = options.keepWhitespace || options.keyIn ? input : input.trim());
} }
function readlineExt(options) { function readlineExt(options) {
@ -382,6 +387,7 @@ function getHostArgs(options) {
return args; return args;
})({ })({
display: 'string', display: 'string',
displayOnly: 'boolean',
keyIn: 'boolean', keyIn: 'boolean',
hideEchoBack: 'boolean', hideEchoBack: 'boolean',
mask: 'string', mask: 'string',
@ -393,9 +399,9 @@ function getHostArgs(options) {
function flattenArray(array, validator) { function flattenArray(array, validator) {
var flatArray = []; var flatArray = [];
function _flattenArray(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(_flattenArray); } else if (Array.isArray(array)) { array.forEach(_flattenArray); }
else if (!validator || validator(array)) { flatArray.push(array); } else if (!validator || validator(array)) { flatArray.push(array); }
} }
@ -419,9 +425,9 @@ function margeOptions() {
} }
return optionsList.reduce(function(options, optionsPart) { return optionsList.reduce(function(options, optionsPart) {
/* jshint eqnull:true */ /* jshint eqnull:true */
if (optionsPart == null) { return options; } if (optionsPart == null) { return options; }
/* jshint eqnull:false */ /* jshint eqnull:false */
// ======== DEPRECATED ======== // ======== DEPRECATED ========
if (optionsPart.hasOwnProperty('noEchoBack') && if (optionsPart.hasOwnProperty('noEchoBack') &&
@ -445,33 +451,31 @@ function margeOptions() {
// _readlineSync defaultOptions // _readlineSync defaultOptions
// ================ string // ================ string
case 'mask': // * * case 'mask': // * *
case 'encoding': // * *
case 'limitMessage': // * case 'limitMessage': // *
/* jshint eqnull:true */ case 'encoding': // * *
/* jshint eqnull:true */
value = value != null ? value + '' : ''; value = value != null ? value + '' : '';
/* jshint eqnull:false */ /* jshint eqnull:false */
if (value && optionName === 'mask' || optionName === 'encoding') if (value && optionName === 'mask' || optionName === 'encoding')
{ value = value.replace(/[\r\n]/g, ''); } { value = value.replace(/[\r\n]/g, ''); }
options[optionName] = value; options[optionName] = value;
break; break;
// ================ number // ================ number(int)
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; } // limited updating (number is needed) { options[optionName] = value; } // limited updating (number is needed)
break; break;
// ================ boolean // ================ boolean
case 'displayOnly': // *
case 'keyIn': // *
case 'hideEchoBack': // * * case 'hideEchoBack': // * *
case 'caseSensitive': // * * case 'caseSensitive': // * *
case 'keepWhitespace': // * * case 'keepWhitespace': // * *
case 'keyIn': // * case 'history': // *
options[optionName] = !!value; options[optionName] = !!value;
break; break;
// ================ function
case 'print': // * *
options[optionName] = typeof value === 'function' ? value : void 0;
break;
// ================ array // ================ array
case 'limit': // * * readlineExt case 'limit': // * * to string for readlineExt
case 'trueValue': // * case 'trueValue': // *
case 'falseValue': // * case 'falseValue': // *
options[optionName] = flattenArray(value, function(value) { options[optionName] = flattenArray(value, function(value) {
@ -482,12 +486,16 @@ function margeOptions() {
return typeof value === 'string' ? value.replace(/[\r\n]/g, '') : value; return typeof value === 'string' ? value.replace(/[\r\n]/g, '') : value;
}); });
break; break;
// ================ function
case 'print': // * *
options[optionName] = typeof value === 'function' ? value : void 0;
break;
// ================ other // ================ other
case 'prompt': // * case 'prompt': // *
case 'display': // * readlineExt case 'display': // *
/* jshint eqnull:true */ /* jshint eqnull:true */
options[optionName] = value != null ? value : ''; options[optionName] = value != null ? value : '';
/* jshint eqnull:false */ /* jshint eqnull:false */
break; break;
} }
}); });
@ -498,11 +506,11 @@ function margeOptions() {
function isMatched(res, comps, caseSensitive) { function isMatched(res, comps, caseSensitive) {
return comps.some(function(comp) { return comps.some(function(comp) {
var type = typeof comp; var type = typeof comp;
if (type === 'number') { comp += ''; } return type === 'string' ?
return (type === 'string' ?
(caseSensitive ? res === comp : res.toLowerCase() === comp.toLowerCase()) : (caseSensitive ? res === comp : res.toLowerCase() === comp.toLowerCase()) :
type === 'number' ? parseFloat(res) === comp :
type === 'function' ? comp(res) : type === 'function' ? comp(res) :
comp instanceof RegExp ? comp.test(res) : false); comp instanceof RegExp ? comp.test(res) : false;
}); });
} }
@ -558,8 +566,8 @@ function array2charlist(array, caseSensitive, collectSymbols) {
function joinChunks(chunks, suppressed) function joinChunks(chunks, suppressed)
{ return chunks.join(chunks.length > 2 ? ', ' : suppressed ? ' / ' : '/'); } { return chunks.join(chunks.length > 2 ? ', ' : suppressed ? ' / ' : '/'); }
function placeholderInMessage(param, options) { function getPhContent(param, options) {
var text, values, resCharlist = {}; var text, values, resCharlist = {}, arg;
switch (param) { switch (param) {
case 'hideEchoBack': case 'hideEchoBack':
case 'mask': case 'mask':
@ -567,7 +575,7 @@ function placeholderInMessage(param, options) {
case 'keepWhitespace': case 'keepWhitespace':
case 'encoding': case 'encoding':
case 'bufferSize': case 'bufferSize':
case 'input': case 'history':
text = options.hasOwnProperty(param) ? options[param] + '' : ''; text = options.hasOwnProperty(param) ? options[param] + '' : '';
break; break;
case 'prompt': case 'prompt':
@ -593,13 +601,25 @@ function placeholderInMessage(param, options) {
case 'limitCount': case 'limitCount':
case 'limitCountNotZero': case 'limitCountNotZero':
text = options[options.hasOwnProperty('limitSrc') ? 'limitSrc' : 'limit'].length; text = options[options.hasOwnProperty('limitSrc') ? 'limitSrc' : 'limit'].length;
text = (text ? text : param === 'limitCountNotZero' ? '' : text) + ''; text = text || param !== 'limitCountNotZero' ? text + '' : '';
break; break;
case 'lastInput':
text = lastInput;
break;
case 'cwd':
case 'CWD':
text = process.cwd();
if (param === 'CWD') { text = require('path').basename(text); }
break;
default: // with arg
if (typeof(arg = (param.match(/^history_m(\d+)$/) || [])[1]) === 'string') {
text = inputHistory[inputHistory.length - arg] || '';
}
} }
return text; return text;
} }
function placeholderCharlist(param) { function getPhCharlist(param) {
var matches = /^(.)-(.)$/.exec(param), text = '', from, to, code, step; var matches = /^(.)-(.)$/.exec(param), text = '', from, to, code, step;
if (!matches) { return; } if (!matches) { return; }
from = matches[1].charCodeAt(0); from = matches[1].charCodeAt(0);
@ -610,24 +630,56 @@ function placeholderCharlist(param) {
return text; return text;
} }
function readlineWithOptions(options) { function readlineWithOptions(options, preCheck) {
var res, var res, forceNext, resCheck, limitMessage,
generator = function(param) { return placeholderInMessage(param, options); }; matches, histInput;
function _getPhContent(param) { return getPhContent(param, options); }
options.limitSrc = options.limit; options.limitSrc = options.limit;
options.displaySrc = options.display; options.displaySrc = options.display;
options.limit = ''; // for readlineExt options.limit = ''; // for readlineExt
options.display = replacePlaceholder(options.display + '', generator); options.display = replacePlaceholder(options.display + '', _getPhContent);
while (true) { while (true) {
res = _readlineSync(options); res = _readlineSync(options);
if (!options.limitSrc.length || forceNext = false;
isMatched(res, options.limitSrc, options.caseSensitive)) { break; } limitMessage = '';
options.input = res; // for placeholder
options.display += (options.display ? '\n' : '') + if (options.history) {
(options.limitMessage ? if ((matches = /^\s*\!(?:\!|-1)(:p)?\s*$/.exec(res))) { // `!!` `!-1` +`:p`
replacePlaceholder(options.limitMessage, generator) + '\n' : '') + histInput = inputHistory[0] || '';
replacePlaceholder(options.displaySrc + '', generator); if (matches[1]) { forceNext = true; } // only display
else { res = histInput; } // replace input
// Show it even if it is empty (NL only).
options.display +=
(/[^\n]$/.test(options.display) ? '\n' : '') + histInput + '\n';
if (!forceNext) { // Loop may break
options.displayOnly = true;
_readlineSync(options);
options.displayOnly = false;
}
} else if (res && res !== inputHistory[inputHistory.length - 1]) {
inputHistory = [res];
}
}
if (preCheck) {
resCheck = preCheck(res, options);
res = resCheck.res;
if (resCheck.forceNext) { forceNext = true; } // Don't switch to false.
}
if (!forceNext) {
if (!options.limitSrc.length ||
isMatched(res, options.limitSrc, options.caseSensitive)) { break; }
if (options.limitMessage)
{ limitMessage = replacePlaceholder(options.limitMessage, _getPhContent); }
}
options.display += (/[^\n]$/.test(options.display) ? '\n' : '') +
(limitMessage ? limitMessage + '\n' : '') +
replacePlaceholder(options.displaySrc + '', _getPhContent);
} }
return res; return toBool(res, options);
} }
function toBool(res, options) { function toBool(res, options) {
@ -642,25 +694,25 @@ function toBool(res, options) {
exports._DBG_set_useExt = function(val) { _DBG_useExt = val; }; exports._DBG_set_useExt = function(val) { _DBG_useExt = val; };
exports._DBG_set_checkOptions = function(val) { _DBG_checkOptions = val; }; exports._DBG_set_checkOptions = function(val) { _DBG_checkOptions = val; };
exports._DBG_set_checkMethod = function(val) { _DBG_checkMethod = val; }; exports._DBG_set_checkMethod = function(val) { _DBG_checkMethod = val; };
exports._DBG_clearHistory = function() { lastInput = ''; inputHistory = []; };
exports.setDefault = function(options) { exports.setDefault = function(options) {
defaultOptions = margeOptions(true, options); defaultOptions = margeOptions(true, options);
return margeOptions(true); // copy return margeOptions(true); // copy
}; };
// ------------------------------------
exports.prompt = function(options) { exports.prompt = function(options) {
var readOptions = margeOptions(true, options), res; var readOptions = margeOptions(true, options);
readOptions.display = readOptions.prompt; readOptions.display = readOptions.prompt;
res = readlineWithOptions(readOptions); return readlineWithOptions(readOptions);
return toBool(res, readOptions);
}; };
exports.question = function(query, options) { exports.question = function(query, options) {
var readOptions = margeOptions(margeOptions(true, options), { return readlineWithOptions(margeOptions(margeOptions(true, options), {
display: query display: query
}), }));
res = readlineWithOptions(readOptions);
return toBool(res, readOptions);
}; };
exports.keyIn = function(query, options) { exports.keyIn = function(query, options) {
@ -668,14 +720,14 @@ exports.keyIn = function(query, options) {
display: query, display: query,
keyIn: true, keyIn: true,
keepWhitespace: true keepWhitespace: true
}), res; });
// char list // char list
readOptions.limitSrc = readOptions.limit.filter(function(value) { readOptions.limitSrc = readOptions.limit.filter(function(value) {
var type = typeof value; var type = typeof value;
return type === 'string' || type === 'number'; return type === 'string' || type === 'number';
}) })
.map(function(text) { return replacePlaceholder(text + '', placeholderCharlist); }); .map(function(text) { return replacePlaceholder(text + '', getPhCharlist); });
// pattern // pattern
readOptions.limit = readOptions.limitSrc.join('').replace(/[^A-Za-z0-9_ ]/g, '\\$&'); readOptions.limit = readOptions.limitSrc.join('').replace(/[^A-Za-z0-9_ ]/g, '\\$&');
@ -693,18 +745,17 @@ exports.keyIn = function(query, options) {
}); });
readOptions.display = replacePlaceholder(readOptions.display + '', readOptions.display = replacePlaceholder(readOptions.display + '',
function(param) { return placeholderInMessage(param, readOptions); }); function(param) { return getPhContent(param, readOptions); });
res = _readlineSync(readOptions); return toBool(_readlineSync(readOptions), readOptions);
return toBool(res, readOptions);
}; };
// ------------------------------------ // ------------------------------------
exports.questionEMail = function(query, options) { exports.questionEMail = function(query, options) {
/* jshint eqnull:true */ /* jshint eqnull:true */
if (query == null) { query = 'Input e-mail address :'; } if (query == null) { query = 'Input e-mail address :'; }
/* jshint eqnull:false */ /* jshint eqnull:false */
return exports.question(query, margeOptions({ return exports.question(query, margeOptions({
// -------- default // -------- default
hideEchoBack: false, hideEchoBack: false,
@ -724,35 +775,44 @@ exports.questionNewPassword = function(query, options) {
// -------- default // -------- default
hideEchoBack: true, hideEchoBack: true,
mask: '*', mask: '*',
limitMessage: 'It can include: ${charlist}, the length able to be: ${length}', limitMessage: 'It can include: ${charlist}\n' +
caseSensitive: true, 'The length able to be: ${length}',
trueValue: null, trueValue: null,
falseValue: null, falseValue: null,
confirm: 'Reinput same one to confirm it :' caseSensitive: true
}, options, {/* forced limit */}), }, options),
// added: charlist, min, max, confirm // forced: limit
charlist, min, max, resCharlist, res1, res2, limit1, limitMessage1; // added: charlist, min, max, confirm
charlist, min, max, confirm, resCharlist, res1, res2, limit1, limitMessage1;
/* jshint eqnull:true */ function phSpecial(param) {
return param === 'charlist' ? resCharlist.text :
param === 'length' ? min + '...' + max : null;
}
// bug? `eqnull:true` doesn't work
/* jshint ignore:start */
if (query == null) { query = 'Input new password :'; } if (query == null) { query = 'Input new password :'; }
/* jshint eqnull:false */ /* jshint ignore:end */
charlist = options && options.charlist ? options.charlist + '' : '${!-~}'; charlist = options && options.charlist ? options.charlist + '' : '${!-~}';
charlist = replacePlaceholder(charlist, placeholderCharlist); charlist = replacePlaceholder(charlist, getPhCharlist);
if (options) { min = options.min; max = options.max; } if (options) { min = options.min; max = options.max; }
if (isNaN(min = parseInt(min, 10)) || typeof min !== 'number') { min = 12; } if (isNaN(min = parseInt(min, 10)) || typeof min !== 'number') { min = 12; }
if (isNaN(max = parseInt(max, 10)) || typeof max !== 'number') { max = 24; } if (isNaN(max = parseInt(max, 10)) || typeof max !== 'number') { max = 24; }
limit1 = new RegExp( /* jshint eqnull:true */
'^[' + charlist.replace(/[^A-Za-z0-9_ ]/g, '\\$&') + ']{' + min + ',' + max + '}$'); confirm = options && options.confirm != null ? options.confirm :
'Reinput same one to confirm it :';
/* jshint eqnull:false */
limit1 = new RegExp('^[' + charlist.replace(/[^A-Za-z0-9_ ]/g, '\\$&') +
']{' + min + ',' + max + '}$');
if (readOptions.limitMessage) { if (readOptions.limitMessage) {
resCharlist = array2charlist([charlist], readOptions.caseSensitive, true); resCharlist = array2charlist([charlist], readOptions.caseSensitive, true);
resCharlist.text = joinChunks(resCharlist.values, resCharlist.suppressed); resCharlist.text = joinChunks(resCharlist.values, resCharlist.suppressed);
limitMessage1 = replacePlaceholder(readOptions.limitMessage, // getPhContent is called by readlineWithOptions
function(param) { limitMessage1 = replacePlaceholder(readOptions.limitMessage, phSpecial);
return param === 'charlist' ? resCharlist.text :
param === 'length' ? min + '...' + max : null;
});
} }
while (!res2) { while (!res2) {
@ -763,31 +823,84 @@ exports.questionNewPassword = function(query, options) {
readOptions.limit = [res1, '']; readOptions.limit = [res1, ''];
readOptions.limitMessage = 'Two passwords don\'t match.' + readOptions.limitMessage = 'Two passwords don\'t match.' +
' Hit only Enter key if you want to retry from first password.'; ' Hit only Enter key if you want to retry from first password.';
res2 = exports.question(options.confirm, readOptions); // getPhContent is called by readlineWithOptions
res2 = exports.question(replacePlaceholder(confirm, phSpecial), readOptions);
} }
return res1; return res1;
}; };
function _questionParse(query, options, fncParse) {
var validValue;
function getValidValue(value) {
validValue = fncParse(value);
return !isNaN(validValue) && typeof validValue === 'number';
}
exports.question(query, margeOptions(options, {
// -------- forced
limit: getValidValue
// trueValue, falseValue are don't work.
}));
return validValue;
}
exports.questionInt = function(query, options) {
return _questionParse(query, options, function(value) { return parseInt(value, 10); });
};
exports.questionFloat = function(query, options)
{ return _questionParse(query, options, parseFloat); };
exports.questionPath = function(query, options) {
var fs = require('fs'), validPath;
function getValidPath(value) {
var stat;
if (!fs.existsSync(value)) { return; }
validPath = fs.realpathSync(value);
if (options.minSize || options.maxSize || options.isFile || options.isDirectory) {
stat = fs.statSync(validPath);
if (options.minSize && stat.size < +options.minSize ||
options.maxSize && stat.size > +options.maxSize ||
options.isFile && !stat.isFile() ||
options.isDirectory && !stat.isDirectory()) { return false; }
}
if (typeof options.validate === 'function' && !options.validate(validPath))
{ return false; }
return true;
}
exports.question(query, margeOptions(options, {
// -------- forced
limit: getValidPath
// trueValue, falseValue are don't work.
}));
// added: minSize, maxSize, isFile, isDirectory, validate
return validPath;
};
exports.promptCWD = function(options) {
var readOptions = margeOptions({
// -------- default
prompt: '[${cwd}]$ '
}, options);
return exports.prompt(readOptions);
};
function _keyInYN(query, options, limit) { function _keyInYN(query, options, limit) {
var readOptions = margeOptions(options, { var readOptions = margeOptions(options, {
// -------- forced // -------- forced
hideEchoBack: false, hideEchoBack: false,
limit: limit, limit: limit,
caseSensitive: false,
trueValue: 'y', trueValue: 'y',
falseValue: 'n' falseValue: 'n',
caseSensitive: false
}), res; }), res;
/* jshint eqnull:true */ /* jshint eqnull:true */
if (query == null) { query = 'Are you sure? :'; } if (query == null) { query = 'Are you sure? :'; }
/* jshint eqnull:false */ /* jshint eqnull:false */
if ((query += '') && options.keyGuide !== false) if ((query += '') && options.guide !== false)
{ query = query.replace(/\s*:?\s*$/, '') + ' [Y/N] :'; } { query = query.replace(/\s*:?\s*$/, '') + ' [Y/N] :'; }
res = exports.keyIn(query, readOptions); res = exports.keyIn(query, readOptions);
if (typeof res !== 'boolean') { res = ''; } return typeof res === 'boolean' ? res : '';
return res;
} }
exports.keyInYN = function(query, options) { return _keyInYN(query, options); }; exports.keyInYN = function(query, options) { return _keyInYN(query, options); };
exports.keyInYNStrict = function(query, options) exports.keyInYNStrict = function(query, options)
@ -800,10 +913,10 @@ exports.keyInPause = function(query, options) {
mask: '' mask: ''
}); });
/* jshint eqnull:true */ /* jshint eqnull:true */
if (query == null) { query = 'Continue...'; } if (query == null) { query = 'Continue...'; }
/* jshint eqnull:false */ /* jshint eqnull:false */
if ((query += '') && options.keyGuide !== false) if ((query += '') && options.guide !== false)
{ query = query.replace(/\s+$/, '') + ' (Hit any key)'; } { query = query.replace(/\s+$/, '') + ' (Hit any key)'; }
exports.keyIn(query, readOptions); exports.keyIn(query, readOptions);
@ -816,10 +929,10 @@ exports.keyInSelect = function(query, items, options) {
hideEchoBack: false, hideEchoBack: false,
}, options, { }, options, {
// -------- forced // -------- forced
caseSensitive: false,
trueValue: null, trueValue: null,
falseValue: null falseValue: null,
}), res, keylist = '', key2i = {}, charCode = 49 /* '1' */, display = '\n'; caseSensitive: false
}), keylist = '', key2i = {}, charCode = 49 /* '1' */, display = '\n';
if (!Array.isArray(items) || items.length > 35) if (!Array.isArray(items) || items.length > 35)
{ throw '`items` must be Array (max length: 35).'; } { throw '`items` must be Array (max length: 35).'; }
@ -838,17 +951,16 @@ exports.keyInSelect = function(query, items, options) {
readOptions.limit = keylist; readOptions.limit = keylist;
display += '\n'; display += '\n';
/* jshint eqnull:true */ /* jshint eqnull:true */
if (query == null) { query = 'Choose one from list :'; } if (query == null) { query = 'Choose one from list :'; }
/* jshint eqnull:false */ /* jshint eqnull:false */
if ((query += '')) { if ((query += '')) {
if (options.keyGuide !== false) if (options.guide !== false)
{ query = query.replace(/\s*:?\s*$/, '') + ' [${limit}] :'; } { query = query.replace(/\s*:?\s*$/, '') + ' [${limit}] :'; }
display += query; display += query;
} }
res = exports.keyIn(display, readOptions); return key2i[exports.keyIn(display, readOptions).toUpperCase()];
return key2i[res.toUpperCase()];
}; };
// ======== DEPRECATED ======== // ======== DEPRECATED ========