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 两版仍绿。
60 lines
3.3 KiB
PowerShell
60 lines
3.3 KiB
PowerShell
$out = Join-Path $env:TEMP 'console-probe.txt'
|
|
$lines = New-Object System.Collections.Generic.List[string]
|
|
function L($s) { $lines.Add($s) }
|
|
$src = @"
|
|
using System;
|
|
using System.Runtime.InteropServices;
|
|
public static class K32 {
|
|
[DllImport("kernel32.dll", SetLastError=true)] public static extern bool AllocConsole();
|
|
[DllImport("kernel32.dll", SetLastError=true)] public static extern bool FreeConsole();
|
|
[DllImport("kernel32.dll")] public static extern IntPtr GetStdHandle(int n);
|
|
[DllImport("kernel32.dll", SetLastError=true)] public static extern bool GetConsoleMode(IntPtr h, out uint m);
|
|
[DllImport("kernel32.dll", SetLastError=true)] public static extern bool SetConsoleMode(IntPtr h, uint m);
|
|
[DllImport("kernel32.dll")] public static extern IntPtr GetConsoleWindow();
|
|
}
|
|
"@
|
|
Add-Type -TypeDefinition $src -ErrorAction Stop
|
|
$hwndBefore = [K32]::GetConsoleWindow()
|
|
L ("PSVersion : " + $PSVersionTable.PSVersion)
|
|
L ("CLR : " + [System.Environment]::Version)
|
|
L ("GetConsoleWindow(before AllocConsole) : " + $hwndBefore)
|
|
$ok = [K32]::AllocConsole()
|
|
L ("AllocConsole() : " + $ok)
|
|
$hOut = [K32]::GetStdHandle(-11); $hIn = [K32]::GetStdHandle(-10)
|
|
$mOut = 0; $mIn = 0
|
|
$okOut = [K32]::GetConsoleMode($hOut, [ref]$mOut)
|
|
$okIn = [K32]::GetConsoleMode($hIn, [ref]$mIn)
|
|
L ("STDOUT GetConsoleMode: " + $okOut + " mode=0x" + ('{0:X}' -f $mOut))
|
|
L ("STDIN GetConsoleMode: " + $okIn + " mode=0x" + ('{0:X}' -f $mIn))
|
|
L (" STDOUT ENABLE_VIRTUAL_TERMINAL_PROCESSING (0x0004) : " + (($mOut -band 4) -eq 4))
|
|
L (" STDOUT ENABLE_PROCESSED_OUTPUT (0x0001) : " + (($mOut -band 1) -eq 1))
|
|
L (" STDOUT ENABLE_WRAP_AT_EOL_OUTPUT (0x0002) : " + (($mOut -band 2) -eq 2))
|
|
L (" STDIN ENABLE_VIRTUAL_TERMINAL_INPUT (0x0200) : " + (($mIn -band 0x200) -eq 0x200))
|
|
$raw = $Host.UI.RawUI
|
|
$vtProp = $raw | Get-Member -Name SupportsVirtualTerminal -ErrorAction SilentlyContinue
|
|
$rawMembers = ($raw | Get-Member -MemberType Property | Select-Object -ExpandProperty Name) -join ','
|
|
L ("RawUI property list : " + $rawMembers)
|
|
L ("RawUI has SupportsVirtualTerminal member? " + [bool]$vtProp)
|
|
try { L ("RawUI.SupportsVirtualTerminal value : [" + $raw.SupportsVirtualTerminal + "]") } catch { L ("RawUI.SupportsVirtualTerminal THREW: " + $_.Exception.Message) }
|
|
try { L ("`$Host.UI.SupportsVirtualTerminal : [" + $Host.UI.SupportsVirtualTerminal + "]") } catch { L ("`$Host.UI.SupportsVirtualTerminal THREW: " + $_.Exception.Message) }
|
|
L ("[Console]::OutputEncoding : " + [Console]::OutputEncoding.WebName)
|
|
L ("[Console]::IsOutputRedirected : " + [Console]::IsOutputRedirected)
|
|
L ("[Console] ReinitCheck : skipped")
|
|
$w = $raw.WindowSize; $b = $raw.BufferSize
|
|
L ("RawUI.WindowSize : " + $w.Width + "x" + $w.Height)
|
|
L ("RawUI.BufferSize : " + $b.Width + "x" + $b.Height)
|
|
# activate VT and re-read
|
|
$okSet = [K32]::SetConsoleMode($hOut, $mOut -bor 4)
|
|
$mOut2 = 0
|
|
[void][K32]::GetConsoleMode($hOut, [ref]$mOut2)
|
|
L ("SetConsoleMode(out, mode|0x4) : " + $okSet + " -> new mode=0x" + ('{0:X}' -f $mOut2))
|
|
$tmp = join-path $env:TEMP 'vt-write-test.txt'
|
|
$lines | Set-Content -Path $tmp -Encoding UTF8
|
|
Write-Host ""
|
|
Write-Host "=== PROBE OUTPUT ON ATTACHED CONSOLE ===" -ForegroundColor Cyan
|
|
Write-Host "Try ANSI: [31mRED[0m [32mGREEN[0m"
|
|
$lines | ForEach-Object { Write-Host $_ }
|
|
Write-Host "=== END ===" -ForegroundColor Cyan
|
|
Start-Sleep -Seconds 2
|
|
[void][K32]::FreeConsole()
|