整个 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)。
110 lines
7.0 KiB
PowerShell
110 lines
7.0 KiB
PowerShell
param([Parameter(Mandatory=$true)][string]$OutFile, [Parameter(Mandatory=$true)][string]$Work, [int]$HoldSeconds = 5)
|
|
$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))) }
|
|
function Braino([string]$m) { return $null }
|
|
L ("PSVersion: " + $PSVersionTable.PSVersion + " CLR " + [System.Environment]::Version + " Redirected=" + [Console]::IsOutputRedirected)
|
|
$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'
|
|
function A([string]$fullName, [string]$asmName) { return ([AppDomain]::CurrentDomain.GetAssemblies() | Where-Object { $_.GetName().Name -eq $asmName }).GetType($fullName) }
|
|
function GetT([string]$fullName) { return ([AppDomain]::CurrentDomain.GetAssemblies() | Where-Object { $_.GetName().Name -eq 'Terminal.Gui' }).GetType($fullName) }
|
|
function GetS([string]$fullName) { return ([AppDomain]::CurrentDomain.GetAssemblies() | Where-Object { $_.GetName().Name -eq 'Spectre.Console' }).GetType($fullName) }
|
|
|
|
L ""
|
|
L "############ QUESTION: after Add-Type -Path, do PS type literals work? ############"
|
|
Add-Type -Path $nstack -ErrorAction Stop
|
|
Add-Type -Path $tg -ErrorAction Stop
|
|
L (" [bool]([Type]::GetType('Terminal.Gui.Application, Terminal.Gui')) = " + [bool]([Type]::GetType('Terminal.Gui.Application, Terminal.Gui')))
|
|
L (" [bool]([Type]::GetType('Terminal.Gui.Application, ' + full)) = " + [bool]([Type]::GetType('Terminal.Gui.Application, ' + (GetT 'Terminal.Gui.Application').AssemblyQualifiedName)))
|
|
L (" script GetT('Terminal.Gui.Application') = " + [bool](GetT 'Terminal.Gui.Application') + " <-- reliable in BOTH hosts")
|
|
$litOk = $false
|
|
try { $litOk = [bool]([Terminal.Gui.Application] -is [Type]) } catch { $litOk = "THREW: " + $_.Exception.Message }
|
|
L (" PS type literal [Terminal.Gui.Application] = " + $litOk)
|
|
|
|
L ""
|
|
L "############ A) Terminal.Gui 1.15.0 (net472) DRIVER-WIRED RUN ############"
|
|
$tgResult = 'not-attempted'
|
|
try {
|
|
$appT = GetT 'Terminal.Gui.Application'
|
|
$netT = GetT 'Terminal.Gui.NetDriver'
|
|
L (" Application type : " + [bool]$appT + " NetDriver type: " + [bool]$netT)
|
|
$initM = $appT.GetMethod('Init', [Type[]]@($netT, ($netT.GetInterfaces() | Where-Object { $_.FullName -eq 'Terminal.Gui.IMainLoopDriver' })[0]))
|
|
L (" Init(NetDriver, IMainLoopDriver) overload: " + [bool]$initM)
|
|
if ([Console]::IsOutputRedirected) {
|
|
L " stdout redirected -> Terminal.Gui CANNOT paint; skipping the live run (documented limitation)"
|
|
try { $initM.Invoke($null, @([Activator]::CreateInstance($netT), $null)) | Out-Null; L " unexpectedly Init'ed" } catch { L (" expected failure: " + $_.Exception.InnerException.Message.Split([char]10)[0]) }
|
|
$tgResult = 'blocked-by-redirect'
|
|
} else {
|
|
$drv = [Activator]::CreateInstance($netT)
|
|
$initM.Invoke($null, @($drv, $null)) | Out-Null
|
|
L " Application.Init(NetDriver) -> SUCCEEDED *** Terminal.Gui 1.15.0 is LIVE inside PowerShell " + $PSVersionTable.PSVersion + " ***"
|
|
$winT = GetT 'Terminal.Gui.Window'; $lblT = GetT 'Terminal.Gui.Label'; $btnT = GetT 'Terminal.Gui.Button'
|
|
$w = [Activator]::CreateInstance($winT, [object[]]@('BakNRet / Terminal.Gui - PS ' + $PSVersionTable.PSVersion))
|
|
$l1 = [Activator]::CreateInstance($lblT, [object[]]@('Terminal.Gui 1.15.0 net472 - running on .NET Framework ' + [System.Environment]::Version))
|
|
$l2 = [Activator]::CreateInstance($lblT, [object[]]@('CJK cell-width check:'))
|
|
$l3 = [Activator]::CreateInstance($lblT, [object[]]@(' 2 CJK chars -> 4 columns'))
|
|
$b1 = [Activator]::CreateInstance($btnT, [object[]]@('OK'))
|
|
$w.Add($l1); $w.Add($l2); $w.Add($l3); $w.Add($b1)
|
|
$w.X = 1; $w.Y = 1; $w.Width = 70; $w.Height = 9
|
|
$l1.Y = 1; $l2.Y = 3; $l3.Y = 4; $b1.Y = 6; $b1.X = 2
|
|
$homeT = GetT 'Terminal.Gui.Toplevel'
|
|
($appT.GetProperty('TopLevel')).SetValue($null, $w)
|
|
$w.SetFocus()
|
|
$w.Redraw($w.Bounds)
|
|
$appT.GetMethod('Refresh', [Type[]]@()).Invoke($null, @()) | Out-Null
|
|
L " Window(70x9) + 3 Labels + Button painted. Holding " + $HoldSeconds + "s..."
|
|
Start-Sleep -Seconds $HoldSeconds
|
|
$appT.GetMethod('Shutdown', [Type[]]@()).Invoke($null, @()) | Out-Null
|
|
L " Application.Shutdown() -> clean"
|
|
$tgResult = 'WORKS'
|
|
}
|
|
} catch {
|
|
L (" Terminal.Gui FAILED: " + $_.Exception.GetType().Name + ': ' + $_.Exception.Message)
|
|
$ie = $_.Exception.InnerException; while ($ie) { L (" inner: " + $ie.GetType().Name + ': ' + ($ie.Message -split "`n")[0]); $ie = $ie.InnerException }
|
|
$tgResult = 'FAILED'
|
|
}
|
|
|
|
L ""
|
|
L "############ B) Spectre.Console 0.49.1 (netstandard2.0) REAL RENDERING ############"
|
|
$scResult = 'not-attempted'
|
|
try {
|
|
Add-Type -Path $sc -ErrorAction Stop
|
|
$ansiT = GetS 'Spectre.Console.AnsiConsole'
|
|
$tableT = GetS 'Spectre.Console.Table'
|
|
$extT = GetS 'Spectre.Console.TableExtensions'
|
|
L (" AnsiConsole/Table/TableExtensions types: " + [bool]$ansiT + "/" + [bool]$tableT + "/" + [bool]$extT)
|
|
# Build a table WITHOUT AddRow extension methods (instance API only) to isolate extension-call support
|
|
$t = [Activator]::CreateInstance($tableT)
|
|
$colT = GetS 'Spectre.Console.TableColumn'
|
|
$t.AddColumn([Activator]::CreateInstance($colT, [object[]]@('项目 Item')))
|
|
$t.AddColumn([Activator]::CreateInstance($colT, [object[]]@('大小 Size')))
|
|
L " 2 columns added via INSTANCE AddColumn(TableColumn) -> OK"
|
|
# extension method called the PowerShell way
|
|
$addRow = $extT.GetMethod('AddRow', [Type[]]@($tableT, [string[]]))
|
|
L (" found extension TableExtensions.AddRow(Table, string[]): " + [bool]$addRow)
|
|
$addRow.Invoke($null, [object[]]@($t, [string[]]@('中文备份 alpha', '1.2 GB'))) | Out-Null
|
|
$addRow.Invoke($null, [object[]]@($t, [string[]]@('beta', 'OK'))) | Out-Null
|
|
L " 2 rows added via reflection-invoked EXTENSION method -> OK"
|
|
$ansiT::Write($t)
|
|
L " >>> AnsiConsole.Write(Table) with mixed-width CJK content rendered above <<<"
|
|
try { $ansiT::Markup('[red]red[/] [green]green[/] [bold]bold[/] [yellow]yellow[/]') ; L " AnsiConsole.Markup color test -> OK" } catch { L (" Markup FAILED: " + $_.Exception.Message) }
|
|
Write-Host ""
|
|
Write-Host " -- Spectre.Console table rendered from PowerShell $($PSVersionTable.PSVersion) --" -ForegroundColor Cyan
|
|
$ansiT::Write($t)
|
|
Start-Sleep -Seconds 3
|
|
# Progress demo (bounded)
|
|
try {
|
|
$progT = GetS 'Spectre.Console.Progress'
|
|
L (" Progress type: " + [bool]$progT)
|
|
$act = { }
|
|
L " (Progress/Status need a script-block -> Action`1 delegate; skipped live run to stay bounded)"
|
|
} catch { L (" progress probe failed: " + $_.Exception.Message) }
|
|
$scResult = 'WORKS'
|
|
} catch {
|
|
L (" Spectre FAILED: " + $_.Exception.GetType().Name + ': ' + $_.Exception.Message)
|
|
$ie = $_.Exception.InnerException; while ($ie) { L (" inner: " + $ie.GetType().Name + ': ' + ($ie.Message -split "`n")[0]); $ie = $ie.InnerException }
|
|
$scResult = 'FAILED'
|
|
}
|
|
L ""
|
|
L "RESULT: TerminalGui=$tgResult SpectreConsole=$scResult"
|