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 }