Files
BakNRet/.scratch/_probe/width-probe.ps1
T
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

61 lines
4.1 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) }
$zh = [string]::Concat([char]0x4E2D, [char]0x6587)
$zh2 = [string]::Concat([char]0x4E2D, [char]0x6587, [char]0x5907, [char]0x4EFD)
$ascii8 = '12345678'
$emoji = [string]::Concat([char]::ConvertFromUtf32(0x1F600))
$box = [string]::Concat([char]0x250C, [char]0x2500, [char]0x2510)
$boxOdd = [string]::Concat([char]0x250C, [char]0x2502, [char]0x2510)
L "===== Chinese / fullwidth width measurement [$HostLabel] ====="
L ("PSVersion : " + $PSVersionTable.PSVersion)
L ("Console.WindowWidth : " + [Console]::WindowWidth)
L ("Console.BufferWidth : " + [Console]::BufferWidth)
try { L ("RawUI.WindowSize : " + $Host.UI.RawUI.WindowSize.Width + 'x' + $Host.UI.RawUI.WindowSize.Height) } catch { L ("RawUI.WindowSize THREW: " + $_.Exception.Message) }
L ("Console.OutputEncoding : " + [Console]::OutputEncoding.WebName)
L ("IsOutputRedirected : " + [Console]::IsOutputRedirected)
L ""
L ".Length (CHARACTER COUNT) vs REAL COLUMNS:"
foreach ($item in @(@('ascii 8 chars ', $ascii8), @('Chinese 2 chars', $zh), @('Chinese 4 chars', $zh2), @('box-drawing 3 ', $box), @('emoji 1 ', $emoji))) {
$name = $item[0]; $s = $item[1]
$chars = $s.ToCharArray()
$codes = ($chars | ForEach-Object { 'U+{0:X4}' -f [int]$_ }) -join ' '
$surr = if ($s.Length -gt 0) { ($s.ToCharArray() | Where-Object { [int]$_ -ge 0xD800 -and [int]$_ -le 0xDFFF }).Count } else { 0 }
L (" $name : .Length=$($s.Length) UTF16-surrogates=$surr Measure-Object -Character=$(($s | Measure-Object -Character).Characters) codeunits=$codes")
}
$ms = [string]::Concat($zh, ':', [System.IO.File]::ReadAllText((Join-Path $env:TEMP 'nowhere.txt')) ) 2>$null
L ""
L "WHICH BUILT-IN CAN MEASURE DISPLAY WIDTH? (probe of plausible names)"
$candidates = @('Get-DisplayWidth','Get-StringWidth','Measure-String','Get-VisualLength','Get-ConsoleStringWidth','Format-WideString','Get-WCWidth','Get-CharWidth','Get-TextWidth','Measure-Text','Get-EastAsianWidth')
foreach ($c in $candidates) {
$cmd = Get-Command $c -ErrorAction SilentlyContinue
L (" Get-Command $c -> " + $(if ($cmd) { $cmd.CommandType.ToString() + ' ' + $cmd.Source } else { 'NOT FOUND' }))
}
L (" [System.Globalization.StringInfo]::ParseCombiningCharacters('$zh').Count = " + [System.Globalization.StringInfo]::ParseCombiningCharacters($zh).Count + " (text-element count, NOT width)")
L (" TextInfo: CurrentCulture = " + [System.Globalization.CultureInfo]::CurrentCulture.Name)
L (" IsInputRedirected/IsOutputRedirected : " + [Console]::IsInputRedirected + '/' + [Console]::IsOutputRedirected)
L ""
L "CURSOR POSITION TEST (needs a real console; skipped if redirected)"
$skip = $false
try { if ([Console]::IsOutputRedirected) { L " SKIPPED: stdout is redirected, [Console]::SetCursorPosition would throw 'The handle is invalid'"; $skip = $true } } catch { $skip = $true }
if (-not $skip) {
try {
[Console]::CursorVisible = $false
foreach ($pair in @(@('8 ASCII chars ', $ascii8), @('2 CJK chars ', $zh), @('4 CJK chars ', $zh2), @('3 box-drawing ', $box), @('1 emoji ', $emoji))) {
$name = $pair[0]; $s = $pair[1]
[Console]::BufferWidth = [Console]::BufferWidth
[Console]::SetCursorPosition(0, 10)
[Console]::Write($s)
$after = [Console]::CursorLeft
L (" write '$name' from column 0 -> CursorLeft = $after (implied display columns = $after)")
}
[Console]::SetCursorPosition(0, 10); [Console]::Write('.' * 40)
[Console]::SetCursorPosition(0, 10); [Console]::Write($zh); [Console]::Write('X')
L " visual check: expect 'ZhongWenX' where X starts at column 4 (2 CJK = 4 columns)"
[Console]::CursorVisible = $true
} catch { L (" CURSOR TEST THREW: " + $_.Exception.Message) }
}
[System.IO.File]::WriteAllLines($OutFile, $lines, (New-Object System.Text.UTF8Encoding($false)))
Write-Host ("[width-probe {0}] done, wrote {1}" -f $HostLabel, $OutFile) -ForegroundColor Yellow
if ($WatchSeconds -gt 0) { Start-Sleep -Seconds $WatchSeconds }