diff --git a/docs/subsystems/claude-subscription-bridge.md b/docs/subsystems/claude-subscription-bridge.md index 280d498d9..5ec04c083 100644 --- a/docs/subsystems/claude-subscription-bridge.md +++ b/docs/subsystems/claude-subscription-bridge.md @@ -374,8 +374,10 @@ tested on the bytes Claude Code 2.1.316 drew, `testdata/usage-2.1.316.bin`), for `provider.parseClaudeUsage`. Claude Code 2.1.316's `-p /usage` printed only the session's cost there, no window, in a real run. The helper reads the screen as a terminal shows it (`grid`, -[`screen.go`](../../internal/claudetui/screen.go), 200 by 50 on Windows -as its pseudo console is made, 80 by 24 elsewhere), beside the stream: +[`screen.go`](../../internal/claudetui/screen.go), 200 by 50, the size +the pseudo-terminal and Windows' pseudo console are made, from one +`screenCols`/`screenRows`; an 80 by 24 grid of the 200 by 50 Unix +terminal drew the panel's rows over one another, #4988), beside the stream: Windows' pseudo console sends only the cells that changed, skipping one that holds the same letter ("Settin\x1b[1Cs"), and a window drawn only for a moment is kept as it looked (`panel`). The panel is drawn before diff --git a/internal/claudetui/child_unix.go b/internal/claudetui/child_unix.go index fd6340c1e..15610912c 100644 --- a/internal/claudetui/child_unix.go +++ b/internal/claudetui/child_unix.go @@ -29,7 +29,7 @@ func (s *shim) start(path string, args []string) error { return err } // wide, so a line of Claude Code's screen is a line - _ = unix.IoctlSetWinsize(int(tty.Fd()), unix.TIOCSWINSZ, &unix.Winsize{Row: 50, Col: 200}) + _ = unix.IoctlSetWinsize(int(tty.Fd()), unix.TIOCSWINSZ, &unix.Winsize{Row: screenRows, Col: screenCols}) cmd := proc.Command(path, args...) cmd.Env = childEnv(s.env...) cmd.Stdin, cmd.Stdout, cmd.Stderr = tty, tty, tty diff --git a/internal/claudetui/child_windows.go b/internal/claudetui/child_windows.go index 9347d6424..f89e24e1e 100644 --- a/internal/claudetui/child_windows.go +++ b/internal/claudetui/child_windows.go @@ -55,7 +55,7 @@ func (s *shim) start(path string, args []string) error { } var console windows.Handle // wide, so a line of Claude Code's screen is a line - err := windows.CreatePseudoConsole(windows.Coord{X: 200, Y: 50}, inR, outW, 0, &console) + err := windows.CreatePseudoConsole(windows.Coord{X: screenCols, Y: screenRows}, inR, outW, 0, &console) // the console has its own of these windows.CloseHandle(inR) windows.CloseHandle(outW) diff --git a/internal/claudetui/tui.go b/internal/claudetui/tui.go index b1cdea92c..d96403437 100644 --- a/internal/claudetui/tui.go +++ b/internal/claudetui/tui.go @@ -30,7 +30,6 @@ import ( "os/signal" "path/filepath" "regexp" - "runtime" "slices" "strconv" "strings" @@ -336,15 +335,20 @@ func (s *shim) clearScreen() { s.screenMu.Unlock() } -// screenSize is the size of Claude Code's terminal: Windows' pseudo -// console is made 200 by 50 (child_windows.go); a pty here is given no -// size, and Claude Code draws in 80 by 24. -func screenSize() (cols, rows int) { - if runtime.GOOS == "windows" { - return 200, 50 - } - return 80, 24 -} +// screenCols and screenRows are the size of Claude Code's terminal, the +// pseudo-terminal (child_unix.go) and Windows' pseudo console +// (child_windows.go) made alike, and the grid the helper reads it on. A +// grid of another size misreads the screen: an 80 by 24 one, on macOS and +// Linux while their pseudo-terminal was 200 by 50, drew every row under +// the 24th over the 24th, and /usage's panel, at the foot of the screen, +// was told as one window or as a figure since drawn again (#4988, shinnqy +// on X). +const ( + screenCols = 200 + screenRows = 50 +) + +func screenSize() (cols, rows int) { return screenCols, screenRows } // looks is the screen as it is now, and the last look of it with a // usage window since it was cleared, while watched. diff --git a/internal/claudetui/tui_test.go b/internal/claudetui/tui_test.go index a83327f4e..80395cdfb 100644 --- a/internal/claudetui/tui_test.go +++ b/internal/claudetui/tui_test.go @@ -339,20 +339,26 @@ func fakeClaude(args []string) { } // fakeFrames draws b a frame at a time, as Claude Code does: each frame -// ends parking the cursor at the screen's foot (ESC[24;1H), and the next -// comes a moment later. A terminal that renders what it is given (Windows' -// pseudo console) passes on each frame, not only how the last one left -// the screen. +// ends parking the cursor at the screen's foot (ESC[24;1H in the 80 by 24 +// the captures were made in, ESC[50;1H in the helper's 200 by 50), and the +// next comes a moment later. A terminal that renders what it is given +// (Windows' pseudo console) passes on each frame, not only how the last +// one left the screen. func fakeFrames(b []byte) { - park := []byte("\x1b[24;1H") + parks := [][]byte{[]byte("\x1b[24;1H"), []byte("\x1b[50;1H")} for len(b) > 0 { - i := bytes.Index(b, park) + i, n := -1, 0 + for _, park := range parks { + if j := bytes.Index(b, park); j >= 0 && (i < 0 || j < i) { + i, n = j, len(park) + } + } if i < 0 { _, _ = os.Stdout.Write(b) return } - _, _ = os.Stdout.Write(b[:i+len(park)]) - b = b[i+len(park):] + _, _ = os.Stdout.Write(b[:i+n]) + b = b[i+n:] time.Sleep(50 * time.Millisecond) } } @@ -845,6 +851,40 @@ func TestShimReadsUsage(t *testing.T) { } } +// Claude Code draws in the terminal the helper gives it, 200 by 50 on +// every system, so a panel with three windows sits below the 24th row. +// Each is read: read on an 80 by 24 grid, the rows under the 24th were +// drawn over one another, and only the last window drawn was told +// (#4988, "7 days · Fable" alone on a Team plan; shinnqy on X). +func TestShimReadsUsageBelowTheTwentyFourthRow(t *testing.T) { + // as 2.1.316 draws: from the top, down by rows (CR, ESC[nB), each + // figure placed at its column (ESC[55G) + var b strings.Builder + b.WriteString("\x1b[H\r\x1b[29B Settings Status Config Usage Stats\x1b[K") + for _, w := range []struct{ name, used, reset string }{ + {"Current session", "29%", "Resets 2pm (Asia/Shanghai)"}, + {"Current week (all models)", "17%", "Resets Nov 8 at 4am (Asia/Shanghai)"}, + {"Current week (Fable)", "0%", "Resets Nov 8 at 4am (Asia/Shanghai)"}, + } { + b.WriteString("\r\x1b[2B " + w.name + "\x1b[K") + b.WriteString("\r\x1b[1B " + strings.Repeat("█", 10) + "\x1b[55G" + w.used + "\x1b[59Gused\x1b[K") + b.WriteString("\r\x1b[1B " + w.reset + "\x1b[K") + } + b.WriteString("\r\x1b[2B Esc to cancel\x1b[K\x1b[50;1H") + // a frame later the session's figure is drawn again, only the cells + // that changed, as Claude Code's renderer does + b.WriteString("\x1b[H\r\x1b[32B\x1b[55G30%\x1b[50;1H") + panel := filepath.Join(t.TempDir(), "usage-200x50.bin") + if err := os.WriteFile(panel, []byte(b.String()), 0o600); err != nil { + t.Fatal(err) + } + out := runShim(t, []string{"FAKE_USAGE=" + panel}, append([]string{fakePath(t)}, pArgs...), []string{`raw:{"type":"command","command":"/usage"}`}, 2) + want := "Current session: 30% used · resets 2pm (Asia/Shanghai)\nCurrent week (all models): 17% used · resets Nov 8 at 4am (Asia/Shanghai)\nCurrent week (Fable): 0% used · resets Nov 8 at 4am (Asia/Shanghai)" + if r := out[1]; r["type"] != "result" || r["is_error"] != false || r["result"] != want { + t.Fatalf("told %v", r) + } +} + // Claude Code draws its banner again before /usage's panel, and a slow // /usage leaves it still for seconds. It is waited past, never told as the // panel: read as one, the banner was an allowance's error (ikunbots on X,