Files
BakNRet/.scratch/_probe/width-probe2.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

71 lines
3.8 KiB
PowerShell

param([Parameter(Mandatory=$true)][string]$HostLabel, [Parameter(Mandatory=$true)][string]$OutFile, [int]$WatchSeconds = 2)
$lines = New-Object System.Collections.Generic.List[string]
function L($s) { $lines.Add($s) }
function MK([int[]]$cp) { -join ($cp | ForEach-Object { [char]$_ }) }
$zh2 = MK @(0x4E2D, 0x6587) # 2 CJK
$zh4 = MK @(0x4E2D, 0x6587, 0x5907, 0x4EFD) # 4 CJK
$ascii8 = '12345678'
$emoji = [char]::ConvertFromUtf32(0x1F600)
$box3 = MK @(0x250C, 0x2500, 0x2510) # box drawing light
$fullw = MK @(0xFF21, 0xFF22) # fullwidth latin A B
$halfk = MK @(0xFF71, 0xFF72) # halfwidth katakana
L "===== DISPLAY-WIDTH PROBE [$HostLabel] ====="
L ("PSVersion : " + $PSVersionTable.PSVersion)
L ("PSEdition : " + $PSVersionTable.PSEdition)
L ("IsOutputRedirected : " + [Console]::IsOutputRedirected)
L ("Console.OutputEncoding : " + [Console]::OutputEncoding.WebName + " (CP " + [Console]::OutputEncoding.CodePage + ")")
L ("`$OutputEncoding : " + $OutputEncoding.WebName)
try { L ("Console.WindowWidth : " + [Console]::WindowWidth) } catch { L ("Console.WindowWidth THREW") }
try { L ("RawUI.WindowSize : " + $Host.UI.RawUI.WindowSize.Width + 'x' + $Host.UI.RawUI.WindowSize.Height) } catch { L ("RawUI.WindowSize THREW") }
L ""
L "--- .Length / Measure-Object -Character / CODEPOINTS ---"
$cases = @(
,@('8 ASCII ', $ascii8)
,@('2 CJK ', $zh2)
,@('4 CJK ', $zh4)
,@('3 boxdraw ', $box3)
,@('1 emoji ', $emoji)
,@('2 fullwidth', $fullw)
,@('2 halfkana ', $halfk)
)
foreach ($c in $cases) {
$nm = $c[0]; $s = $c[1]
$s = [string]$s
$cps = ($s.ToCharArray() | ForEach-Object { 'U+{0:X4}' -f [int]$_ }) -join ' '
L (" $nm .Length=" + $s.Length + " Measure-Object-Character=" + (($s | Measure-Object -Character).Characters) + " cps=[$cps]")
}
L ""
L "--- BUILT-IN DISPLAY-WIDTH FUNCTION? ---"
$found = @()
foreach ($c in (Get-Command -CommandType Cmdlet,Function -ErrorAction SilentlyContinue)) {
if ($c.Name -match 'Width|Wide') { $found += ($c.Name + '(' + $c.CommandType + '/' + $c.Source + ')') }
}
L (" commands matching *Width*|*Wide* : " + $(if ($found.Count) { $found -join ', ' } else { 'NONE' }))
L (" [System.Globalization.StringInfo]::ParseCombiningCharacters(2CJK).Count = " + [System.Globalization.StringInfo]::ParseCombiningCharacters($zh2).Count + " (text elements, not columns)")
L ""
L "--- MEASURED COLUMNS (cursor teleport to column 0 of row 12, write, read CursorLeft) ---"
$skip = $false
try { if ([Console]::IsOutputRedirected) { L " SKIPPED (redirected)"; $skip = $true } } catch { $skip = $true }
if (-not $skip) {
$row = 12
if ([Console]::BufferWidth -lt 40) { $row = 5 }
$vis = [Console]::CursorVisible
try { [Console]::CursorVisible = $false } catch {}
foreach ($c in $cases) {
$nm = $c[0]; $s = [string]$c[1]
try {
[Console]::SetCursorPosition(0, $row)
[Console]::Write((' ' * 60))
[Console]::SetCursorPosition(0, $row)
[Console]::Write($s)
$col = [Console]::CursorLeft
L (" write [$nm] at col 0 -> CursorLeft=" + $col + " => title column width = " + $col + " (.Length=" + $s.Length + ")")
} catch { L (" write [$nm] THREW: " + $_.Exception.Message) }
}
try { [Console]::SetCursorPosition(0, $row); [Console]::Write((' ' * 60)); [Console]::SetCursorPosition(0, $row); [Console]::Write($zh2 + 'X'); [Console]::SetCursorPosition(0, $row + 1) } catch {}
try { [Console]::CursorVisible = $vis } catch {}
}
[System.IO.File]::WriteAllLines($OutFile, $lines, (New-Object System.Text.UTF8Encoding($false)))
Write-Host ("[width-probe2 {0}] wrote {1}" -f $HostLabel, $OutFile) -ForegroundColor Yellow
if ($WatchSeconds -gt 0) { Start-Sleep -Seconds $WatchSeconds }