整个 TUI 的地基:位置、对齐、边框都靠它。而它算错了**不会报错** —— 表现只是菜单右边框歪一点、光标偏一列,所以每条边界都在断言里钉住。 为什么必须自己算:.Length 是 UTF-16 code unit 的个数(汉字 1 个 char、2 列);而 $Host.UI.RawUI.LengthInBufferCells 在 PowerShell 7 上对、在 5.1 上错(实测 中文 返回 2、字体边框字符 返回 6),且不抛错 —— 这种"一边对一边错"的 API 比两边都错更危险。 表里三个边界是实测踩过的:半角片假名 U+FF61–FF9F 是 1 列(不能划进全角区);全角拉丁 U+FF00–FF60 是 2 列;emoji 是代理对,按一个码点算。 验收:断言 8 条(含空串与控制字符);test.ps1 9/9 全绿(5.1 与 7)。
97 lines
5.7 KiB
PowerShell
97 lines
5.7 KiB
PowerShell
param([Parameter(Mandatory=$true)][string]$OutFile, [Parameter(Mandatory=$true)][string]$Work, [int]$HoldSeconds = 4)
|
|
$lines = New-Object System.Collections.Generic.List[string]
|
|
function L($s) { $lines.Add($s); [System.IO.File]::WriteAllLines($OutFile, $lines, (New-Object System.Text.UTF8Encoding($false))) }
|
|
L ("PSVersion: " + $PSVersionTable.PSVersion + " CLR " + [System.Environment]::Version)
|
|
L ("IsOutputRedirected: " + [Console]::IsOutputRedirected)
|
|
L ""
|
|
$nstack = Join-Path $Work 'nstack111\lib\netstandard2.0\NStack.dll'
|
|
$tg = Join-Path $Work 'tg115\lib\net472\Terminal.Gui.dll'
|
|
$sc = Join-Path $Work 'sc049\lib\netstandard2.0\Spectre.Console.dll'
|
|
L "############ A) Terminal.Gui 1.15.0 (lib/net472) ############"
|
|
$tgOk = $false
|
|
try {
|
|
Add-Type -Path $nstack -ErrorAction Stop
|
|
L " Add-Type -Path NStack.Core 1.1.1 (netstandard2.0) : OK"
|
|
Add-Type -Path $tg -ErrorAction Stop
|
|
L " Add-Type -Path Terminal.Gui 1.15.0 (net472) : OK"
|
|
foreach ($a in ([AppDomain]::CurrentDomain.GetAssemblies() | Where-Object { $_.GetName().Name -in @('NStack', 'Terminal.Gui', 'System.Management') })) {
|
|
L (" loaded assembly: " + $a.GetName().Name + " v" + $a.GetName().Version + " <- " + $(try { $a.Location } catch { 'dynamic' }))
|
|
}
|
|
$appType = [Type]::GetType('Terminal.Gui.Application, Terminal.Gui')
|
|
L (" [Type]::GetType('Terminal.Gui.Application, Terminal.Gui') -> " + $(if ($appType) { 'FOUND ' + $appType.AssemblyQualifiedName.Split(',')[0] } else { 'NULL' }))
|
|
if ($appType) {
|
|
if ([Console]::IsOutputRedirected) { L " SKIP Init()/paint: stdout is redirected (no real console)" }
|
|
else {
|
|
$initM = $appType.GetMethod('Init', [Type[]]@())
|
|
L (" Application.Init() method: " + [bool]$initM)
|
|
$initM.Invoke($null, @()) | Out-Null
|
|
$tgOk = $true
|
|
L " Application.Init() -> SUCCEEDED *** Terminal.Gui is LIVE inside this PowerShell ***"
|
|
$winT = [Type]::GetType('Terminal.Gui.Window, Terminal.Gui')
|
|
$lblT = [Type]::GetType('Terminal.Gui.Label, Terminal.Gui')
|
|
$btnT = [Type]::GetType('Terminal.Gui.Button, Terminal.Gui')
|
|
$lbl2T = [Type]::GetType('Terminal.Gui.Label, Terminal.Gui')
|
|
L (" types: Window=$([bool]$winT) Label=$([bool]$lblT) Button=$([bool]$btnT)")
|
|
$w = [Activator]::CreateInstance($winT, [object[]]@("BakNRet - Terminal.Gui on PS $($PSVersionTable.PSVersion.Major).$($PSVersionTable.PSVersion.Minor)"))
|
|
$l1 = [Activator]::CreateInstance($lblT, [object[]]@('Window drawn by Terminal.Gui 1.15.0 (net472)'))
|
|
$l2 = [Activator]::CreateInstance($lbl2T, [object[]]@('CJK check: Chinese chars, box-drawing'))
|
|
$b1 = [Activator]::CreateInstance($btnT, [object[]]@('OK'))
|
|
$w.Add($l1); $w.Add($l2); $w.Add($b1)
|
|
$w.X = 1; $w.Y = 1; $w.Width = 62; $w.Height = 9
|
|
$l1.Y = 1; $l2.Y = 2; $b1.Y = 4
|
|
$appType::TopLevel = $w
|
|
$w.SetFocus()
|
|
$w.Redraw($w.Bounds)
|
|
$appType::Refresh()
|
|
L " Window + 2 Labels + Button added, TopLevel set, Redraw+Refresh called (look at the window)"
|
|
Start-Sleep -Seconds $HoldSeconds
|
|
$shut = $appType.GetMethod('Shutdown', [Type[]]@())
|
|
$shut.Invoke($null, @()) | Out-Null
|
|
L " Application.Shutdown() -> called cleanly"
|
|
}
|
|
}
|
|
} catch {
|
|
L (" FAILED: " + $_.Exception.GetType().Name + ': ' + $_.Exception.Message)
|
|
$ie = $_.Exception.InnerException; while ($ie) { L (" inner: " + $ie.GetType().Name + ': ' + $ie.Message); $ie = $ie.InnerException }
|
|
$le = $_.Exception.LoaderExceptions; if ($le) { foreach ($x in $le) { L (" loaderEx: " + $x.Message) } }
|
|
$tgOk = $false
|
|
}
|
|
L ""
|
|
L "############ B) Spectre.Console 0.49.1 (lib/netstandard2.0) ############"
|
|
$scOk = $false
|
|
try {
|
|
Add-Type -Path $sc -ErrorAction Stop
|
|
L " Add-Type -Path Spectre.Console 0.49.1 (netstandard2.0) -> OK"
|
|
$ansiT = [Type]::GetType('Spectre.Console.AnsiConsole, Spectre.Console')
|
|
L (" Spectre.Console.AnsiConsole -> " + $(if ($ansiT) { 'FOUND' } else { 'NULL' }))
|
|
if ($ansiT) {
|
|
$m = $ansiT.GetMethod('Markup', [Type[]]@([string]))
|
|
L (" AnsiConsole.Markup(string) : " + [bool]$m)
|
|
$m.Invoke($null, [object[]]@('[red]red markup[/] [green]green[/] [bold]bold[/]')) | Out-Null
|
|
L " Markup(red/green/bold) INVOKED"
|
|
$tableT = [Type]::GetType('Spectre.Console.Table, Spectre.Console')
|
|
$t = [Activator]::CreateInstance($tableT)
|
|
$t.AddColumn('Item'); $t.AddColumn('Size')
|
|
$t.AddRow('alpha', '1.2 GB'); $t.AddRow('beta', 'OK')
|
|
$ansiT::Write($t)
|
|
L " AnsiConsole.Write(Table with 2 cols, 2 rows) INVOKED"
|
|
$ansiT::Write($t)
|
|
L " (table drawn a second time)"
|
|
foreach ($n in @('Spectre.Console.ConfirmationPrompt', 'Spectre.Console.SelectionPrompt`1', 'Spectre.Console.MultiSelectionPrompt`1', 'Spectre.Console.Progress', 'Spectre.Console.Status', 'Spectre.Console.Live', 'Spectre.Console.TextPrompt`1', 'Spectre.Console.Layout', 'Spectre.Console.Panel', 'Spectre.Console.BarChart', 'Spectre.Console.BreakdownChart', 'Spectre.Console.Tree', 'Spectre.Console.Rule', 'Spectre.Console.Exception`1')) {
|
|
$tt = [Type]::GetType("$n, Spectre.Console")
|
|
L (" primitive " + $n.PadRight(42) + " -> " + $(if ($tt) { 'AVAILABLE' } else { 'not found' }))
|
|
}
|
|
$scOk = $true
|
|
}
|
|
} catch {
|
|
L (" FAILED: " + $_.Exception.GetType().Name + ': ' + $_.Exception.Message)
|
|
$ie = $_.Exception.InnerException; while ($ie) { L (" inner: " + $ie.GetType().Name + ': ' + $ie.Message); $ie = $ie.InnerException }
|
|
}
|
|
L ""
|
|
L "RESULT: TerminalGuiInitOK=$tgOk SpectreConsoleOK=$scOk"
|
|
if (-not [Console]::IsOutputRedirected) {
|
|
Write-Host ""
|
|
Write-Host ("[loadtest PS {0}] TerminalGui={1} Spectre.Console={2} -- window closes in {3}s" -f $PSVersionTable.PSVersion, $tgOk, $scOk, $HoldSeconds) -ForegroundColor Yellow
|
|
Start-Sleep -Seconds $HoldSeconds
|
|
}
|