Fix: PowerShell fails when the key is pushed before it is ready (#19)

This commit is contained in:
anseki 2015-08-31 02:14:21 +09:00
parent 274d13a44b
commit c3b717b5b5
2 changed files with 16 additions and 8 deletions

View file

@ -45,14 +45,20 @@ foreach ($arg in $argList) {
# **NOTE** Don't include special characters of DOS in $command when $getRes is True.
# [string] $cmdPath = $Env:ComSpec
# [string] $psPath = 'powershell.exe'
function execWithTTY ($command, $getRes = $False) {
function execWithTTY ($command, $getRes = $False, $errorThrow = $False) {
if ($getRes) {
$res = (cmd.exe /C "<CON powershell.exe -Command $command")
if ($LastExitCode -ne 0) { exit $LastExitCode }
if ($LastExitCode -ne 0) {
if ($errorThrow) { throw $LastExitCode }
else { exit $LastExitCode }
}
return $res
} else {
$command | cmd.exe /C ">CON powershell.exe -Command -"
if ($LastExitCode -ne 0) { exit $LastExitCode }
if ($LastExitCode -ne 0) {
if ($errorThrow) { throw $LastExitCode }
else { exit $LastExitCode }
}
}
}
@ -67,10 +73,12 @@ if ($options.display) {
if ($options.displayOnly) { return "''" }
if (-not $options.keyIn -and $options.hideEchoBack -and $options.mask -eq '*') {
$inputTTY = execWithTTY ('$text = Read-Host -AsSecureString;' +
'$bstr = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($text);' +
'[Runtime.InteropServices.Marshal]::PtrToStringAuto($bstr)') $True
return '''' + $inputTTY + ''''
try {
$inputTTY = execWithTTY ('$text = Read-Host -AsSecureString;' +
'$bstr = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($text);' +
'[Runtime.InteropServices.Marshal]::PtrToStringAuto($bstr)') $True $True
return '''' + $inputTTY + ''''
} catch {} # ignore
}
if ($options.keyIn) { $reqSize = 1 }

View file

@ -1,6 +1,6 @@
{
"name": "readline-sync",
"version": "1.2.20",
"version": "1.2.21",
"title": "readlineSync",
"description": "Synchronous Readline for interactively running to have a conversation with the user via a console(TTY).",
"keywords": [