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 两版仍绿。
117 lines
7.5 KiB
PowerShell
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 }
|