Files
BakNRet/.scratch/_probe/tgwin2-probe.ps1
T
Shuery 60f2ae3e0f docs: TUI 与入口重组的设计记录(4 条 ADR + 验收锚点)
grilling 三轮把设计树走完了,这里把它落成文件,避免决策只活在对话里。

ADR-0010 零依赖自研 TUI:三个候选库全部实测排除 —— ConsoleGuiTools 的 PSGallery 元数据声明最低 7.2(5.1 装不上);Spectre.Console 0.49.1 能在 5.1 加载但要塞 4 个第三方 DLL 且不支持鼠标;Terminal.Gui 1.15.0 能反射接线到真的渲染出窗口,但 Application.Shutdown() 在两端都抛 NRE。同时记下两条实测坑:无控制台时 $Host.UI.SupportsVirtualTerminal 会撒谎返回 True(第一道闸门必须是 [Console]::IsOutputRedirected),以及 5.1 的 RawUI.LengthInBufferCells 对中文返回 2、对边框字符返回 6(都不能用来排版)。

ADR-0011 进度回调这个例外:它是注入点不是状态,无头路径为 $null 时行为与今天完全一致。

ADR-0012 入口改名与垫片:这次留垫片而 Common.psm1 直接删,是因为前者是外部接口(断了是静默没用)后者是内部实现(断了当场报错)。并纠正了自己的一个伪前提 —— 本机实测没有注册任何 BakNRet 计划任务。

ADR-0013 TUI 写配置:外科式改写 + 校验通过才原子替换。硬事实是 SoftwareCatalog.psd1 根本不能被 Import-PowerShellDataFile 读入(动态表达式),任何"解析成对象再序列化"的方案都会毁掉表达式与注释。

.scratch/tui/spec.md 是这次改造的验收锚点:9 条已定决策 + 三轮的判据 + 风险表。

验收:Parse 两版仍绿。
2026-09-27 15:27:01 +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 }