Files
Shuery 8a0924330a feat(tui): 控制台列宽表(第一轮第一块)
整个 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)。
2026-09-27 15:31:26 +08:00

117 lines
7.5 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))) }
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'
$mem461 = Join-Path $Work 'system.memory-4.5.5\lib\net461\System.Memory.dll'
$memNs = Join-Path $Work 'system.memory-4.5.5\lib\netstandard2.0\System.Memory.dll'
function Asm([string]$n) { return [AppDomain]::CurrentDomain.GetAssemblies() | Where-Object { $_.GetName().Name -eq $n } | Select-Object -First 1 }
function T([string]$n, [string]$a) { $x = Asm $a; if ($x) { return $x.GetType($n) } else { return $null } }
L ""
L "############ STEP 0: side-load the ONE dependency Spectre.Console needs ############"
$mem = if ($PSVersionTable.PSEdition -eq 'Desktop') { $mem461 } else { $memNs }
L (" chosen System.Memory for this host: " + $mem)
try {
Add-Type -Path $mem -ErrorAction Stop
$m = Asm 'System.Memory'
L (" Add-Type -Path System.Memory -> OK: " + $m.GetName().FullName)
L (" [System.Span[int]] resolves now: " + [bool]([Type]::GetType('System.Span`1')) )
} catch { L (" System.Memory load FAILED: " + $_.Exception.Message) }
L ""
L "############ A) Terminal.Gui 1.15.0 net472 - CORRECTED driver wiring ############"
$tgResult = 'not-attempted'
try {
Add-Type -Path $nstack -ErrorAction Stop
Add-Type -Path $tg -ErrorAction Stop
$appT = T 'Terminal.Gui.Application' 'Terminal.Gui'
$netT = T 'Terminal.Gui.NetDriver' 'Terminal.Gui'
L (" Application: " + [bool]$appT + " NetDriver: " + [bool]$netT)
L (" NetDriver interfaces: " + (($netT.GetInterfaces() | ForEach-Object { $_.FullName }) -join ', '))
$mlT = T 'Terminal.Gui.IMainLoopDriver' 'Terminal.Gui'
L (" IMainLoopDriver found as type: " + [bool]$mlT)
$initM = $appT.GetMethod('Init', [Type[]]@($netT, $mlT))
L (" Init(NetDriver, IMainLoopDriver) overload: " + [bool]$initM)
if ([Console]::IsOutputRedirected) {
L " stdout redirected -> Terminal.Gui cannot paint. Expected failure follows:"
try { $initM.Invoke($null, @([Activator]::CreateInstance($netT), $null)) | Out-Null; L " unexpectedly Init'ed" }
catch { L (" expected: " + ($_.Exception.InnerException.Message -split "`n")[0]) }
$tgResult = 'works-only-with-a-real-console'
} else {
$drv = [Activator]::CreateInstance($netT)
L (" NetDriver instance: " + $drv.GetType().FullName + " is IMainLoopDriver: " + [bool]($mlT.IsInstanceOfType($drv)))
$initM.Invoke($null, @($drv, $drv)) | Out-Null
L " Application.Init(driver, driver) -> SUCCEEDED *** Terminal.Gui is LIVE on PowerShell " + $PSVersionTable.PSVersion + " ***"
IEX "0"
$winT = T 'Terminal.Gui.Window' 'Terminal.Gui'; $lblT = T 'Terminal.Gui.Label' 'Terminal.Gui'; $btnT = T 'Terminal.Gui.Button' 'Terminal.Gui'
$w = [Activator]::CreateInstance($winT, [object[]]@('BakNRet - Terminal.Gui on PS ' + $PSVersionTable.PSVersion))
$l1 = [Activator]::CreateInstance($lblT, [object[]]@('Terminal.Gui 1.15.0 (net472) on ' + $(if ($PSVersionTable.PSEdition -eq 'Desktop') { '.NET Framework ' } else { '.NET ' }) + [System.Environment]::Version))
$l2 = [Activator]::CreateInstance($lblT, [object[]]@('CJK test: 2 CJK chars occupy 4 screen columns'))
$l3 = [Activator]::CreateInstance($lblT, [object[]]@('Box drawing: expected to align'))
$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 = 72; $w.Height = 9
$l1.Y = 1; $l2.Y = 3; $l3.Y = 4; $b1.Y = 6; $b1.X = 2
($appT.GetProperty('TopLevel')).SetValue($null, $w)
$w.SetFocus(); $w.Redraw($w.Bounds)
$appT.GetMethod('Refresh', [Type[]]@()).Invoke($null, @()) | Out-Null
L " Painted Window(72x9)+3 Labels+Button. Holding " + $HoldSeconds + "s for visual confirmation..."
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 - with System.Memory present ############"
$scResult = 'not-attempted'
try {
Add-Type -Path $sc -ErrorAction Stop
$ansiT = T 'Spectre.Console.AnsiConsole' 'Spectre.Console'
$tableT = T 'Spectre.Console.Table' 'Spectre.Console'
$colT = T 'Spectre.Console.TableColumn' 'Spectre.Console'
$extT = T 'Spectre.Console.TableExtensions' 'Spectre.Console'
$t = [Activator]::CreateInstance($tableT)
L " new Table() -> OK *** THIS FAILED BEFORE System.Memory WAS SIDE-LOADED ***"
$t.AddColumn([Activator]::CreateInstance($colT, [object[]]@('项目 / Item')))
$t.AddColumn([Activator]::CreateInstance($colT, [object[]]@('大小 / Size')))
$addRow = $extT.GetMethod('AddRow', [Type[]]@($tableT, [string[]]))
$addRow.Invoke($null, [object[]]@($t, [string[]]@('中文备份归档 alpha', '1.2 GB'))) | Out-Null
$addRow.Invoke($null, [object[]]@($t, [string[]]@('beta', 'OK'))) | Out-Null
Write-Host ""
Write-Host (" === Spectre.Console table, rendered by PowerShell $($PSVersionTable.PSVersion) ===") -ForegroundColor Cyan
$ansiT::Write($t)
Write-Host ""
$ansiT::Markup('[red]red[/] [green]green[/] [yellow]yellow[/] [bold]bold[/] [blue on white]reverse[/]')
L " AnsiConsole.Write(Table + CJK) + Markup colors -> INVOKED"
# Now confirm extension methods callable with PowerShell's native :: syntax
try {
$t2 = [Activator]::CreateInstance($tableT)
$ok1 = $false
try { [Spectre.Console.TableExtensions]::AddColumn($t2, 'ColA') | Out-Null; $ok1 = $true } catch { $ok1 = 'THREW: ' + $_.Exception.Message }
L (" PowerShell-native [Spectre.Console.TableExtensions]::AddColumn(`$table,'ColA') -> " + $ok1)
$t3 = [Activator]::CreateInstance($tableT)
[Spectre.Console.TableExtensions]::AddColumns($t3, [string[]]@('A','B')) | Out-Null
[Spectre.Console.TableExtensions]::AddRow($t3, [string[]]@('x','y')) | Out-Null
L " [Spectre.Console.TableExtensions]::AddColumns/AddRow via PS-native static call -> OK"
$t4 = [Activator]::CreateInstance($tableT)
try { $t4.AddColumns('A','B'); L " INSTANCE SYNTAX `$t4.AddColumns('A','B') -> OK (PS resolves extension methods)" } catch { L (" INSTANCE SYNTAX `$t4.AddColumns -> THREW: " + $_.Exception.Message) }
} catch { L (" native-syntax 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"
if (-not [Console]::IsOutputRedirected) { Write-Host ""; Write-Host ("[final PS " + $PSVersionTable.PSVersion + "] TerminalGui=$tgResult Spectre=$scResult (closing in $HoldSeconds s)") -ForegroundColor Yellow; Start-Sleep -Seconds $HoldSeconds }