Change the way to exit from input.

This commit is contained in:
anseki 2015-03-30 19:36:31 +09:00
parent 5f2410132b
commit 2f091b829c
2 changed files with 16 additions and 19 deletions

View file

@ -36,7 +36,7 @@ if ($options.encoded) {
[bool] $isCooked = (-not $options.noEchoBack) -and (-not $options.keyIn) [bool] $isCooked = (-not $options.noEchoBack) -and (-not $options.keyIn)
function writeTTY ($text) { function writeTTY ($text) {
execWithTTY ('Write-Host ''' + ($text -replace '''', '''''') + ''' -NoNewline') execWithTTY ('Write-Host ''' + ($text -replace '''', '''''') + ''' -NoNewline') | Out-Null
$script:isInputLine = $True $script:isInputLine = $True
} }
@ -69,35 +69,32 @@ if ($options.noEchoBack -and (-not $options.keyIn) -and ($options.mask -eq '*'))
if ($options.keyIn) { $reqSize = 1 } if ($options.keyIn) { $reqSize = 1 }
while ($True) { while ($True) {
if ($isCooked) { if (-not $isCooked) {
$chunk = execWithTTY 'Read-Host' $True
$chunk += "`n"
} else { # raw
$chunk = execWithTTY '[System.Console]::ReadKey($True).KeyChar' $True $chunk = execWithTTY '[System.Console]::ReadKey($True).KeyChar' $True
$chunk = $chunk -replace '[\r\n]', ''
if ($chunk -eq '') { $isEol = $True } # NL or empty-text was input
} else {
$chunk = execWithTTY 'Read-Host' $True
$chunk = $chunk -replace '[\r\n]', ''
$isEol = $True
} }
if ($chunk -eq '') { break }
# other ctrl-chars # other ctrl-chars
$chunk = $chunk -replace '[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]', '' $chunk = $chunk -replace '[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]', ''
if ($chunk -eq '') { continue }
if (-not $isCooked) { if ($chunk -ne '' -and (-not $isCooked)) {
$displayTmp = $chunk -replace '[\r\n]', '' if (-not $options.noEchoBack) {
if ($displayTmp -ne '') { writeTTY $chunk
if ($options.noEchoBack) { } elseif ($options.mask -ne '') {
if ($options.mask -eq '') { $displayTmp = '' } writeTTY ($options.mask * $chunk.Length)
else { $displayTmp = $options.mask * $displayTmp.Length }
}
if ($displayTmp -ne '') { writeTTY $displayTmp }
} }
} }
$inputTTY += $chunk $inputTTY += $chunk
if (($inputTTY -match '[\r\n]$') -or if ($isEol -or ($options.keyIn -and ($inputTTY.Length -ge $reqSize))) { break }
($options.keyIn -and ($inputTTY.Length -ge $reqSize))) { break }
} }
if ((-not $isCooked) -and (-not ($options.keyIn -and (-not $isInputLine)))) if ((-not $isCooked) -and (-not ($options.keyIn -and (-not $isInputLine))))
{ execWithTTY 'Write-Host ''''' } # new line { execWithTTY 'Write-Host ''''' | Out-Null } # new line
return '''' + $inputTTY + '''' return '''' + $inputTTY + ''''

View file

@ -241,7 +241,7 @@ function readlineExt(options) {
res = _execFileSync(options, execOptions); res = _execFileSync(options, execOptions);
} }
if (!res.error) { if (!res.error) {
res.input = res.input.replace(/^'|'$/g, ''); res.input = res.input.replace(/^\s*'|'\s*$/g, '');
options.display = ''; options.display = '';
} }