Check calling print

This commit is contained in:
anseki 2015-03-31 20:27:02 +09:00
parent 2f091b829c
commit 4037ba9845
4 changed files with 93 additions and 66 deletions

View file

@ -45,16 +45,16 @@ var
return options; return options;
})({ })({
display: 'string', display: 'string',
keyIn: 'boolean',
noEchoBack: 'boolean', noEchoBack: 'boolean',
mask: 'string', mask: 'string',
keyIn: 'boolean',
encoded: 'boolean' encoded: 'boolean'
}); });
if (!options.noEchoBack && !options.keyIn) { if (!options.noEchoBack && !options.keyIn) {
if (options.display !== '') { writeTTY(options.display); } if (options.display !== '') { writeTTY(options.display); }
input = readByFSO(); input = readByFSO();
} else if (options.noEchoBack && !options.keyIn && options.mask === '*') { } else if (options.noEchoBack && !options.keyIn && options.mask === '') {
if (options.display !== '') { writeTTY(options.display); } if (options.display !== '') { writeTTY(options.display); }
input = readByPW(); input = readByPW();
} else { } else {

View file

@ -1,9 +1,11 @@
Param( Param(
[string] $display, [string] $display,
[switch] $keyIn,
[switch] $noEchoBack, [switch] $noEchoBack,
[string] $mask, [string] $mask,
[switch] $keyIn, [string] $exclude,
[switch] $cs,
[switch] $encoded [switch] $encoded
) )
@ -19,7 +21,7 @@ function decodeDOS ($arg) {
} }
$options = @{} $options = @{}
foreach ($arg in @('display', 'noEchoBack', 'mask', 'keyIn', 'encoded')) { foreach ($arg in @('display', 'keyIn', 'noEchoBack', 'mask', 'exclude', 'cs', 'encoded')) {
$options.Add($arg, (Get-Variable $arg -ValueOnly)) $options.Add($arg, (Get-Variable $arg -ValueOnly))
} }
if ($options.encoded) { if ($options.encoded) {
@ -71,6 +73,8 @@ if ($options.keyIn) { $reqSize = 1 }
while ($True) { while ($True) {
if (-not $isCooked) { if (-not $isCooked) {
$chunk = execWithTTY '[System.Console]::ReadKey($True).KeyChar' $True $chunk = execWithTTY '[System.Console]::ReadKey($True).KeyChar' $True
# ReadKey() may returns [System.Array], then don't cast data.
if ($chunk -isnot [string]) { $chunk = '' }
$chunk = $chunk -replace '[\r\n]', '' $chunk = $chunk -replace '[\r\n]', ''
if ($chunk -eq '') { $isEol = $True } # NL or empty-text was input if ($chunk -eq '') { $isEol = $True } # NL or empty-text was input
} else { } else {

View file

@ -4,9 +4,11 @@ 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="$1";; 'display') shift; options_display="$1";;
'keyin') options_keyIn='true';;
'noechoback') options_noEchoBack='true';; 'noechoback') options_noEchoBack='true';;
'mask') shift; options_mask="$1";; 'mask') shift; options_mask="$1";;
'keyin') options_keyIn='true';; 'exclude') shift; options_exclude="$1";;
'cs') options_cs='true';;
'encoded') options_encoded='true';; 'encoded') options_encoded='true';;
esac esac
shift shift

View file

@ -31,13 +31,16 @@ var
/* /*
display: string display: string
keyIn: boolean
noEchoBack: boolean noEchoBack: boolean
mask: string mask: string
keyIn: boolean exclude: string (pattern, not RegExp)
cs: boolean
noTrim: boolean noTrim: boolean
*/ */
function readlineSync(options) { function readlineSync(options) {
var input = '', displayTmp; var input = '', displaySave = options.display,
silent = !options.display && options.keyIn && options.noEchoBack && !options.mask;
function tryExt() { function tryExt() {
var res = readlineExt(options); var res = readlineExt(options);
@ -45,7 +48,7 @@ function readlineSync(options) {
return res.input; return res.input;
} }
(function() { (function() { // open TTY
var fsB, constants; var fsB, constants;
function getFsB() { function getFsB() {
@ -109,19 +112,10 @@ function readlineSync(options) {
} }
})(); })();
// Call before tryExt()
if (options.display !== '' && typeof print === 'function')
{ print(options.display, encoding); }
(function() { // try read (function() { // try read
var buffer, reqSize, readSize, chunk, isEol, isInputLine = false, line, var buffer, reqSize, readSize, chunk, isEol, line, exclude,
isCooked = !options.noEchoBack && !options.keyIn; isCooked = !options.noEchoBack && !options.keyIn;
function writeTTY(text) {
fs.writeSync(fdW, text);
isInputLine = true;
}
// Node v0.10- returns an error if same mode is set. // Node v0.10- returns an error if same mode is set.
function setRawMode(mode) { function setRawMode(mode) {
if (mode === isRawMode) { return true; } if (mode === isRawMode) { return true; }
@ -130,14 +124,13 @@ function readlineSync(options) {
return true; return true;
} }
if (useExt || !ttyR || if (useExt || !ttyR || typeof fdW !== 'number' && (options.display || !isCooked)) {
typeof fdW !== 'number' && (options.display !== '' || !isCooked)) {
input = tryExt(); input = tryExt();
return; return;
} }
if (options.display !== '') { if (options.display) {
writeTTY(options.display); fs.writeSync(fdW, options.display);
options.display = ''; options.display = '';
} }
@ -147,6 +140,10 @@ function readlineSync(options) {
} }
buffer = new Buffer((reqSize = options.keyIn ? 1 : bufSize)); buffer = new Buffer((reqSize = options.keyIn ? 1 : bufSize));
if (options.exclude) {
exclude = new RegExp(options.exclude, 'g' + (options.cs ? '' : 'i'));
}
while (true) { while (true) {
readSize = 0; readSize = 0;
try { try {
@ -162,20 +159,20 @@ function readlineSync(options) {
if (!isCooked) { if (!isCooked) {
chunk = chunk.replace(/[\r\n]/g, ''); chunk = chunk.replace(/[\r\n]/g, '');
if (chunk === '') { isEol = true; } // NL or empty-text was input if (!chunk) { isEol = true; } // NL or empty-text was input
} else if (typeof(line = (chunk.match(/^(.*?)[\r\n]/) || [])[1]) === 'string') { } else if (typeof(line = (chunk.match(/^(.*?)[\r\n]/) || [])[1]) === 'string') {
chunk = line; chunk = line;
isEol = true; isEol = true;
} }
// other ctrl-chars chunk = chunk.replace(/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/g, ''); // other ctrl-chars
chunk = chunk.replace(/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/g, ''); if (chunk && options.keyIn && exclude) { chunk = chunk.replace(exclude, ''); }
if (chunk !== '' && !isCooked) { if (chunk && !isCooked) {
if (!options.noEchoBack) { if (!options.noEchoBack) {
writeTTY(chunk); fs.writeSync(fdW, chunk);
} else if (options.mask !== '') { } else if (options.mask) {
writeTTY((new Array(chunk.length + 1)).join(options.mask)); fs.writeSync(fdW, (new Array(chunk.length + 1)).join(options.mask));
} }
} }
@ -183,19 +180,16 @@ function readlineSync(options) {
if (isEol || options.keyIn && input.length >= reqSize) { break; } if (isEol || options.keyIn && input.length >= reqSize) { break; }
} }
if (!isCooked && !(options.keyIn && !isInputLine)) { writeTTY('\n'); } if (!isCooked && !silent) { fs.writeSync(fdW, '\n'); }
setRawMode(false); setRawMode(false);
})(); })();
if (typeof print === 'function') { if (typeof print === 'function' && !silent) { // must at least write '\n'
displayTmp = input.replace(/[\r\n]/g, ''); print(displaySave + (options.noEchoBack ?
print((options.noEchoBack ? (new Array(input.length + 1)).join(options.mask) : input) + '\n', encoding);
(new Array(displayTmp.length + 1)).join(options.mask) : displayTmp) +
'\n', encoding);
} }
return options.noTrim || options.keyIn ? return options.noTrim || options.keyIn ? input : input.trim();
input.replace(/[\r\n]+$/, '') : input.trim();
} }
function readlineExt(options) { function readlineExt(options) {
@ -225,6 +219,7 @@ function readlineExt(options) {
if (childProc.execFileSync) { if (childProc.execFileSync) {
hostArgs = getHostArgs(options); hostArgs = getHostArgs(options);
console.warn('<childProc.execFileSync>');
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
@ -238,9 +233,11 @@ function readlineExt(options) {
res.error.signal = e.signal; res.error.signal = e.signal;
} }
} else { } else {
console.warn('<_execFileSync>');
res = _execFileSync(options, execOptions); res = _execFileSync(options, execOptions);
} }
if (!res.error) { if (!res.error) {
console.warn('ROW-RES:<'+res.input+'>');
res.input = res.input.replace(/^\s*'|'\s*$/g, ''); res.input = res.input.replace(/^\s*'|'\s*$/g, '');
options.display = ''; options.display = '';
} }
@ -359,7 +356,7 @@ function getHostArgs(options) {
if (conf[key] === 'boolean') { if (conf[key] === 'boolean') {
if (options[key]) { args.push('--' + key); } if (options[key]) { args.push('--' + key); }
} else if (conf[key] === 'string') { } else if (conf[key] === 'string') {
if (options[key] !== '') { if (options[key]) {
args.push('--' + key, args.push('--' + key,
options.encoded ? encodeDOS(options[key]) : options[key]); options.encoded ? encodeDOS(options[key]) : options[key]);
} }
@ -369,13 +366,28 @@ function getHostArgs(options) {
return args; return args;
})({ })({
display: 'string', display: 'string',
keyIn: 'boolean',
noEchoBack: 'boolean', noEchoBack: 'boolean',
mask: 'string', mask: 'string',
keyIn: 'boolean', exclude: 'string',
cs: 'boolean',
encoded: 'boolean' encoded: 'boolean'
})); }));
} }
function flattenArray(array, validate) {
var flatArray = [];
function parseArray(array) {
/* jshint eqnull:true */
if (array == null) { return; }
/* jshint eqnull:false */
else if (Array.isArray(array)) { array.forEach(parseArray); }
else if (!validate || validate(array)) { flatArray.push(array); }
}
parseArray(array);
return flatArray;
}
// for dev // for dev
exports._useExtSet = function(use) { useExt = use; }; exports._useExtSet = function(use) { useExt = use; };
@ -415,9 +427,11 @@ exports.setBufferSize = function(newBufSize) {
exports.prompt = function(options) { exports.prompt = function(options) {
var readOptions = { var readOptions = {
display: promptText + '', display: promptText + '',
keyIn: false,
noEchoBack: !!(options && options.noEchoBack), noEchoBack: !!(options && options.noEchoBack),
mask: mask, mask: mask,
keyIn: false, exclude: '',
cs: !!(options && options.caseSensitive),
noTrim: !!(options && options.noTrim) noTrim: !!(options && options.noTrim)
}; };
return readlineSync(readOptions); return readlineSync(readOptions);
@ -428,22 +442,29 @@ exports.question = function(query, options) {
/* jshint eqnull:true */ /* jshint eqnull:true */
display: query != null ? query + '' : '', display: query != null ? query + '' : '',
/* jshint eqnull:false */ /* jshint eqnull:false */
keyIn: false,
noEchoBack: !!(options && options.noEchoBack), noEchoBack: !!(options && options.noEchoBack),
mask: mask, mask: mask,
keyIn: false, exclude: '',
cs: !!(options && options.caseSensitive),
noTrim: !!(options && options.noTrim) noTrim: !!(options && options.noTrim)
}; };
return readlineSync(readOptions); return readlineSync(readOptions);
}; };
exports.keyIn = function(query, options) { exports.keyIn = function(query, options) {
var readOptions = { var limit = options ? flattenArray(options.limit, function(value) {
return typeof value === 'string' || typeof value === 'number'; }) : [],
readOptions = {
/* jshint eqnull:true */ /* jshint eqnull:true */
display: query != null ? query + '' : '', display: query != null ? query + '' : '',
/* jshint eqnull:false */ /* jshint eqnull:false */
keyIn: true,
noEchoBack: !!(options && options.noEchoBack), noEchoBack: !!(options && options.noEchoBack),
mask: mask, mask: mask,
keyIn: true, exclude: limit.length ?
'[^' + limit.join('').replace(/\W/g, '\\$&') + ']' : '',
cs: !!(options && options.caseSensitive),
noTrim: true noTrim: true
}; };
return readlineSync(readOptions); return readlineSync(readOptions);