readline-sync/lib/read.ps1

129 lines
3.8 KiB
PowerShell
Raw Normal View History

2015-04-01 09:34:31 +02:00
# readlineSync
# https://github.com/anseki/readline-sync
#
2018-02-07 04:07:35 +01:00
# Copyright (c) 2018 anseki
2015-04-01 09:34:31 +02:00
# Licensed under the MIT license.
2015-03-25 13:22:44 +01:00
Param(
[string] $display,
2015-04-13 08:55:33 +02:00
[switch] $displayOnly,
2015-03-31 13:27:02 +02:00
[switch] $keyIn,
2015-04-07 11:38:14 +02:00
[switch] $hideEchoBack,
[string] $mask,
[string] $limit,
[switch] $caseSensitive
2015-03-25 13:22:44 +01:00
)
$ErrorActionPreference = 'Stop' # for cmdlet
trap {
# `throw $_` and `Write-Error $_` return exit-code 0
$Host.UI.WriteErrorLine($_)
exit 1
}
function decodeArg ($arg) {
2015-03-25 13:22:44 +01:00
[Regex]::Replace($arg, '#(\d+);', { [char][int] $args[0].Groups[1].Value })
}
$options = @{}
2015-04-13 08:55:33 +02:00
foreach ($arg in @('display', 'displayOnly', 'keyIn', 'hideEchoBack', 'mask', 'limit', 'caseSensitive')) {
$options.Add($arg, (Get-Variable $arg -ValueOnly))
}
$argList = New-Object string[] $options.Keys.Count
$options.Keys.CopyTo($argList, 0)
foreach ($arg in $argList) {
if ($options[$arg] -is [string] -and $options[$arg])
{ $options[$arg] = decodeArg $options[$arg] }
2015-03-25 13:22:44 +01:00
}
[string] $inputTTY = ''
[bool] $silent = -not $options.display -and
2015-04-07 11:38:14 +02:00
$options.keyIn -and $options.hideEchoBack -and -not $options.mask
[bool] $isCooked = -not $options.hideEchoBack -and -not $options.keyIn
2015-03-25 13:22:44 +01:00
# Instant method that opens TTY without CreateFile via P/Invoke in .NET Framework
# **NOTE** Don't include special characters of DOS in $command when $getRes is True.
2015-04-01 09:34:31 +02:00
# [string] $cmdPath = $Env:ComSpec
# [string] $psPath = 'powershell.exe'
2015-08-31 09:46:58 +02:00
function execWithTTY ($command, $getRes = $False, $throwError = $False) {
2015-03-25 13:22:44 +01:00
if ($getRes) {
$res = (cmd.exe /C "<CON powershell.exe -Command $command")
if ($LastExitCode -ne 0) {
2015-08-31 09:46:58 +02:00
if ($throwError) { throw $LastExitCode }
else { exit $LastExitCode }
}
2015-04-01 09:34:31 +02:00
return $res
2015-03-25 13:22:44 +01:00
} else {
$command | cmd.exe /C ">CON powershell.exe -Command -"
if ($LastExitCode -ne 0) {
2015-08-31 09:46:58 +02:00
if ($throwError) { throw $LastExitCode }
else { exit $LastExitCode }
}
2015-03-25 13:22:44 +01:00
}
}
2015-04-01 12:36:00 +02:00
function writeTTY ($text) {
execWithTTY ('Write-Host (''' +
2015-04-08 15:57:52 +02:00
(($text -replace '''', '''''') -replace '[\r\n]', '''+"`n"+''') + ''') -NoNewline')
2015-04-01 12:36:00 +02:00
}
if ($options.display) {
writeTTY $options.display
2015-03-25 13:22:44 +01:00
}
2015-04-13 08:55:33 +02:00
if ($options.displayOnly) { return "''" }
2015-03-25 13:22:44 +01:00
2015-04-07 11:38:14 +02:00
if (-not $options.keyIn -and $options.hideEchoBack -and $options.mask -eq '*') {
2015-08-31 09:46:58 +02:00
# It fails when it's not ready.
try {
$inputTTY = execWithTTY ('$text = Read-Host -AsSecureString;' +
'$bstr = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($text);' +
'[Runtime.InteropServices.Marshal]::PtrToStringAuto($bstr)') $True $True
return '''' + $inputTTY + ''''
} catch {} # ignore
2015-03-25 13:22:44 +01:00
}
if ($options.keyIn) { $reqSize = 1 }
2015-03-25 13:22:44 +01:00
if ($options.keyIn -and $options.limit) {
$limitPtn = '[^' + $options.limit + ']'
}
2015-03-25 13:22:44 +01:00
while ($True) {
2015-03-30 12:36:31 +02:00
if (-not $isCooked) {
$chunk = [char][int] (execWithTTY '[int] [Console]::ReadKey($True).KeyChar' $True)
2015-03-30 12:36:31 +02:00
} else {
$chunk = execWithTTY 'Read-Host' $True
$chunk += "`n"
2015-03-25 13:22:44 +01:00
}
if ($chunk -and $chunk -match '^(.*?)[\r\n]') {
$chunk = $Matches[1]
$atEol = $True
} else { $atEol = $False }
# other ctrl-chars
if ($chunk) { $chunk = $chunk -replace '[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]', '' }
if ($chunk -and $limitPtn) {
2015-04-04 17:37:42 +02:00
if ($options.caseSensitive) { $chunk = $chunk -creplace $limitPtn, '' }
else { $chunk = $chunk -ireplace $limitPtn, '' }
}
2015-03-25 13:22:44 +01:00
if ($chunk) {
if (-not $isCooked) {
2015-04-07 11:38:14 +02:00
if (-not $options.hideEchoBack) {
writeTTY $chunk
} elseif ($options.mask) {
writeTTY ($options.mask * $chunk.Length)
}
2015-03-25 13:22:44 +01:00
}
$inputTTY += $chunk
2015-03-25 13:22:44 +01:00
}
if ((-not $options.keyIn -and $atEol) -or
($options.keyIn -and $inputTTY.Length -ge $reqSize)) { break }
2015-03-25 13:22:44 +01:00
}
if (-not $isCooked -and -not $silent) { execWithTTY 'Write-Host ''''' } # new line
2015-03-25 13:22:44 +01:00
return "'$inputTTY'"