From a60336df9b96c91cccfeeff823fcc05ab4131001 Mon Sep 17 00:00:00 2001 From: anseki Date: Sun, 5 Apr 2015 00:37:42 +0900 Subject: [PATCH] Change: way to manage options data --- lib/read.ps1 | 8 ++-- lib/read.sh | 4 +- lib/readline-sync.js | 96 ++++++++++++++++++++++++++------------------ 3 files changed, 64 insertions(+), 44 deletions(-) diff --git a/lib/read.ps1 b/lib/read.ps1 index 7f3631a..fdc4c8f 100644 --- a/lib/read.ps1 +++ b/lib/read.ps1 @@ -10,7 +10,7 @@ Param( [switch] $noEchoBack, [string] $mask, [string] $limit, - [switch] $cs, + [switch] $caseSensitive, [switch] $encoded ) @@ -26,7 +26,7 @@ function decodeDOS ($arg) { } $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)) } if ($options.encoded) { @@ -95,8 +95,8 @@ while ($True) { # other ctrl-chars if ($chunk) { $chunk = $chunk -replace '[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]', '' } if ($chunk -and $limitPtn) { - if ($options.cs) { $chunk = $chunk -creplace $limitPtn, '' } - else { $chunk = $chunk -ireplace $limitPtn, '' } + if ($options.caseSensitive) { $chunk = $chunk -creplace $limitPtn, '' } + else { $chunk = $chunk -ireplace $limitPtn, '' } } if ($chunk) { diff --git a/lib/read.sh b/lib/read.sh index 20f6929..6aac68d 100644 --- a/lib/read.sh +++ b/lib/read.sh @@ -13,7 +13,7 @@ while [ $# -ge 1 ]; do 'noechoback') options_noEchoBack=true;; 'mask') shift; options_mask="$1";; 'limit') shift; options_limit="$1";; - 'cs') options_cs=true;; + 'caseSensitive') options_caseSensitive=true;; 'encoded') options_encoded=true;; esac shift @@ -67,7 +67,7 @@ fi [ "$options_keyIn" = true ] && req_size=1 if [ "$options_keyIn" = true ] && [ -n "$options_limit" ]; then - if [ "$options_cs" = true ]; then + if [ "$options_caseSensitive" = true ]; then limit_ptn="$options_limit" else # Safe list diff --git a/lib/readline-sync.js b/lib/readline-sync.js index c5c4a0b..0e6e4bf 100644 --- a/lib/readline-sync.js +++ b/lib/readline-sync.js @@ -19,11 +19,17 @@ var TTY = process.binding('tty_wrap').TTY, childProc = require('child_process'), - promptText = '> ', - encoding = 'utf8', - bufSize = 1024, - print, - mask = '*', + defaultOptions = { + prompt: '> ', // for API + noEchoBack: false, + mask: '*', + limit: '', + caseSensitive: false, + noTrim: false, + encoding: 'utf8', + bufSize: 1024, + print: null + }, useExt = false, fdR = 'none', fdW, ttyR, isRawMode = false, @@ -35,8 +41,9 @@ var noEchoBack: boolean mask: string limit: string (pattern) - cs: boolean + caseSensitive: boolean noTrim: boolean + encoding, bufSize, print */ function readlineSync(options) { var input = '', displaySave = options.display, @@ -125,7 +132,8 @@ function readlineSync(options) { return true; } - if (useExt || !ttyR || typeof fdW !== 'number' && (options.display || !isCooked)) { + if (useExt || !ttyR || + typeof fdW !== 'number' && (options.display || !isCooked)) { input = tryExt(); return; } @@ -139,10 +147,11 @@ function readlineSync(options) { input = tryExt(); return; } - buffer = new Buffer((reqSize = options.keyIn ? 1 : bufSize)); + buffer = new Buffer((reqSize = options.keyIn ? 1 : options.bufSize)); 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) { @@ -156,9 +165,10 @@ function readlineSync(options) { 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; atEol = true; } @@ -186,9 +196,10 @@ function readlineSync(options) { setRawMode(false); })(); - if (typeof print === 'function' && !silent) { // must at least write '\n' - print(displaySave + (options.noEchoBack ? - (new Array(input.length + 1)).join(options.mask) : input) + '\n', encoding); + if (typeof options.print === 'function' && !silent) { // must at least write '\n' + options.print(displaySave + (options.noEchoBack ? + (new Array(input.length + 1)).join(options.mask) : input) + '\n', + options.encoding); } return options.noTrim || options.keyIn ? input : input.trim(); @@ -196,7 +207,7 @@ function readlineSync(options) { function readlineExt(options) { var hostArgs, res = {}, - execOptions = {env: process.env, encoding: encoding}; + execOptions = {env: process.env, encoding: options.encoding}; if (!extHostPath) { if (IS_WIN) { @@ -318,17 +329,20 @@ function _execFileSync(options, execOptions) { res.error.args = shellArgs; } - while (fs.readFileSync(pathDone, {encoding: encoding}).trim() !== '1') {} - if ((exitCode = fs.readFileSync(pathExit, {encoding: encoding}).trim()) === '0') { + while (fs.readFileSync(pathDone, {encoding: options.encoding}).trim() !== '1') {} + if ((exitCode = + fs.readFileSync(pathExit, {encoding: options.encoding}).trim()) === '0') { res.input = - decipher.update(fs.readFileSync(pathStdout, {encoding: 'binary'}), 'hex', encoding) + - decipher.final(encoding); + decipher.update(fs.readFileSync(pathStdout, {encoding: 'binary'}), + 'hex', options.encoding) + + decipher.final(options.encoding); } else { res.error = new Error(DEFAULT_ERR_MSG); res.error.method = '_execFileSync'; res.error.program = shellPath; 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; } @@ -369,11 +383,17 @@ function getHostArgs(options) { noEchoBack: 'boolean', mask: 'string', limit: 'string', - cs: 'boolean', - encoded: 'boolean' + caseSensitive: 'boolean', + encoded: 'boolean' // added by readlineExt, _execFileSync })); } +function margeOptions() { + var optsHashes = Array.prototype.slice.call(arguments), + options = {}; + return options; +} + function flattenArray(array, validate) { var flatArray = []; function parseArray(array) { @@ -390,47 +410,47 @@ function flattenArray(array, validate) { // for dev exports._useExtSet = function(use) { useExt = use; }; -exports.setPrint = function(fnc) { print = fnc; }; +exports.setPrint = function(fnc) { defaultOptions.print = fnc; }; exports.setPrompt = function(newPrompt) { /* jshint eqnull:true */ if (newPrompt != null) { /* jshint eqnull:false */ - promptText = newPrompt; + defaultOptions.prompt = newPrompt; } - return promptText; + return defaultOptions.prompt; }; exports.setEncoding = function(newEncoding) { if (typeof newEncoding === 'string') { - encoding = newEncoding; + defaultOptions.encoding = newEncoding; } - return encoding; + return defaultOptions.encoding; }; exports.setMask = function(newMask) { if (typeof newMask === 'string') { - mask = newMask; + defaultOptions.mask = newMask; } - return mask; + return defaultOptions.mask; }; exports.setBufferSize = function(newBufSize) { newBufSize = parseInt(newBufSize, 10); if (!isNaN(newBufSize) && typeof newBufSize === 'number') { - bufSize = newBufSize; + defaultOptions.bufSize = newBufSize; } - return bufSize; + return defaultOptions.bufSize; }; exports.prompt = function(options) { var readOptions = { - display: promptText + '', + display: defaultOptions.prompt + '', keyIn: false, noEchoBack: !!(options && options.noEchoBack), - mask: mask, + mask: defaultOptions.mask, limit: '', - cs: !!(options && options.caseSensitive), + caseSensitive: !!(options && options.caseSensitive), noTrim: !!(options && options.noTrim) }; return readlineSync(readOptions); @@ -443,9 +463,9 @@ exports.question = function(query, options) { /* jshint eqnull:false */ keyIn: false, noEchoBack: !!(options && options.noEchoBack), - mask: mask, + mask: defaultOptions.mask, limit: '', - cs: !!(options && options.caseSensitive), + caseSensitive: !!(options && options.caseSensitive), noTrim: !!(options && options.noTrim) }; return readlineSync(readOptions); @@ -462,9 +482,9 @@ exports.keyIn = function(query, options) { /* jshint eqnull:false */ keyIn: true, noEchoBack: !!(options && options.noEchoBack), - mask: mask, + mask: defaultOptions.mask, limit: limit, - cs: !!(options && options.caseSensitive), + caseSensitive: !!(options && options.caseSensitive), noTrim: true }; return readlineSync(readOptions);