Add setDefault.

This commit is contained in:
anseki 2015-04-05 14:45:42 +09:00
parent a60336df9b
commit efc4b8837c

View file

@ -20,7 +20,7 @@ var
childProc = require('child_process'), childProc = require('child_process'),
defaultOptions = { defaultOptions = {
prompt: '> ', // for API prompt: '> ',
noEchoBack: false, noEchoBack: false,
mask: '*', mask: '*',
limit: '', limit: '',
@ -36,7 +36,7 @@ var
extHostPath, extHostArgs, tempdir, salt = 0; extHostPath, extHostArgs, tempdir, salt = 0;
/* /*
display: string display: other (to string)
keyIn: boolean keyIn: boolean
noEchoBack: boolean noEchoBack: boolean
mask: string mask: string
@ -45,8 +45,8 @@ var
noTrim: boolean noTrim: boolean
encoding, bufSize, print encoding, bufSize, print
*/ */
function readlineSync(options) { function _readlineSync(options) {
var input = '', displaySave = options.display, var input = '', displaySave = (options.display += ''),
silent = !options.display && options.keyIn && options.noEchoBack && !options.mask; silent = !options.display && options.keyIn && options.noEchoBack && !options.mask;
function tryExt() { function tryExt() {
@ -196,7 +196,7 @@ function readlineSync(options) {
setRawMode(false); setRawMode(false);
})(); })();
if (typeof options.print === 'function' && !silent) { // must at least write '\n' if (options.print && !silent) { // must at least write '\n'
options.print(displaySave + (options.noEchoBack ? options.print(displaySave + (options.noEchoBack ?
(new Array(input.length + 1)).join(options.mask) : input) + '\n', (new Array(input.length + 1)).join(options.mask) : input) + '\n',
options.encoding); options.encoding);
@ -363,19 +363,17 @@ function getHostArgs(options) {
} }
return extHostArgs.concat((function(conf) { return extHostArgs.concat((function(conf) {
var args = [], key; var args = [];
for (key in conf) { Object.keys(conf).forEach(function(optionName) {
if (conf.hasOwnProperty(key)) { if (conf[optionName] === 'boolean') {
if (conf[key] === 'boolean') { if (options[optionName]) { args.push('--' + optionName); }
if (options[key]) { args.push('--' + key); } } else if (conf[optionName] === 'string') {
} else if (conf[key] === 'string') { if (options[optionName]) {
if (options[key]) { args.push('--' + optionName,
args.push('--' + key, options.encoded ? encodeDOS(options[optionName]) : options[optionName]);
options.encoded ? encodeDOS(options[key]) : options[key]);
}
}
} }
} }
});
return args; return args;
})({ })({
display: 'string', display: 'string',
@ -388,10 +386,67 @@ function getHostArgs(options) {
})); }));
} }
// margeOptions(options1, options2 ... )
// margeOptions(true, options1, options2 ... ) // from defaultOptions
function margeOptions() { function margeOptions() {
var optsHashes = Array.prototype.slice.call(arguments), var optionsList = Array.prototype.slice.call(arguments),
options = {}; optionNames, fromDefault;
if (optionsList.length && typeof optionsList[0] === 'boolean') {
fromDefault = optionsList.shift();
if (fromDefault) {
optionNames = Object.keys(defaultOptions);
optionsList.unshift(defaultOptions);
}
}
return optionsList.reduce(function(options, optionsPart) {
/* jshint eqnull:true */
if (optionsPart == null) { return options; }
/* jshint eqnull:false */
if (!fromDefault) { optionNames = Object.keys(optionsPart); }
optionNames.forEach(function(optionName) {
var value;
if (!optionsPart.hasOwnProperty(optionName)) { return; }
value = optionsPart[optionName];
switch (optionName) {
// _readlineSync defaultOptions
// string
case 'mask': // * *
case 'limit': // * *
case 'encoding': // * *
/* jshint eqnull:true */
options[optionName] = value != null ? value + '' : '';
/* jshint eqnull:false */
break;
// number
case 'bufSize': // * *
if (!isNaN(value = parseInt(value, 10)) && typeof value === 'number')
{ options[optionName] = value; }
break;
// boolean
case 'noEchoBack': // * *
case 'caseSensitive': // * *
case 'noTrim': // * *
case 'keyIn': // *
options[optionName] = !!value;
break;
// function
case 'print': // * *
options[optionName] = typeof value === 'function' ? value : null;
break;
// other
case 'prompt': // *
case 'display': // *
/* jshint eqnull:true */
options[optionName] = value != null ? value : '';
/* jshint eqnull:false */
break;
}
});
return options; return options;
}, {});
} }
function flattenArray(array, validate) { function flattenArray(array, validate) {
@ -410,65 +465,17 @@ function flattenArray(array, validate) {
// for dev // for dev
exports._useExtSet = function(use) { useExt = use; }; exports._useExtSet = function(use) { useExt = use; };
exports.setPrint = function(fnc) { defaultOptions.print = fnc; };
exports.setPrompt = function(newPrompt) {
/* jshint eqnull:true */
if (newPrompt != null) {
/* jshint eqnull:false */
defaultOptions.prompt = newPrompt;
}
return defaultOptions.prompt;
};
exports.setEncoding = function(newEncoding) {
if (typeof newEncoding === 'string') {
defaultOptions.encoding = newEncoding;
}
return defaultOptions.encoding;
};
exports.setMask = function(newMask) {
if (typeof newMask === 'string') {
defaultOptions.mask = newMask;
}
return defaultOptions.mask;
};
exports.setBufferSize = function(newBufSize) {
newBufSize = parseInt(newBufSize, 10);
if (!isNaN(newBufSize) && typeof newBufSize === 'number') {
defaultOptions.bufSize = newBufSize;
}
return defaultOptions.bufSize;
};
exports.prompt = function(options) { exports.prompt = function(options) {
var readOptions = { var readOptions = margeOptions(true, options);
display: defaultOptions.prompt + '', readOptions.display = readOptions.prompt;
keyIn: false, return _readlineSync(readOptions);
noEchoBack: !!(options && options.noEchoBack),
mask: defaultOptions.mask,
limit: '',
caseSensitive: !!(options && options.caseSensitive),
noTrim: !!(options && options.noTrim)
};
return readlineSync(readOptions);
}; };
exports.question = function(query, options) { exports.question = function(query, options) {
var readOptions = { var readOptions = margeOptions(margeOptions(true, options), {
/* jshint eqnull:true */ display: query
display: query != null ? query + '' : '', });
/* jshint eqnull:false */ return _readlineSync(readOptions);
keyIn: false,
noEchoBack: !!(options && options.noEchoBack),
mask: defaultOptions.mask,
limit: '',
caseSensitive: !!(options && options.caseSensitive),
noTrim: !!(options && options.noTrim)
};
return readlineSync(readOptions);
}; };
exports.keyIn = function(query, options) { exports.keyIn = function(query, options) {
@ -476,16 +483,23 @@ exports.keyIn = function(query, options) {
flattenArray(options.limit, function(value) flattenArray(options.limit, function(value)
{ return typeof value === 'string' || typeof value === 'number'; }) { return typeof value === 'string' || typeof value === 'number'; })
.join('').replace(/\n/g, '').replace(/[^A-Za-z0-9_ ]/g, '\\$&') : '', .join('').replace(/\n/g, '').replace(/[^A-Za-z0-9_ ]/g, '\\$&') : '',
readOptions = { readOptions = margeOptions(margeOptions(true, options), {
/* jshint eqnull:true */ display: query,
display: query != null ? query + '' : '',
/* jshint eqnull:false */
keyIn: true, keyIn: true,
noEchoBack: !!(options && options.noEchoBack),
mask: defaultOptions.mask,
limit: limit, limit: limit,
caseSensitive: !!(options && options.caseSensitive),
noTrim: true noTrim: true
}; });
return readlineSync(readOptions); return _readlineSync(readOptions);
}; };
exports.setDefault = function(options) {
defaultOptions = margeOptions(true, options);
return margeOptions(true); // copy
};
// These APIs are now obsolete.
exports.setPrint = function(value) { return exports.setDefault({print: value}).print; };
exports.setPrompt = function(value) { return exports.setDefault({prompt: value}).prompt; };
exports.setEncoding = function(value) { return exports.setDefault({encoding: value}).encoding; };
exports.setMask = function(value) { return exports.setDefault({mask: value}).mask; };
exports.setBufferSize = function(value) { return exports.setDefault({bufSize: value}).bufSize; };