From b190efc82acecbf74b5ed4dd1e954adb49d73f16 Mon Sep 17 00:00:00 2001 From: ed Date: Tue, 22 Dec 2020 17:20:28 +0100 Subject: [PATCH] workaround powershell quirks --- .gitattributes | 2 +- .vscode/settings.json | 3 +++ clients/powershell.ps1 | 39 +++++++++++++++++++++++++++++---------- 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/.gitattributes b/.gitattributes index 90eaec1..ac7b510 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,3 +1,3 @@ * text eol=lf - +*.ps1 text eol=crlf *.png binary diff --git a/.vscode/settings.json b/.vscode/settings.json index d507be4..7ce5730 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -42,6 +42,9 @@ "[html]": { "editor.formatOnSave": false, }, + "[powershell]": { + "editor.formatOnSave": false, + }, "editor.codeActionsOnSaveTimeout": 9001, "editor.formatOnSaveTimeout": 9001, // diff --git a/clients/powershell.ps1 b/clients/powershell.ps1 index 18d168b..5efea15 100644 --- a/clients/powershell.ps1 +++ b/clients/powershell.ps1 @@ -1,4 +1,11 @@ -# Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted +# usage: +# powershell .\powershell.ps1 127.0.0.1:531 +# (or just doubleclick this script in win10) +# +# fix permissions on win7 by running this in a powershell console: +# Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted + +####################################################################### function Seppuku { Write-Host "" @@ -11,7 +18,7 @@ function Seppuku { function Test-IsISE { try { - return $psISE -ne $null; + return $null -ne $psISE; } catch { return $false; @@ -37,6 +44,17 @@ catch { ####################################################################### +$ver = [Environment]::OSVersion.Version +$ver10 = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").ReleaseId +Write-Host "Windows $ver ($ver10)" + +$v1 = new-object 'Version' 10,0,10586 # OK, 1511 +$v2 = new-object 'Version' 10,0,15063 # OK, 1703 +$scroll_ng = $ver -gt $v1 -and $ver -lt $v2 +# LTSB 2015 OK (<1511), 2016 NG (~1607), LTSC 2019 OK (1809) + +####################################################################### + $r0chost = if ($args.count -ge 1) {$args[0]} else {""} $r0cport = if ($args.count -ge 2) {$args[1]} else {""} $arr = $r0chost.Split(":") @@ -73,15 +91,14 @@ while ($socket.Connected) { $text = [System.Text.Encoding]::UTF8.GetString($buf, 0, $n_read) Write-Host $text -NoNewLine - if ($text -match '\x48\x1B\x44') { + if ($scroll_ng -and $text -match '\x48\x0a\x0a\x1b\x5b\x4b') { $messages_lost += 1 } } if ($messages_lost -gt 0) { - # >2018 - # >still can't get vt100 right - # microsoft in charge of conserving bandwidth + # bad powershell ver, do full redraw $stream.Write([Byte[]] (0x12), 0, 1) + $stream.Flush() $messages_lost = 0 } while ([console]::KeyAvailable) { @@ -107,13 +124,15 @@ while ($socket.Connected) { if ($kc -eq 40) { $bytes = [Byte[]] (0x1b,0x5b,0x42) } # D if ($kc -eq 36) { $bytes = [Byte[]] (0x1b,0x5b,0x31,0x7e) } # Home if ($kc -eq 35) { $bytes = [Byte[]] (0x1b,0x5b,0x34,0x7e) } # End - if ($kc -eq 33) { $bytes = [Byte[]] (0x1b,0x5b,0x35,0x7e,0x12) } # PgUp - if ($kc -eq 34) { $bytes = [Byte[]] (0x1b,0x5b,0x36,0x7e,0x12) } # PgDn + if ($kc -eq 33) { $bytes = [Byte[]] (0x1b,0x5b,0x35,0x7e) } # PgUp + if ($kc -eq 34) { $bytes = [Byte[]] (0x1b,0x5b,0x36,0x7e) } # PgDn + if ($scroll_ng -and ($kc -eq 33 -or $kc -eq 34)) { + $bytes += [Byte] 0x12 + } $stream.Write($bytes, 0, $bytes.Length) $stream.Flush() # 0x12 is ^R meaning we redraw the TUI on every scroll event - # because powershell /usually/ can't scroll - # (it decides to work occasionally) + # for powershell versions with busted scrolling } Start-Sleep -Milliseconds 10 }