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

56 lines
3.6 KiB
PowerShell

param([Parameter(Mandatory=$true)][string]$OutFile, [Parameter(Mandatory=$true)][string]$Work, [int]$HoldSeconds = 6)
$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 Asm([string]$n) { return [AppDomain]::CurrentDomain.GetAssemblies() | Where-Object { $_.GetName().Name -eq $n } | Select-Object -First 1 }
L ("PSVersion: " + $PSVersionTable.PSVersion)
try {
Add-Type -Path (Join-Path $Work 'nstack111\lib\netstandard2.0\NStack.dll') -ErrorAction Stop
Add-Type -Path (Join-Path $Work 'tg115\lib\net472\Terminal.Gui.dll') -ErrorAction Stop
$tgAsm = Asm 'Terminal.Gui'
$drvT = $tgAsm.GetType('Terminal.Gui.NetDriver')
$mlT = $tgAsm.GetType('Terminal.Gui.IMainLoopDriver')
$appT = $tgAsm.GetType('Terminal.Gui.Application')
$loopT = $tgAsm.GetType('Terminal.Gui.NetMainLoop')
$drv = [Activator]::CreateInstance($drvT)
$loop = ($loopT.GetConstructors([System.Reflection.BindingFlags]'Public,NonPublic,Instance') | Select-Object -First 1).Invoke(@($drv))
$appT.GetMethod('Init', [Type[]]@($drvT, $mlT)).Invoke($null, @($drv, $loop)) | Out-Null
L " Application.Init(NetDriver, NetMainLoop) -> SUCCEEDED"
$winT = $tgAsm.GetType('Terminal.Gui.Window')
$lblT = $tgAsm.GetType('Terminal.Gui.Label')
$btnT = $tgAsm.GetType('Terminal.Gui.Button')
# ALL parameterless ctors -> the interop-safe path
$w = [Activator]::CreateInstance($winT); L (" Window() parameterless -> " + ($w -ne $null))
$l1 = [Activator]::CreateInstance($lblT); L (" Label() parameterless -> " + ($l1 -ne $null))
$b1 = [Activator]::CreateInstance($btnT); L (" Button() parameterless -> " + ($b1 -ne $null))
# set text via .Text property
$textProp = $lblT.GetProperty('Text')
L (" Label.Text property type: " + $textProp.PropertyType.FullName)
$usT = (Asm 'NStack').GetType('NStack.ustring')
$conv = $usT.GetMethod('op_Implicit', [Type[]]@([string]))
$textProp.SetValue($l1, $conv.Invoke($null, @("Terminal.Gui 1.15.0 on PowerShell $($PSVersionTable.PSVersion)")), $null)
L (" Label.Text set via ustring implicit conversion -> OK: " + $l1.Text.ToString())
$titleProp = $winT.GetProperty('Title')
if ($titleProp) { $titleProp.SetValue($w, $conv.Invoke($null, @('BakNRet TUI')), $null); L (" Window.Title set -> OK: " + $w.Title.ToString()) }
$l2 = [Activator]::CreateInstance($lblT)
$textProp.SetValue($l2, $conv.Invoke($null, @('CJK cell test: 2 CJK chars occupy 4 columns')), $null)
$w.Add($l1); $w.Add($l2); $w.Add($b1)
$w.X = 1; $w.Y = 1; $w.Width = 80; $w.Height = 9
$l1.Y = 1; $l2.Y = 3; $b1.Y = 5; $b1.X = 2
$appT.GetProperty('Top').SetValue($null, $w)
$appT.GetProperty('Current').SetValue($null, $w)
$w.SetFocus(); $w.Redraw($w.Bounds)
$appT.GetMethod('Refresh', [Type[]]@()).Invoke($null, @()) | Out-Null
L " *** WORKING Terminal.Gui TUI: Window(80x9) + 2 Labels (via ustring) + Button ***"
L "RESULT: TerminalGui=WORKS on PowerShell $($PSVersionTable.PSVersion)"
Write-Host ""
Write-Host (" ==== Terminal.Gui 1.15.0 rendered by PowerShell $($PSVersionTable.PSVersion) ====") -ForegroundColor Cyan
Start-Sleep -Seconds $HoldSeconds
$appT.GetMethod('Shutdown', [Type[]]@()).Invoke($null, @()) | Out-Null
L " Application.Shutdown() -> clean"
} catch {
L (" FAILED: " + $_.Exception.GetType().Name + ': ' + $_.Exception.Message)
$ie = $_.Exception.InnerException; while ($ie) { L (" inner: " + $ie.GetType().Name + ': ' + ($ie.Message -split "`n")[0]); $ie = $ie.InnerException }
L "RESULT: TerminalGui=FAILED"
}
if (-not [Console]::IsOutputRedirected) { Start-Sleep -Seconds 3 }