Change: way to manage options data

This commit is contained in:
anseki 2015-04-05 00:37:42 +09:00
parent e623c8eb21
commit a60336df9b
3 changed files with 64 additions and 44 deletions

View file

@ -10,7 +10,7 @@ Param(
[switch] $noEchoBack, [switch] $noEchoBack,
[string] $mask, [string] $mask,
[string] $limit, [string] $limit,
[switch] $cs, [switch] $caseSensitive,
[switch] $encoded [switch] $encoded
) )
@ -26,7 +26,7 @@ function decodeDOS ($arg) {
} }
$options = @{} $options = @{}
foreach ($arg in @('display', 'keyIn', 'noEchoBack', 'mask', 'limit', 'cs', 'encoded')) { foreach ($arg in @('display', 'keyIn', 'noEchoBack', 'mask', 'limit', 'caseSensitive', 'encoded')) {
$options.Add($arg, (Get-Variable $arg -ValueOnly)) $options.Add($arg, (Get-Variable $arg -ValueOnly))
} }
if ($options.encoded) { if ($options.encoded) {
@ -95,8 +95,8 @@ while ($True) {
# other ctrl-chars # other ctrl-chars
if ($chunk) { $chunk = $chunk -replace '[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]', '' } if ($chunk) { $chunk = $chunk -replace '[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]', '' }
if ($chunk -and $limitPtn) { if ($chunk -and $limitPtn) {
if ($options.cs) { $chunk = $chunk -creplace $limitPtn, '' } if ($options.caseSensitive) { $chunk = $chunk -creplace $limitPtn, '' }
else { $chunk = $chunk -ireplace $limitPtn, '' } else { $chunk = $chunk -ireplace $limitPtn, '' }
} }
if ($chunk) { if ($chunk) {

View file

@ -13,7 +13,7 @@ while [ $# -ge 1 ]; do
'noechoback') options_noEchoBack=true;; 'noechoback') options_noEchoBack=true;;
'mask') shift; options_mask="$1";; 'mask') shift; options_mask="$1";;
'limit') shift; options_limit="$1";; 'limit') shift; options_limit="$1";;
'cs') options_cs=true;; 'caseSensitive') options_caseSensitive=true;;
'encoded') options_encoded=true;; 'encoded') options_encoded=true;;
esac esac
shift shift
@ -67,7 +67,7 @@ fi
[ "$options_keyIn" = true ] && req_size=1 [ "$options_keyIn" = true ] && req_size=1
if [ "$options_keyIn" = true ] && [ -n "$options_limit" ]; then if [ "$options_keyIn" = true ] && [ -n "$options_limit" ]; then
if [ "$options_cs" = true ]; then if [ "$options_caseSensitive" = true ]; then
limit_ptn="$options_limit" limit_ptn="$options_limit"
else else
# Safe list # Safe list

View file

@ -19,11 +19,17 @@ var
TTY = process.binding('tty_wrap').TTY, TTY = process.binding('tty_wrap').TTY,
childProc = require('child_process'), childProc = require('child_process'),
promptText = '> ', defaultOptions = {
encoding = 'utf8', prompt: '> ', // for API
bufSize = 1024, noEchoBack: false,
print, mask: '*',
mask = '*', limit: '',
caseSensitive: false,
noTrim: false,
encoding: 'utf8',
bufSize: 1024,
print: null
},
useExt = false, useExt = false,
fdR = 'none', fdW, ttyR, isRawMode = false, fdR = 'none', fdW, ttyR, isRawMode = false,
@ -35,8 +41,9 @@ var
noEchoBack: boolean noEchoBack: boolean
mask: string mask: string
limit: string (pattern) limit: string (pattern)
cs: boolean caseSensitive: boolean
noTrim: boolean noTrim: boolean
encoding, bufSize, print
*/ */
function readlineSync(options) { function readlineSync(options) {
var input = '', displaySave = options.display, var input = '', displaySave = options.display,
@ -125,7 +132,8 @@ function readlineSync(options) {
return true; return true;
} }
if (useExt || !ttyR || typeof fdW !== 'number' && (options.display || !isCooked)) { if (useExt || !ttyR ||
typeof fdW !== 'number' && (options.display || !isCooked)) {
input = tryExt(); input = tryExt();
return; return;
} }
@ -139,10 +147,11 @@ function readlineSync(options) {
input = tryExt(); input = tryExt();
return; return;
} }
buffer = new Buffer((reqSize = options.keyIn ? 1 : bufSize)); buffer = new Buffer((reqSize = options.keyIn ? 1 : options.bufSize));
if (options.keyIn && options.limit) { if (options.keyIn && options.limit) {
limit = new RegExp('[^' + options.limit + ']', 'g' + (options.cs ? '' : 'i')); limit = new RegExp('[^' + options.limit + ']',
'g' + (options.caseSensitive ? '' : 'i'));
} }
while (true) { while (true) {
@ -156,9 +165,10 @@ function readlineSync(options) {
return; return;
} }
} }
chunk = readSize > 0 ? buffer.toString(encoding, 0, readSize) : '\n'; chunk = readSize > 0 ? buffer.toString(options.encoding, 0, readSize) : '\n';
if (chunk && typeof(line = (chunk.match(/^(.*?)[\r\n]/) || [])[1]) === 'string') { if (chunk &&
typeof(line = (chunk.match(/^(.*?)[\r\n]/) || [])[1]) === 'string') {
chunk = line; chunk = line;
atEol = true; atEol = true;
} }
@ -186,9 +196,10 @@ function readlineSync(options) {
setRawMode(false); setRawMode(false);
})(); })();
if (typeof print === 'function' && !silent) { // must at least write '\n' if (typeof options.print === 'function' && !silent) { // must at least write '\n'
print(displaySave + (options.noEchoBack ? options.print(displaySave + (options.noEchoBack ?
(new Array(input.length + 1)).join(options.mask) : input) + '\n', encoding); (new Array(input.length + 1)).join(options.mask) : input) + '\n',
options.encoding);
} }
return options.noTrim || options.keyIn ? input : input.trim(); return options.noTrim || options.keyIn ? input : input.trim();
@ -196,7 +207,7 @@ function readlineSync(options) {
function readlineExt(options) { function readlineExt(options) {
var hostArgs, res = {}, var hostArgs, res = {},
execOptions = {env: process.env, encoding: encoding}; execOptions = {env: process.env, encoding: options.encoding};
if (!extHostPath) { if (!extHostPath) {
if (IS_WIN) { if (IS_WIN) {
@ -318,17 +329,20 @@ function _execFileSync(options, execOptions) {
res.error.args = shellArgs; res.error.args = shellArgs;
} }
while (fs.readFileSync(pathDone, {encoding: encoding}).trim() !== '1') {} while (fs.readFileSync(pathDone, {encoding: options.encoding}).trim() !== '1') {}
if ((exitCode = fs.readFileSync(pathExit, {encoding: encoding}).trim()) === '0') { if ((exitCode =
fs.readFileSync(pathExit, {encoding: options.encoding}).trim()) === '0') {
res.input = res.input =
decipher.update(fs.readFileSync(pathStdout, {encoding: 'binary'}), 'hex', encoding) + decipher.update(fs.readFileSync(pathStdout, {encoding: 'binary'}),
decipher.final(encoding); 'hex', options.encoding) +
decipher.final(options.encoding);
} else { } else {
res.error = new Error(DEFAULT_ERR_MSG); res.error = new Error(DEFAULT_ERR_MSG);
res.error.method = '_execFileSync'; res.error.method = '_execFileSync';
res.error.program = shellPath; res.error.program = shellPath;
res.error.args = shellArgs; res.error.args = shellArgs;
res.error.extMessage = fs.readFileSync(pathStderr, {encoding: encoding}).trim(); res.error.extMessage =
fs.readFileSync(pathStderr, {encoding: options.encoding}).trim();
res.error.exitCode = +exitCode; res.error.exitCode = +exitCode;
} }
@ -369,11 +383,17 @@ function getHostArgs(options) {
noEchoBack: 'boolean', noEchoBack: 'boolean',
mask: 'string', mask: 'string',
limit: 'string', limit: 'string',
cs: 'boolean', caseSensitive: 'boolean',
encoded: 'boolean' encoded: 'boolean' // added by readlineExt, _execFileSync
})); }));
} }
function margeOptions() {
var optsHashes = Array.prototype.slice.call(arguments),
options = {};
return options;
}
function flattenArray(array, validate) { function flattenArray(array, validate) {
var flatArray = []; var flatArray = [];
function parseArray(array) { function parseArray(array) {
@ -390,47 +410,47 @@ function flattenArray(array, validate) {
// for dev // for dev
exports._useExtSet = function(use) { useExt = use; }; exports._useExtSet = function(use) { useExt = use; };
exports.setPrint = function(fnc) { print = fnc; }; exports.setPrint = function(fnc) { defaultOptions.print = fnc; };
exports.setPrompt = function(newPrompt) { exports.setPrompt = function(newPrompt) {
/* jshint eqnull:true */ /* jshint eqnull:true */
if (newPrompt != null) { if (newPrompt != null) {
/* jshint eqnull:false */ /* jshint eqnull:false */
promptText = newPrompt; defaultOptions.prompt = newPrompt;
} }
return promptText; return defaultOptions.prompt;
}; };
exports.setEncoding = function(newEncoding) { exports.setEncoding = function(newEncoding) {
if (typeof newEncoding === 'string') { if (typeof newEncoding === 'string') {
encoding = newEncoding; defaultOptions.encoding = newEncoding;
} }
return encoding; return defaultOptions.encoding;
}; };
exports.setMask = function(newMask) { exports.setMask = function(newMask) {
if (typeof newMask === 'string') { if (typeof newMask === 'string') {
mask = newMask; defaultOptions.mask = newMask;
} }
return mask; return defaultOptions.mask;
}; };
exports.setBufferSize = function(newBufSize) { exports.setBufferSize = function(newBufSize) {
newBufSize = parseInt(newBufSize, 10); newBufSize = parseInt(newBufSize, 10);
if (!isNaN(newBufSize) && typeof newBufSize === 'number') { if (!isNaN(newBufSize) && typeof newBufSize === 'number') {
bufSize = newBufSize; defaultOptions.bufSize = newBufSize;
} }
return bufSize; return defaultOptions.bufSize;
}; };
exports.prompt = function(options) { exports.prompt = function(options) {
var readOptions = { var readOptions = {
display: promptText + '', display: defaultOptions.prompt + '',
keyIn: false, keyIn: false,
noEchoBack: !!(options && options.noEchoBack), noEchoBack: !!(options && options.noEchoBack),
mask: mask, mask: defaultOptions.mask,
limit: '', limit: '',
cs: !!(options && options.caseSensitive), caseSensitive: !!(options && options.caseSensitive),
noTrim: !!(options && options.noTrim) noTrim: !!(options && options.noTrim)
}; };
return readlineSync(readOptions); return readlineSync(readOptions);
@ -443,9 +463,9 @@ exports.question = function(query, options) {
/* jshint eqnull:false */ /* jshint eqnull:false */
keyIn: false, keyIn: false,
noEchoBack: !!(options && options.noEchoBack), noEchoBack: !!(options && options.noEchoBack),
mask: mask, mask: defaultOptions.mask,
limit: '', limit: '',
cs: !!(options && options.caseSensitive), caseSensitive: !!(options && options.caseSensitive),
noTrim: !!(options && options.noTrim) noTrim: !!(options && options.noTrim)
}; };
return readlineSync(readOptions); return readlineSync(readOptions);
@ -462,9 +482,9 @@ exports.keyIn = function(query, options) {
/* jshint eqnull:false */ /* jshint eqnull:false */
keyIn: true, keyIn: true,
noEchoBack: !!(options && options.noEchoBack), noEchoBack: !!(options && options.noEchoBack),
mask: mask, mask: defaultOptions.mask,
limit: limit, limit: limit,
cs: !!(options && options.caseSensitive), caseSensitive: !!(options && options.caseSensitive),
noTrim: true noTrim: true
}; };
return readlineSync(readOptions); return readlineSync(readOptions);