The instruments that measured the two lies, on the box itself
`squid-platform::console` now refuses to trust a console's answer about its own size or about the price of a glyph. It is entitled to that refusal only because somebody measured both, on the machine, with scripts that could not cheat. `win-tui-resize.py` grows the console *underneath* a running interface and reads the screen back, then asks how many columns the right-hand borders sit in: one column means a screen, two means two frames drawn on top of each other. That is the resize bug made countable, and its header keeps the five facts about Windows OpenSSH that shaped it — including that the resize has to be driven on `CONOUT$`, because after the alternate screen went up, the process asking for the resize is still describing the buffer nobody is looking at. `win-tui-widths.ps1` is the same trick applied to a single character: park the cursor on a known column, write one glyph, read the cursor back, and the cells it travelled are that glyph's width *in this console*. It measures both write paths separately — `WriteConsoleW` into the buffer, and encoded bytes through stdout — because they genuinely disagree, and it reads the cell's contents back too, so a character the code page quietly replaced cannot hide inside a width that happens to look right. This is the measurement `symbol_cells` now performs in-process; the script is what proved the answer was worth asking for, and it still answers for a console the program cannot see. `win-tui-clean.ps1` keeps the harness honest: a remote PowerShell left behind by a dropped ssh session keeps the release binary's file handle open, so the next build cannot overwrite it, and it later writes its `MARK_QUIT` into whichever report file exists — a previous round testifying on behalf of this one. It kills the tested binary and its own grower by command line, and nothing else.main
parent
8fd235b961
commit
dbc06c77cb
|
|
@ -0,0 +1,30 @@
|
|||
# 清掉上一轮测试留在机器上的进程。测试台在每次运行前后各调一次。
|
||||
#
|
||||
# powershell -NoProfile -ExecutionPolicy Bypass -File win-tui-clean.ps1
|
||||
#
|
||||
# 为什么需要:测试台是通过 ssh 起的,本地 Ctrl-C 或 ssh 断掉并不会让远端那条
|
||||
# PowerShell 结束 —— 它会一直卡在 WaitForExit 上,而它的子进程(squid.exe)
|
||||
# 则一直占着那块控制台和一个 release 版 exe 文件句柄。后果有两个:
|
||||
# 编译产物覆盖不了,以及旧进程会往报告文件里补写 MARK_QUIT,
|
||||
# 让下一轮读到一份不属于这一轮的事实。
|
||||
#
|
||||
# 只杀两类,且都按命令行匹配,不碰用户自己开的窗口:
|
||||
# * squid.exe(被测程序本身);
|
||||
# * 命令行里带 win-tui-grow.ps1 的 powershell.exe(测试台自己),本进程除外。
|
||||
$targets = @()
|
||||
$binaries = Get-Process -Name squid -ErrorAction SilentlyContinue
|
||||
foreach ($process in $binaries) {
|
||||
$targets += [pscustomobject]@{ Id = $process.Id; Why = "squid.exe" }
|
||||
}
|
||||
|
||||
$growers = Get-CimInstance -ClassName Win32_Process -Filter "Name = 'powershell.exe'" |
|
||||
Where-Object { $_.ProcessId -ne $PID -and $_.CommandLine -match 'win-tui-grow\.ps1' }
|
||||
foreach ($process in $growers) {
|
||||
$targets += [pscustomobject]@{ Id = $process.ProcessId; Why = "win-tui-grow.ps1" }
|
||||
}
|
||||
|
||||
foreach ($target in $targets) {
|
||||
Write-Output "KILLED $($target.Why) pid=$($target.Id)"
|
||||
Stop-Process -Id $target.Id -Force -ErrorAction SilentlyContinue
|
||||
}
|
||||
if ($targets.Count -eq 0) { Write-Output "CLEAN nothing to kill" }
|
||||
|
|
@ -0,0 +1,194 @@
|
|||
# 在程序运行的过程中把它所依附的控制台改大,给 scripts/win-tui-resize.py 用。
|
||||
#
|
||||
# powershell -NoProfile -ExecutionPolicy Bypass -File win-tui-grow.ps1 `
|
||||
# 100 30 180 50 10 en C:\dev\target\release\squid.exe `
|
||||
# C:\Users\kangwj\report-1700.txt C:\Users\kangwj\trace-1700.log
|
||||
#
|
||||
# 报告/日志路径每轮都不同(测试台在文件名里带上这一轮的时刻)。上一轮这一点是
|
||||
# 靠"启动前先 del 掉"保证的,结果 del 不掉的那一轮读到的是上一轮的报告:
|
||||
# 文件里只剩孤零零一行 MARK_QUIT,看着像"改尺寸根本没发生",其实是另一条
|
||||
# 还卡在 WaitForExit 的僵尸进程在替这一轮说话。名字唯一,事实就不会串台。
|
||||
#
|
||||
# 四条用真机换来的规矩:
|
||||
#
|
||||
# 1) 不能用 mode con。在 Windows OpenSSH 给的 ConPTY 上 `mode con cols=180 lines=50`
|
||||
# 会静默失败(返回 0,什么都没改)。
|
||||
#
|
||||
# 2) [Console]::WindowWidth 也不行 —— 至少在界面起来以后不行。crossterm 进备用屏是
|
||||
# CreateConsoleScreenBuffer + SetConsoleActiveScreenBuffer,换掉的是"当前正在显示
|
||||
# 的那块屏幕缓冲区",而本进程的 stdout 句柄还指着进来时那块。实测:程序起来之后
|
||||
# 对 [Console]::WindowWidth 赋值,读回来纹丝不动 —— 改的是一块没人看的屏幕。
|
||||
# CONOUT$ 这个名字每次打开都解析成"当前活动的那块缓冲区",所以本脚本自己
|
||||
# P/Invoke CreateFileW("CONOUT$"),改的才是界面真正依附的那一块。
|
||||
#
|
||||
# 3) 改窗口之前必须先改缓冲区。conhost 的 dwMaximumWindowSize 就等于缓冲区尺寸,
|
||||
# 窗口永远不能比缓冲区大:直接把窗口设成 180 会被拒绝,于是"改大"根本没发生,
|
||||
# 测的是一条根本没变过的尺寸。所以先把缓冲区撑到两种尺寸里最大的那个,再把窗口
|
||||
# 收到小的,运行一段时间后才放大窗口。
|
||||
#
|
||||
# 4) 界面跑起来以后,一个字都不能往这个控制台里写。测试台自己的输出也是画在界面上的:
|
||||
# 一行 Write-Output 会落在界面正在画的区域里,末尾的换行还会让 conhost 整体上滚
|
||||
# 一行 —— 渲染器的 diff 从此和真实屏幕差一行,屏幕上到处是上一帧的残影。
|
||||
# 所以启动之后的事实全部写进报告文件,由测试台另外一条 ssh 连接读回来。
|
||||
param(
|
||||
[Parameter(Mandatory = $true)][int]$SmallColumns,
|
||||
[Parameter(Mandatory = $true)][int]$SmallRows,
|
||||
[Parameter(Mandatory = $true)][int]$BigColumns,
|
||||
[Parameter(Mandatory = $true)][int]$BigRows,
|
||||
[Parameter(Mandatory = $true)][int]$GrowAfter,
|
||||
[Parameter(Mandatory = $true)][string]$Lang,
|
||||
[Parameter(Mandatory = $true)][string]$Bin,
|
||||
[Parameter(Mandatory = $true)][string]$Report,
|
||||
[string]$Trace = ""
|
||||
)
|
||||
|
||||
Add-Type -TypeDefinition @'
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Squid {
|
||||
public static class ConsoleProbe {
|
||||
[StructLayout(LayoutKind.Sequential)] public struct COORD { public short X; public short Y; }
|
||||
[StructLayout(LayoutKind.Sequential)] public struct SMALL_RECT {
|
||||
public short Left; public short Top; public short Right; public short Bottom;
|
||||
}
|
||||
[StructLayout(LayoutKind.Sequential)] public struct INFO {
|
||||
public COORD dwSize; public COORD dwCursorPosition; public ushort wAttributes;
|
||||
public SMALL_RECT srWindow; public COORD dwMaximumWindowSize;
|
||||
}
|
||||
|
||||
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
|
||||
static extern IntPtr CreateFileW(string name, uint access, uint share, IntPtr security,
|
||||
uint disposition, uint flags, IntPtr template);
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
public static extern bool GetConsoleScreenBufferInfo(IntPtr handle, out INFO info);
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
public static extern bool SetConsoleScreenBufferSize(IntPtr handle, COORD size);
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
public static extern bool SetConsoleWindowInfo(IntPtr handle, bool absolute, ref SMALL_RECT rect);
|
||||
[DllImport("kernel32.dll")] public static extern bool CloseHandle(IntPtr handle);
|
||||
|
||||
// 0 是 NULL:脚本自己不是控制台程序时也拿不到句柄,调用方负责报错。
|
||||
public static IntPtr Open() {
|
||||
IntPtr handle = CreateFileW("CONOUT$", 0xC0000000, 0x00000003, IntPtr.Zero,
|
||||
3, 0, IntPtr.Zero);
|
||||
return handle.ToInt64() == -1 ? IntPtr.Zero : handle;
|
||||
}
|
||||
|
||||
public static uint LastError() { return (uint)Marshal.GetLastWin32Error(); }
|
||||
|
||||
public static string Size(IntPtr handle) {
|
||||
INFO info;
|
||||
if (!GetConsoleScreenBufferInfo(handle, out info)) { return "unknown"; }
|
||||
int windowColumns = info.srWindow.Right - info.srWindow.Left + 1;
|
||||
int windowRows = info.srWindow.Bottom - info.srWindow.Top + 1;
|
||||
return windowColumns + "x" + windowRows + " in " +
|
||||
info.dwSize.X + "x" + info.dwSize.Y;
|
||||
}
|
||||
|
||||
/// 先撑缓冲区,再撑窗口。返回 false 时通过 LastError 说明为什么。
|
||||
public static bool Resize(IntPtr handle, int columns, int rows,
|
||||
out int bufferColumns, out int bufferRows) {
|
||||
INFO info;
|
||||
bufferColumns = 0; bufferRows = 0;
|
||||
if (!GetConsoleScreenBufferInfo(handle, out info)) { return false; }
|
||||
int wide = columns > info.dwSize.X ? columns : info.dwSize.X;
|
||||
int tall = rows > info.dwSize.Y ? rows : info.dwSize.Y;
|
||||
if (wide != info.dwSize.X || tall != info.dwSize.Y) {
|
||||
COORD want = new COORD(); want.X = (short)wide; want.Y = (short)tall;
|
||||
if (!SetConsoleScreenBufferSize(handle, want)) { return false; }
|
||||
if (!GetConsoleScreenBufferInfo(handle, out info)) { return false; }
|
||||
}
|
||||
// 只改大小,不动位置:窗口左上角留在原地,才不会把用户正在看的内容卷走。
|
||||
SMALL_RECT window = info.srWindow;
|
||||
window.Right = (short)(window.Left + columns - 1);
|
||||
window.Bottom = (short)(window.Top + rows - 1);
|
||||
if (window.Right >= info.dwSize.X) { window.Right = (short)(info.dwSize.X - 1); }
|
||||
if (window.Bottom >= info.dwSize.Y) { window.Bottom = (short)(info.dwSize.Y - 1); }
|
||||
if (!SetConsoleWindowInfo(handle, true, ref window)) { return false; }
|
||||
if (!GetConsoleScreenBufferInfo(handle, out info)) { return false; }
|
||||
bufferColumns = info.dwSize.X; bufferRows = info.dwSize.Y;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
'@
|
||||
|
||||
# 报告文件:每次运行从空白开始。用 AppendAllText 而不是 Add-Content,是因为
|
||||
# 后者会走文件系统 provider,路径里一旦有它不认的东西就静默什么都不写。
|
||||
function Tell([string]$line) {
|
||||
[void][System.IO.File]::AppendAllText($Report, "$line`r`n")
|
||||
}
|
||||
|
||||
function Read-console([string]$when) {
|
||||
$handle = [Squid.ConsoleProbe]::Open()
|
||||
if ($handle -eq [IntPtr]::Zero) {
|
||||
Tell "NO_CONOUT $when win32=$([Squid.ConsoleProbe]::LastError())"
|
||||
return "unknown"
|
||||
}
|
||||
try { return [Squid.ConsoleProbe]::Size($handle) } finally {
|
||||
[void][Squid.ConsoleProbe]::CloseHandle($handle)
|
||||
}
|
||||
}
|
||||
|
||||
function Set-console([int]$columns, [int]$rows, [string]$when) {
|
||||
$handle = [Squid.ConsoleProbe]::Open()
|
||||
if ($handle -eq [IntPtr]::Zero) {
|
||||
Tell "SIZE_FAILED $when CONOUT`$ unavailable win32=$([Squid.ConsoleProbe]::LastError())"
|
||||
Tell "$when unknown"
|
||||
return
|
||||
}
|
||||
try {
|
||||
$bufferColumns = 0
|
||||
$bufferRows = 0
|
||||
$worked = [Squid.ConsoleProbe]::Resize($handle, $columns, $rows,
|
||||
[ref]$bufferColumns, [ref]$bufferRows)
|
||||
if (-not $worked) {
|
||||
Tell "SIZE_FAILED $when win32=$([Squid.ConsoleProbe]::LastError())"
|
||||
}
|
||||
# 报告里写的是改完之后控制台自己承认的尺寸,不是我们请求的数字。
|
||||
$seen = [Squid.ConsoleProbe]::Size($handle)
|
||||
Tell "$when $seen"
|
||||
$script:seen = $seen
|
||||
} finally {
|
||||
[void][Squid.ConsoleProbe]::CloseHandle($handle)
|
||||
}
|
||||
}
|
||||
|
||||
Set-Content -LiteralPath $Report -Value "" -Encoding ascii
|
||||
if ($Trace -ne "") {
|
||||
Remove-Item -LiteralPath $Trace -ErrorAction SilentlyContinue
|
||||
$env:SQUID_CONSOLE_TRACE = $Trace
|
||||
}
|
||||
# 第一行先留下"这一轮是谁":两个进程抢同一份报告时,这行就是裁判。
|
||||
Tell "MARK_ENTER pid=$PID bin=$Bin report=$Report"
|
||||
|
||||
Set-console $SmallColumns $SmallRows "MARK_STARTED"
|
||||
# 启动之前打印一次是安全的:那时候界面上还没有任何东西需要保持原样。
|
||||
# 测试台靠这一行对齐时钟 —— 改尺寸就发生在它之后的第 $GrowAfter 秒。
|
||||
"MARK_STARTED $script:seen"
|
||||
|
||||
$arguments = "state"
|
||||
if ($Lang -notin @('en', 'zh')) { $arguments = "state --lang $Lang" }
|
||||
# 用 .NET 的 Process,不用 Start-Process:实测后者 -PassThru 给的对象的 ExitCode
|
||||
# 是空的(报告里那行 "MARK_QUIT " 后面什么都没有),于是"界面到底怎么退的"没答案。
|
||||
# UseShellExecute=false 才会继承本进程的控制台,后面改的才是界面依附的那块缓冲区。
|
||||
$startInfo = New-Object System.Diagnostics.ProcessStartInfo
|
||||
$startInfo.FileName = $Bin
|
||||
$startInfo.Arguments = $arguments
|
||||
$startInfo.UseShellExecute = $false
|
||||
$interface = [System.Diagnostics.Process]::Start($startInfo)
|
||||
Tell "MARK_RUNNING pid=$($interface.Id) arguments=$arguments"
|
||||
|
||||
Start-Sleep -Seconds $GrowAfter
|
||||
# 顺序不能反:Resize() 内部已经是先撑缓冲区再撑窗口,反过来会被 conhost 拒绝。
|
||||
Set-console $BigColumns $BigRows "MARK_RESIZED"
|
||||
# 两秒后再读一次。ConPTY 的尺寸是由 ssh 客户端说了算的:如果它把我们改的又改回去,
|
||||
# 这一行和上一行不一样,而界面看到的就是一场没人认领的抖动。
|
||||
Start-Sleep -Seconds 2
|
||||
Tell "MARK_STILL $(Read-console 'MARK_STILL')"
|
||||
|
||||
$interface.WaitForExit()
|
||||
Tell "MARK_QUIT $($interface.ExitCode)"
|
||||
# 界面已经退出,控制台重新归脚本所有,这一行才允许打印。
|
||||
"MARK_QUIT $($interface.ExitCode)"
|
||||
|
|
@ -0,0 +1,492 @@
|
|||
#!/usr/bin/env python3
|
||||
"""Grow the console underneath a running TUI and check what is left on screen.
|
||||
|
||||
Resizing is the one thing a terminal interface is not allowed to get wrong, and
|
||||
the one thing no unit test can prove. The bug lives between the frame the
|
||||
renderer compared and the cells the console moved: a diff renderer writes the
|
||||
cells that changed, so after a resize it happily leaves behind every cell it
|
||||
still believes in, in coordinates the console no longer agrees to. A legacy
|
||||
Windows console adds its own twist -- it answers a size question with the size of
|
||||
its *window* while the buffer behind it is still the size it had when the program
|
||||
entered the alternate screen. Every column that just came into view is then full
|
||||
of the previous frame, and every cell written past the end of a real row lands at
|
||||
the start of the next one. What the user sees is a screen of diagonals.
|
||||
|
||||
The test: start the interface small, paint a panel, grow the console while it is
|
||||
running, then read the screen back through pyte and ask how many columns the
|
||||
right hand borders sit in. One column means a screen. Two means two frames drawn
|
||||
on top of each other.
|
||||
|
||||
Five facts about Windows OpenSSH were measured on the box, and each one
|
||||
shaped this script:
|
||||
|
||||
* the ConPTY it hands a session ignores the client's window-change requests, so
|
||||
this side cannot resize the console it is watching. The grower runs on the box,
|
||||
in a process attached to the same console;
|
||||
* `mode con cols=X lines=Y` fails silently there -- it reports no error and
|
||||
changes nothing -- so the grower drives `SetConsoleWindowInfo` itself. Any
|
||||
resize test built on `mode con` measured nothing at all;
|
||||
* and it drives that call on a handle to `CONOUT$`, not on its own stdout or on
|
||||
`[Console]::WindowWidth`: entering the alternate screen swapped the *shown*
|
||||
buffer, and the process that asked for the resize is still describing the one
|
||||
nobody is looking at. A resize done that way reads back unchanged, which is
|
||||
the same silent nothing `mode con` produces;
|
||||
* a window can never be wider than the screen buffer, and a ConPTY reports its
|
||||
buffer size as the largest window it will allow. Growing a 120 column console
|
||||
to 180 columns therefore means growing the buffer first, or the resize is
|
||||
refused and the test watches a size that never changed;
|
||||
* nothing may be written to that console while the interface is drawing, not even
|
||||
by the test itself. A line printed by the harness lands inside the picture, and
|
||||
its newline scrolls the console a row -- after which the renderer's diff and
|
||||
the cells on the glass are one row apart everywhere, which looks exactly like
|
||||
the bug being hunted. So the grower reports into a file, and a second ssh
|
||||
connection reads that file once the interface has exited.
|
||||
|
||||
The console is resized by its own box on a timer this script starts alongside the
|
||||
interface, which is also when the emulator is told to grow. Both are derived from
|
||||
the moment the box announced the interface had started, so the two stay within a
|
||||
few milliseconds of each other on a LAN.
|
||||
|
||||
python3 scripts/win-tui-resize.py # 100x30 -> 180x50
|
||||
python3 scripts/win-tui-resize.py --lang zh # CJK is twice as wide
|
||||
python3 scripts/win-tui-resize.py --small 80x24 --big 200x60
|
||||
|
||||
Exit code 0 means the interface survived the resize as one picture.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import codecs
|
||||
import fcntl
|
||||
import os
|
||||
import pty
|
||||
import re
|
||||
import select
|
||||
import signal
|
||||
import struct
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
|
||||
try:
|
||||
import pyte
|
||||
except ImportError:
|
||||
sys.exit("requires pyte: python3 -m pip install --user pyte")
|
||||
|
||||
HOST = os.environ.get("WIN_HOST", "KANGWJ@192.168.2.92")
|
||||
KEY = os.environ.get("WIN_KEY", os.path.expanduser("~/.ssh/id_ed25519_windows"))
|
||||
BIN = os.environ.get("WIN_BIN", r"C:\dev\target\release\squid.exe")
|
||||
HOME = "C:\\Users\\kangwj"
|
||||
REMOTE_GROW = HOME + "\\win-tui-grow.ps1"
|
||||
REMOTE_CLEAN = HOME + "\\win-tui-clean.ps1"
|
||||
|
||||
|
||||
def report_path(run_id):
|
||||
"""Where this run writes the facts it cannot print, named after this run.
|
||||
|
||||
The name carries the run on purpose. Sharing one path and deleting it first
|
||||
looks equivalent and is not: a grower left behind by an interrupted run is
|
||||
still parked in WaitForExit, and when its interface finally dies it appends
|
||||
its own MARK_QUIT to whatever file is there. The next run then reads a
|
||||
report with one line in it and concludes that no resize ever happened,
|
||||
which is a fact about a process from ten minutes ago.
|
||||
"""
|
||||
return f"{HOME}\\win-tui-report-{run_id}.txt"
|
||||
|
||||
|
||||
def trace_path(run_id):
|
||||
return f"{HOME}\\win-tui-trace-{run_id}.log"
|
||||
|
||||
# TIOCSWINSZ, as macOS spells it. `struct winsize` is rows, cols, xpixel, ypixel.
|
||||
TIOCSWINSZ = 0x40087468
|
||||
|
||||
|
||||
def winsize(columns, rows):
|
||||
return struct.pack("HHHH", rows, columns, 0, 0)
|
||||
|
||||
|
||||
def shape(text):
|
||||
columns, rows = text.lower().split("x")
|
||||
return int(columns), int(rows)
|
||||
|
||||
|
||||
def ssh(*arguments):
|
||||
return ["ssh", "-i", KEY, "-o", "BatchMode=yes", HOST] + list(arguments)
|
||||
|
||||
|
||||
def told(text, marker):
|
||||
"""What the box said after `marker`, or None when it has not said yet.
|
||||
|
||||
A cursor sequence can land between the words even in the safe part of the
|
||||
stream, so the pattern tolerates a handful of them. What it will not do is
|
||||
guess: nothing here trusts a requested size.
|
||||
"""
|
||||
gap = r"(?:\x1b\[[0-9;?]*[a-zA-Z]|\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)|\s){0,8}"
|
||||
match = re.search(marker + gap + r"(\d{1,4})" + gap + r"x" + gap + r"(\d{1,4})", text)
|
||||
return None if not match else (int(match.group(1)), int(match.group(2)))
|
||||
|
||||
|
||||
def read_back(remote_path):
|
||||
"""A file the box wrote while the picture owned the console, read out of band.
|
||||
|
||||
Nothing can be printed to that console while the interface is drawing, so
|
||||
every fact from inside it travels through a file and a second connection.
|
||||
"""
|
||||
try:
|
||||
done = subprocess.run(ssh("type", remote_path), capture_output=True, timeout=30)
|
||||
except subprocess.TimeoutExpired:
|
||||
return ""
|
||||
return done.stdout.decode("utf-8", "replace")
|
||||
|
||||
|
||||
def report(path):
|
||||
"""The grower's own account of the sizes it ended up with."""
|
||||
return read_back(path)
|
||||
|
||||
|
||||
def script(name):
|
||||
return os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
|
||||
"scripts", name)
|
||||
|
||||
|
||||
def upload(local, remote):
|
||||
subprocess.run(["scp", "-i", KEY, "-o", "BatchMode=yes", "-q", local,
|
||||
f"{HOST}:{remote}"], check=True)
|
||||
|
||||
|
||||
def clean(reason):
|
||||
"""End the runs that are still alive on the box, this one included.
|
||||
|
||||
Killing the local ssh does not end the remote PowerShell, and a grower left
|
||||
parked in WaitForExit holds the console, the release binary that the next
|
||||
build cannot overwrite, and the habit of writing into a report file that no
|
||||
longer belongs to it.
|
||||
"""
|
||||
upload(script("win-tui-clean.ps1"), REMOTE_CLEAN)
|
||||
done = subprocess.run(
|
||||
["ssh", "-i", KEY, "-o", "BatchMode=yes", HOST,
|
||||
"powershell -NoProfile -ExecutionPolicy Bypass -File " + REMOTE_CLEAN],
|
||||
capture_output=True, timeout=60)
|
||||
said = done.stdout.decode("utf-8", "replace")
|
||||
for line in said.splitlines():
|
||||
if line.strip():
|
||||
print(f"[clean ] {line.strip()} ({reason})")
|
||||
return said
|
||||
|
||||
|
||||
def grow_script(args):
|
||||
"""Upload the grower and return the command that runs it on the box."""
|
||||
upload(script("win-tui-grow.ps1"), REMOTE_GROW)
|
||||
return [
|
||||
"powershell", "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", REMOTE_GROW,
|
||||
str(args.small[0]), str(args.small[1]), str(args.big[0]), str(args.big[1]),
|
||||
str(int(args.resize_at)), args.lang or "en", BIN, args.report,
|
||||
args.trace_file if args.trace else "",
|
||||
]
|
||||
|
||||
|
||||
def run(args):
|
||||
clean("before")
|
||||
try:
|
||||
return watch(args)
|
||||
finally:
|
||||
clean("after")
|
||||
|
||||
|
||||
def watch(args):
|
||||
master, slave = pty.openpty()
|
||||
fcntl.ioctl(slave, TIOCSWINSZ, winsize(*args.small))
|
||||
child = subprocess.Popen(
|
||||
ssh("-tt", "-o", "ServerAliveInterval=30") + grow_script(args),
|
||||
stdin=slave, stdout=slave, stderr=slave, close_fds=True,
|
||||
)
|
||||
os.close(slave)
|
||||
|
||||
screen = pyte.Screen(*args.small)
|
||||
stream = pyte.Stream(screen)
|
||||
# One decoder for the whole stream: a UTF-8 sequence split across two reads
|
||||
# would otherwise arrive as a replacement character the console never sent.
|
||||
decoder = codecs.getincrementaldecoder("utf-8")("replace")
|
||||
raw, text = [], []
|
||||
|
||||
def feed(chunk):
|
||||
raw.append(chunk)
|
||||
decoded = decoder.decode(chunk)
|
||||
text.append(decoded)
|
||||
stream.feed(decoded)
|
||||
|
||||
def said():
|
||||
return "".join(text)
|
||||
|
||||
def adopt(size, note):
|
||||
"""Follow the console rather than lead it, in the emulator as in the pty."""
|
||||
fcntl.ioctl(master, TIOCSWINSZ, winsize(*size))
|
||||
screen.resize(lines=size[1], columns=size[0])
|
||||
print(f"[shape ] {note}: now {size[0]}x{size[1]}")
|
||||
|
||||
began = time.time()
|
||||
started, announced = None, None
|
||||
grown = None
|
||||
problems = []
|
||||
# Switch to the Power panel before the resize: the frame that has to survive
|
||||
# a resize must not be the frame that was already on screen. A digit opens
|
||||
# the sidebar menu, so `enter` closes it again and hands the keys back.
|
||||
keys = iter([(args.settle, "5"), (args.settle + 2, "\r")])
|
||||
next_key = next(keys, None)
|
||||
deadline = began + args.hold
|
||||
|
||||
while time.time() < deadline:
|
||||
ready, _, _ = select.select([master], [], [], 0.2)
|
||||
if ready:
|
||||
try:
|
||||
chunk = os.read(master, 65536)
|
||||
except OSError:
|
||||
break
|
||||
if not chunk:
|
||||
break
|
||||
feed(chunk)
|
||||
if "MARK_QUIT" in text[-1]:
|
||||
# The interface exited on its own: whatever it printed on the way
|
||||
# out is the finding, so stop rather than press keys into a corpse.
|
||||
problems.append("the interface exited by itself before the test asked it to")
|
||||
break
|
||||
now = time.time() - began
|
||||
if next_key and now >= next_key[0]:
|
||||
os.write(master, next_key[1].encode())
|
||||
next_key = next(keys, None)
|
||||
if started is None:
|
||||
started = told(said(), "MARK_STARTED")
|
||||
if started:
|
||||
announced = time.time()
|
||||
if started != tuple(args.small):
|
||||
print(f"[shape ] requested {args.small[0]}x{args.small[1]}, "
|
||||
f"the console is {started[0]}x{started[1]}")
|
||||
adopt(started, "announced")
|
||||
if grown is None and announced is not None:
|
||||
# The grower sleeps this long between announcing and resizing, so the
|
||||
# two clocks agree to a few milliseconds on a LAN. Growing the
|
||||
# emulator a moment early is the safe direction: a screen wider than
|
||||
# the console never wraps a row by mistake, and the interface wipes
|
||||
# and redraws the whole area the moment the real resize arrives.
|
||||
if time.time() >= announced + max(args.resize_at - args.margin, 0.0):
|
||||
grown = args.big
|
||||
adopt(grown, "grown")
|
||||
|
||||
painted = [line.rstrip() for line in screen.display]
|
||||
|
||||
# Ask to quit. The alternate screen is invisible through this ConPTY (it does
|
||||
# not forward ?1049h/?1049l), so a clean exit is proved by the grower's last
|
||||
# line, which it may print only once the console belongs to it again.
|
||||
quit_cleanly = False
|
||||
try:
|
||||
os.write(master, b"q")
|
||||
except OSError:
|
||||
ending = time.time()
|
||||
else:
|
||||
ending = time.time() + 6
|
||||
while time.time() < ending:
|
||||
ready, _, _ = select.select([master], [], [], 0.3)
|
||||
if not ready:
|
||||
continue
|
||||
try:
|
||||
chunk = os.read(master, 65536)
|
||||
except OSError:
|
||||
break
|
||||
if not chunk:
|
||||
break
|
||||
feed(chunk)
|
||||
if "MARK_QUIT" in said()[-200:]:
|
||||
quit_cleanly = True
|
||||
break
|
||||
|
||||
child.send_signal(signal.SIGTERM)
|
||||
try:
|
||||
child.wait(5)
|
||||
except subprocess.TimeoutExpired:
|
||||
child.kill()
|
||||
os.close(master)
|
||||
|
||||
raw_bytes = b"".join(raw)
|
||||
if args.out:
|
||||
with open(args.out, "wb") as handle:
|
||||
handle.write(raw_bytes)
|
||||
print(f"[raw ] {len(raw_bytes)} bytes kept in {args.out}")
|
||||
return dict(painted=painted, started=started, grown=grown,
|
||||
report_text=report(args.report), trace=trace(args),
|
||||
quit_cleanly=quit_cleanly, problems=problems, characters=len(said()))
|
||||
|
||||
|
||||
def trace(args):
|
||||
"""What the interface itself measured, when it was asked to keep a log."""
|
||||
return read_back(args.trace_file) if args.trace else ""
|
||||
|
||||
|
||||
def told_offband(text, marker):
|
||||
for line in text.splitlines():
|
||||
if line.startswith(marker):
|
||||
return line
|
||||
return None
|
||||
|
||||
|
||||
def window_of(line):
|
||||
"""The window half of `MARK_RESIZED 180x50 in 180x9001`."""
|
||||
match = re.search(r"(\d{1,4})x(\d{1,4})", line or "")
|
||||
return None if not match else (int(match.group(1)), int(match.group(2)))
|
||||
|
||||
|
||||
def buffer_of(line):
|
||||
"""The buffer half of the same line, after the word `in`.
|
||||
|
||||
Worth reading separately because the two halves are the whole story of a
|
||||
legacy console: a window bigger than its buffer is a window with columns
|
||||
that have no cells, and a buffer bigger than its window is a window that
|
||||
can slide away from the picture being painted into it.
|
||||
"""
|
||||
match = re.search(r"\bin\s*(\d{1,4})x(\d{1,4})", line or "")
|
||||
return None if not match else (int(match.group(1)), int(match.group(2)))
|
||||
|
||||
|
||||
def check(found, args):
|
||||
problems = list(found["problems"])
|
||||
painted = found["painted"]
|
||||
|
||||
# What the console says it did, from the grower's file rather than from the
|
||||
# picture: an interface can only draw in the space it was given, so a resize
|
||||
# that the console refused has to be told apart from one it survived.
|
||||
account = found["report_text"]
|
||||
facts = [line for line in account.splitlines() if line.strip()]
|
||||
for line in facts:
|
||||
print(f"[report] {line}")
|
||||
if not facts:
|
||||
problems.append("the grower left no report — nothing on the box was measured")
|
||||
# The file names itself, so a report from a run that never ended cannot be
|
||||
# mistaken for this one's. Everything below depends on that line being here.
|
||||
if told_offband(account, "MARK_ENTER") is None:
|
||||
problems.append("the report does not name this run — it is not this run's report")
|
||||
if told_offband(account, "MARK_RUNNING") is None:
|
||||
problems.append("the interface was never started on the box")
|
||||
# Two seconds after the resize, the console's own final word is the only
|
||||
# size worth judging the picture against. Measured on the 18363 box: a
|
||||
# ConPTY created by sshd keeps the window it was created with — asking for
|
||||
# 180x50 grows the buffer and leaves the window where it was, so a test that
|
||||
# demanded the requested shape would be failing the console for an
|
||||
# ownership fact instead of the drawing fact. What has to hold either way is
|
||||
# that the buffer ends up the size of the window, because that is the one
|
||||
# shape in which the cells being painted are the cells being shown.
|
||||
still = told_offband(account, "MARK_STILL")
|
||||
if still is None:
|
||||
problems.append("the grower never looked at the console a second time")
|
||||
final = window_of(still) or args.big
|
||||
if window_of(still) and window_of(still) != args.big:
|
||||
print(f"[note ] the console kept its window at {final[0]}x{final[1]} "
|
||||
f"instead of the requested {args.big[0]}x{args.big[1]} — this console is "
|
||||
"owned by something that pins its window. Judging the picture "
|
||||
"against what the console actually says.")
|
||||
held = buffer_of(still)
|
||||
if held and final and held != final:
|
||||
problems.append(
|
||||
f"the console still holds a {held[0]}x{held[1]} buffer behind a "
|
||||
f"{final[0]}x{final[1]} window — the window can slide away from that, "
|
||||
"which is a frame painted where nobody is looking"
|
||||
)
|
||||
said = told_offband(account, "MARK_STARTED")
|
||||
if said is None:
|
||||
problems.append("the console never reported started — the grower did not run")
|
||||
elif window_of(said) != args.small:
|
||||
problems.append(
|
||||
f"the console is {said} before the interface ran, not "
|
||||
f"{args.small[0]}x{args.small[1]} — the picture proves nothing about that shape"
|
||||
)
|
||||
if told_offband(account, "MARK_RESIZED") is None:
|
||||
problems.append("the console never looked at itself after the resize")
|
||||
refused = told_offband(account, "SIZE_FAILED")
|
||||
if refused:
|
||||
problems.append(f"the console refused a size: {refused}")
|
||||
|
||||
# The interface's own numbers. It is the only witness that knows which buffer
|
||||
# it was drawing on, so when the two sides disagree this is the line that
|
||||
# says who is wrong.
|
||||
for line in found["trace"].splitlines():
|
||||
if line.strip():
|
||||
print(f"[trace ] {line}")
|
||||
|
||||
edges = {}
|
||||
for line in painted:
|
||||
if line.strip() and "\u2502" in line:
|
||||
edges.setdefault(line.rindex("\u2502"), []).append(line)
|
||||
histogram = {column: len(rows) for column, rows in edges.items()}
|
||||
drawn = sum(1 for line in painted if line.strip())
|
||||
print(f"[screen] {drawn} rows drawn - right borders: "
|
||||
+ (", ".join(f"col {c} x{n}" for c, n in sorted(histogram.items())) or "none"))
|
||||
if not histogram:
|
||||
problems.append("the screen holds no boxes at all")
|
||||
elif len(histogram) > 1:
|
||||
best = max(histogram.values())
|
||||
stray = sum(histogram.values()) - best
|
||||
problems.append(
|
||||
f"{stray} of {sum(histogram.values())} bordered rows end somewhere other than the "
|
||||
f"column the other {best} do - two screens drawn on top of each other"
|
||||
)
|
||||
for column, rows in sorted(edges.items()):
|
||||
print(f"[stray ] col {column}: " + " | ".join(row[:70] for row in rows[:3]))
|
||||
else:
|
||||
column, rows = next(iter(edges.items()))
|
||||
if column + 1 != final[0]:
|
||||
problems.append(
|
||||
f"the picture is {column + 1} columns wide in a {final[0]} column window - "
|
||||
"the resize was never drawn into"
|
||||
)
|
||||
if len(rows) < final[1] * 3 // 5:
|
||||
problems.append(
|
||||
f"only {len(rows)} rows reach the right border in a {final[1]} row window - "
|
||||
"the redraw covered part of the screen"
|
||||
)
|
||||
|
||||
if not found["quit_cleanly"]:
|
||||
problems.append("the interface did not answer `q` - the shell prompt never came back")
|
||||
else:
|
||||
quit_line = (told_offband(account, "MARK_QUIT") or "").split()
|
||||
if quit_line and quit_line[-1] != "0":
|
||||
problems.append("the interface left the console with " + " ".join(quit_line))
|
||||
rows = [n for n, line in enumerate(painted, 1) if "\ufffd" in line]
|
||||
if rows:
|
||||
problems.append(f"screen rows {rows} carry glyphs the console could not draw")
|
||||
|
||||
print()
|
||||
print("\n[screen] ".join(["[screen]"] + painted))
|
||||
print()
|
||||
for problem in problems:
|
||||
print(f"[FAIL] {problem}")
|
||||
print(f"RESULT: {'FAIL' if problems else 'OK'}")
|
||||
return 1 if problems else 0
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__,
|
||||
formatter_class=argparse.RawDescriptionHelpFormatter)
|
||||
parser.add_argument("--small", type=shape, default=(100, 30), metavar="COLSxROWS")
|
||||
parser.add_argument("--big", type=shape, default=(180, 50), metavar="COLSxROWS")
|
||||
parser.add_argument("--lang", choices=["en", "zh"])
|
||||
parser.add_argument("--resize-at", type=float, default=10.0,
|
||||
help="seconds of running before the box grows its own console")
|
||||
parser.add_argument("--margin", type=float, default=1.0,
|
||||
help="seconds before that moment to grow the emulator")
|
||||
parser.add_argument("--hold", type=float, default=25.0,
|
||||
help="seconds to watch the interface before pressing q")
|
||||
parser.add_argument("--settle", type=float, default=6.0,
|
||||
help="seconds before the panel is switched")
|
||||
parser.add_argument("--out", default="/tmp/squid-win-resize.bin")
|
||||
parser.add_argument("--no-trace", dest="trace", action="store_false",
|
||||
help="do not ask the interface to log what its console claimed")
|
||||
parser.set_defaults(trace=True)
|
||||
args = parser.parse_args()
|
||||
# One name per run: see report_path(). A minute of history stays readable and
|
||||
# a leftover from yesterday cannot answer for today.
|
||||
args.run_id = time.strftime("%Y%m%d-%H%M%S")
|
||||
args.report = report_path(args.run_id)
|
||||
args.trace_file = trace_path(args.run_id)
|
||||
print(f"[run ] {args.run_id} - report {args.report}")
|
||||
return check(run(args), args)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
|
|
@ -0,0 +1,229 @@
|
|||
# 量一量"这个控制台认为每个字形占几格",把结果写进文件,不需要任何人盯着屏幕看。
|
||||
#
|
||||
# powershell -NoProfile -ExecutionPolicy Bypass -File win-tui-widths.ps1
|
||||
# powershell -NoProfile -ExecutionPolicy Bypass -File win-tui-widths.ps1 C:\Users\kangwj\widths.txt
|
||||
#
|
||||
# 为什么需要它:渲染器按 Unicode 的 East Asian Width 决定"这一行字占几列",
|
||||
# conhost 按它自己的规矩决定"这个字形占几格"。两边一旦不一致,本该正好顶到右边框的
|
||||
# 行就多出一格,右边框被挤到下一行开头,下面每一行跟着错位 —— 用户看到的就是花屏、
|
||||
# 右边被切掉、页脚重复出现。中文控制台最容易撞这件事:·…—×↑↓→←“”●○≥
|
||||
# 在 GBK 里都是双字节字符,而 Unicode 说它们是"窄"的;─│ 这类制表符同理。
|
||||
#
|
||||
# 判据不靠肉眼:把光标放到已知列,写一个字符,再读回光标停在第几列 ——
|
||||
# 光标走过的格数就是这个字符在这个控制台里的真实宽度。同时把那一格里真正存下来的
|
||||
# 字符读回来,于是"宽度不符"和"字符被码页换掉了"这两件事能一次分开。
|
||||
#
|
||||
# 两条写入路径都量一遍,因为它们真的不一样:
|
||||
# W = WriteConsoleW(UTF-16 直接进缓冲区)。界面在真控制台里走的就是这一条。
|
||||
# A = 程序往 stdout 写编码后的字节,由控制台自己解码。ConPTY/管道走这一条。
|
||||
# 输出被重定向时 A 这条路根本到不了屏幕,那种轮次直接跳过,不量出一条假数据。
|
||||
#
|
||||
# 副作用:探测要在窗口倒数第二、第三行上写字,那两行原来的内容会被盖掉。
|
||||
# 脚本先把那两行读下来,量完原样写回,再把光标放回去 —— 但宽字符是"一个字符占两格",
|
||||
# 还原时第二格只能填回空格,所以还原完那两行可能有点毛刺。按回车或直接 clear 就行。
|
||||
param(
|
||||
[string]$Out = "$env:USERPROFILE\win-tui-widths.txt"
|
||||
)
|
||||
|
||||
# 规矩:Add-Type 用 -TypeDefinition(整份编译单元),不要用 -MemberDefinition。
|
||||
# -MemberDefinition 会把成员塞进一个生成的类体里,放在最前面的 using 就变成
|
||||
# "类成员位置上的 using",PS 5.1 直接编译失败(CS1529),而它报的是 Add-Type
|
||||
# 那一行,很容易误判成"这台机器不让 P/Invoke"。PS 5.1 也没有 -Usings 参数。
|
||||
Add-Type -TypeDefinition @'
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
namespace Squid {
|
||||
public static class CellProbe {
|
||||
[StructLayout(LayoutKind.Sequential)] public struct COORD { public short X; public short Y; }
|
||||
[StructLayout(LayoutKind.Sequential)] public struct SMALL_RECT {
|
||||
public short Left; public short Top; public short Right; public short Bottom;
|
||||
}
|
||||
[StructLayout(LayoutKind.Sequential)] public struct INFO {
|
||||
public COORD dwSize; public COORD dwCursorPosition; public ushort wAttributes;
|
||||
public SMALL_RECT srWindow; public COORD dwMaximumWindowSize;
|
||||
}
|
||||
|
||||
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
|
||||
static extern IntPtr CreateFileW(string name, uint access, uint share, IntPtr security,
|
||||
uint disposition, uint flags, IntPtr templateFile);
|
||||
[DllImport("kernel32.dll")] public static extern bool CloseHandle(IntPtr handle);
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
public static extern bool GetConsoleScreenBufferInfo(IntPtr handle, out INFO info);
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
public static extern bool SetConsoleCursorPosition(IntPtr handle, COORD coordinate);
|
||||
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
|
||||
public static extern bool WriteConsoleW(IntPtr handle, string buffer, uint count,
|
||||
out uint written, IntPtr reserved);
|
||||
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
|
||||
public static extern bool ReadConsoleOutputCharacterW(IntPtr handle, StringBuilder text,
|
||||
uint count, COORD from, out uint read);
|
||||
[DllImport("kernel32.dll")] public static extern uint GetConsoleOutputCP();
|
||||
[DllImport("kernel32.dll")] public static extern uint GetConsoleCP();
|
||||
[DllImport("kernel32.dll")] public static extern bool GetConsoleMode(IntPtr handle, out uint mode);
|
||||
[DllImport("kernel32.dll")] public static extern IntPtr GetStdHandle(int index);
|
||||
[DllImport("kernel32.dll")] public static extern uint GetFileType(IntPtr handle);
|
||||
|
||||
public static IntPtr Open() {
|
||||
IntPtr handle = CreateFileW("CONOUT$", 0xC0000000, 0x00000003, IntPtr.Zero, 3, 0, IntPtr.Zero);
|
||||
return handle.ToInt64() == -1 ? IntPtr.Zero : handle;
|
||||
}
|
||||
|
||||
public static INFO Read(IntPtr handle) {
|
||||
INFO info;
|
||||
GetConsoleScreenBufferInfo(handle, out info);
|
||||
return info;
|
||||
}
|
||||
|
||||
// 别在成员里写 uint:short 隐式转不成 uint(CS0266)。量光标列用 int 就够。
|
||||
public static int CursorColumn(IntPtr handle) { return (int)Read(handle).dwCursorPosition.X; }
|
||||
|
||||
public static bool Place(IntPtr handle, int column, int row) {
|
||||
COORD where = new COORD();
|
||||
where.X = (short)column;
|
||||
where.Y = (short)row;
|
||||
return SetConsoleCursorPosition(handle, where);
|
||||
}
|
||||
|
||||
public static bool PutWide(IntPtr handle, string text) {
|
||||
uint written;
|
||||
return WriteConsoleW(handle, text, (uint)text.Length, out written, IntPtr.Zero);
|
||||
}
|
||||
|
||||
/// 那一格里真正存下来的第一个 UTF-16 单元。<0 表示什么都没读到。
|
||||
public static int CellAt(IntPtr handle, int column, int row) {
|
||||
StringBuilder text = new StringBuilder(2);
|
||||
COORD where = new COORD();
|
||||
where.X = (short)column;
|
||||
where.Y = (short)row;
|
||||
uint read = 0;
|
||||
if (!ReadConsoleOutputCharacterW(handle, text, 1, where, out read) || read == 0) { return -1; }
|
||||
return (int)text[0];
|
||||
}
|
||||
|
||||
public static string RowAt(IntPtr handle, int column, int row, int count) {
|
||||
if (count <= 0) { return ""; }
|
||||
StringBuilder text = new StringBuilder(count + 1);
|
||||
COORD where = new COORD();
|
||||
where.X = (short)column;
|
||||
where.Y = (short)row;
|
||||
uint read = 0;
|
||||
if (!ReadConsoleOutputCharacterW(handle, text, (uint)count, where, out read)) { return ""; }
|
||||
return text.ToString(0, (int)read);
|
||||
}
|
||||
|
||||
public static uint OutputCP() { return GetConsoleOutputCP(); }
|
||||
public static uint InputCP() { return GetConsoleCP(); }
|
||||
public static uint Mode(IntPtr handle) {
|
||||
uint mode = 0;
|
||||
GetConsoleMode(handle, out mode);
|
||||
return mode;
|
||||
}
|
||||
/// stdout 这个句柄是什么:0 未知 / 1 磁盘 / 2 字符设备(真控制台或 ConPTY) / 3 管道。
|
||||
public static uint StdoutKind() { return GetFileType(GetStdHandle(-11)); }
|
||||
/// stdout 自己是不是控制台(能不能 GetConsoleMode)。crossterm 就是靠这个决定
|
||||
/// 走 Windows 控制台 API 还是走 ANSI 转义序列,两条路的"宽度由谁说了算"不一样。
|
||||
public static bool StdoutIsConsole() {
|
||||
uint mode = 0;
|
||||
return GetConsoleMode(GetStdHandle(-11), out mode);
|
||||
}
|
||||
}
|
||||
}
|
||||
'@
|
||||
|
||||
# 这些码位不是随便挑的:每一个都是界面里真的会画到屏幕上的字符,而且都是
|
||||
# "GBK 认为宽、Unicode East Asian Width 认为窄"这一类。A 和 4E2D 是对照组:
|
||||
# 一个必窄,一个必宽,量出来的数不对就说明脚本自己错了。
|
||||
$codePoints = @(
|
||||
0x0041, 0x4E2D, 0x0020,
|
||||
0x00B7, 0x2026, 0x2014, 0x00D7, 0x201C, 0x201D, 0x2018, 0x2019,
|
||||
0x2190, 0x2191, 0x2192, 0x2193, 0x21D2, 0x21B3, 0x2265, 0x2713,
|
||||
0x25CF, 0x25CB, 0x25BE, 0x25A0, 0x2500, 0x2502, 0x250C, 0x2510,
|
||||
0x2514, 0x2518, 0x2550, 0x2503, 0x2460, 0x2461, 0x2462
|
||||
)
|
||||
|
||||
function Hex([int]$value) { return ('{0:X4}' -f $value) }
|
||||
|
||||
$lines = New-Object 'System.Collections.Generic.List[string]'
|
||||
$handle = [Squid.CellProbe]::Open()
|
||||
if ($handle -eq [IntPtr]::Zero) {
|
||||
$lines.Add('ERROR CONOUT$ could not be opened - run this in the console window itself')
|
||||
[void][System.IO.File]::WriteAllLines($Out, $lines)
|
||||
Write-Output "WROTE $Out"
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
$before = [Squid.CellProbe]::Read($handle)
|
||||
$window = $before.srWindow
|
||||
$columns = [int]$window.Right - [int]$window.Left + 1
|
||||
$rows = [int]$window.Bottom - [int]$window.Top + 1
|
||||
$mode = [int][Squid.CellProbe]::Mode($handle)
|
||||
$outCp = [int][Squid.CellProbe]::OutputCP()
|
||||
$inCp = [int][Squid.CellProbe]::InputCP()
|
||||
$stdoutKind = [int][Squid.CellProbe]::StdoutKind()
|
||||
$stdoutConsole = [Squid.CellProbe]::StdoutIsConsole()
|
||||
$dotnet = [Console]::OutputEncoding.WebName
|
||||
$vt = ($mode -band 0x0004) -ne 0
|
||||
$redirected = [Console]::IsOutputRedirected
|
||||
|
||||
# 一行一个事实,每件事都先落成变量再拼:多值 -f 一旦少个参数就整行报
|
||||
# FormatException,诊断脚本最怕的就是这种"整行没了"。
|
||||
$lines += "ENV outcp=$outCp incap=$inCp mode=0x$(Hex $mode) vtoutput=$vt"
|
||||
$lines += "ENV stdout_kind=$stdoutKind stdout_is_console=$stdoutConsole redirected=$redirected dotnet=$dotnet"
|
||||
$lines += "ENV window=${columns}x${rows} at=$($window.Left),$($window.Top) buffer=$($before.dwSize.X)x$($before.dwSize.Y)"
|
||||
$lines += "ENV cursor=$($before.dwCursorPosition.X),$($before.dwCursorPosition.Y) ps=$($PSVersionTable.PSVersion)"
|
||||
|
||||
if ($rows -lt 5 -or $columns -lt 60) {
|
||||
$lines.Add("ERROR window is ${columns}x${rows} - need at least 60x6, resize the window and run again")
|
||||
return
|
||||
}
|
||||
|
||||
# 探测用窗口里倒数第二、第三行:最后一行写字会触发滚动,谁都不想要这种副作用。
|
||||
$probeRows = @{ W = [int]$window.Bottom - 1; A = [int]$window.Bottom - 2 }
|
||||
foreach ($path in @('W', 'A')) {
|
||||
$row = $probeRows[$path]
|
||||
$saved = [Squid.CellProbe]::RowAt($handle, $window.Left, $row, $columns)
|
||||
if ($path -eq 'A' -and ($redirected -or -not $stdoutConsole)) {
|
||||
$lines += "SKIP path=A stdout is not a console screen - the byte path would not reach the screen"
|
||||
continue
|
||||
}
|
||||
$column = [int]$window.Left
|
||||
$stop = [int]$window.Right - 2
|
||||
foreach ($point in $codePoints) {
|
||||
if ($column -ge $stop) {
|
||||
$lines += "TRUNCATED path=$A column=$column - widen the window and run again"
|
||||
break
|
||||
}
|
||||
if (-not [Squid.CellProbe]::Place($handle, $column, $row)) {
|
||||
$lines += "PLACE_FAILED path=$path column=$column row=$row"
|
||||
break
|
||||
}
|
||||
$glyph = [string][char]$point
|
||||
$from = [Squid.CellProbe]::CursorColumn($handle)
|
||||
if ($path -eq 'W') {
|
||||
[void][Squid.CellProbe]::PutWide($handle, $glyph)
|
||||
} else {
|
||||
# 走 .NET 的编码链路:编码后的字节进 stdout 句柄,由控制台自己解码。
|
||||
[Console]::Write($glyph)
|
||||
}
|
||||
$to = [Squid.CellProbe]::CursorColumn($handle)
|
||||
$cell = [Squid.CellProbe]::CellAt($handle, $column, $row)
|
||||
$lines += "MEASURE path=$path cp=U+$(Hex $point) column=$column width=$($to - $from) cell=U+$(Hex ([Math]::Max($cell, 0)))"
|
||||
$column = $to
|
||||
}
|
||||
# 把这一行原本的内容写回去。宽字符还原不完美(第二格只能回空格),报告里说清楚了。
|
||||
[void][Squid.CellProbe]::Place($handle, $window.Left, $row)
|
||||
[void][Squid.CellProbe]::PutWide($handle, ($saved -replace '[^\x20-\x7E]', ' '))
|
||||
}
|
||||
|
||||
# 光标回到脚本进来时的位置:探测不该留下痕迹。
|
||||
[void][Squid.CellProbe]::Place($handle, $before.dwCursorPosition.X, $before.dwCursorPosition.Y)
|
||||
} catch {
|
||||
$lines += "FAILED $($_.Exception.GetType().Name) $($_.Exception.Message)"
|
||||
} finally {
|
||||
[void][Squid.CellProbe]::CloseHandle($handle)
|
||||
[void][System.IO.File]::WriteAllLines($Out, $lines, [System.Text.Encoding]::UTF8)
|
||||
Write-Output "WROTE $Out lines=$($lines.Count)"
|
||||
}
|
||||
Loading…
Reference in New Issue