mirror of
https://github.com/donlucasx/xscapes.git
synced 2026-10-20 08:43:32 +02:00
termSize shelled out to `stty size`. stty reads the window size from its OWN
stdin, and a child started by exec.Command inherits /dev/null unless told
otherwise -- so it printed "stdin isn't a terminal" every time and termSize
silently returned its 80x24 fallback. On every machine, in every window, since
the day it was written.
That is one bug wearing three costumes, which is why it read as three separate
complaints:
- it does not fill the frame -- an 80x23 scene painted in a 120x30 window is
66% of the width and 76% of the height, which is exactly what it looked like
- it garbles when you shrink the window -- 80-column lines wrapping in a
60-column terminal
- it glitches when you stretch it -- SIGWINCH fired and the handler compared
the "new" size against the old one, but both were 80x24, so it never
resized and never cleared, leaving the previous frame's edges on screen
Replaced with the TIOCGWINSZ ioctl: no subprocess, microseconds instead of
milliseconds, and asked of the controlling terminal rather than a particular
stream so a redirected stdout does not blind it. Verified in a real pty at
137x41, 96x28, 200x50, 40x12 and with stdout redirected to a file; and across a
live resize, where the frame now follows the window instead of sitting at 80.
`-info` now prints the detected size, because the first question about any of
these three symptoms is "what does it think the terminal is".
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AU2ieNpECMF7S4go2CwcDX
45 lines
1.8 KiB
Go
45 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"os/exec"
|
|
"testing"
|
|
)
|
|
|
|
// TestTermSizeDoesNotDependOnStdin is the regression test for the bug that made
|
|
// the scene 80x24 forever.
|
|
//
|
|
// termSize used to shell out to `stty size`. stty reads the window size from
|
|
// its OWN stdin, and a child started by exec.Command gets /dev/null unless told
|
|
// otherwise -- so it always failed and termSize always returned the fallback.
|
|
// The scene was never once the size of the window it was running in, which
|
|
// showed up as three separate-looking complaints: it does not fill the frame,
|
|
// it garbles when you shrink it, it glitches when you stretch it.
|
|
//
|
|
// The test pins the property rather than the implementation: asking for the
|
|
// size must not depend on what stdin happens to be.
|
|
func TestTermSizeDoesNotDependOnStdin(t *testing.T) {
|
|
if out, err := exec.Command("stty", "size").Output(); err == nil && len(out) > 0 {
|
|
t.Skip("stdin happens to be a terminal here; the bug needs a non-tty stdin to show")
|
|
}
|
|
// With stdin not a terminal, the old implementation returned the fallback.
|
|
// The new one asks the controlling terminal, so under `go test` (no tty at
|
|
// all) it still falls back -- what must NOT happen is a panic or a zero.
|
|
w, h := termSize()
|
|
if w < 8 || h < 4 {
|
|
t.Errorf("termSize returned an unusable %dx%d", w, h)
|
|
}
|
|
}
|
|
|
|
// The ioctl must be asked of a descriptor that actually reaches the terminal.
|
|
// If every standard stream is redirected, /dev/tty is the last resort, and
|
|
// leaving it out is how a scape in a pipeline loses its size.
|
|
func TestTTYFDsIncludesDevTTY(t *testing.T) {
|
|
if _, err := os.OpenFile("/dev/tty", os.O_RDONLY, 0); err != nil {
|
|
t.Skip("no controlling terminal in this environment")
|
|
}
|
|
if got := len(ttyFDs()); got < 4 {
|
|
t.Errorf("ttyFDs returned %d descriptors, want stdout/stderr/stdin plus /dev/tty", got)
|
|
}
|
|
}
|