Skip to content

Tui: re-measure the terminal on SIGWINCH, not on a timer - #62

Merged
junjihashimoto merged 1 commit into
mainfrom
tui-sigwinch
Nov 29, 2026
Merged

junjihashimoto merged 1 commit into
mainfrom
tui-sigwinch

Conversation

@junjihashimoto

Copy link
Copy Markdown
Contributor

When idle, the TUI loop spawned stty size every 500 ms to notice window resizes. Each spawn costs 6–15 ms. With this change it measures the terminal once at start, and again only after a SIGWINCH.

Change

  • ResizeWatch (in LeanTea/Tui/App.lean) sets up a repeating Std.Async.Signal.Waiter for .sigwinch. Each signal sets a flag, and the loop reads and clears it each turn. A burst of signals therefore causes only one re-measure.
  • No thread blocks on the signal: each round registers a callback on the signal's task and arms the next one.
  • The watch is armed before the first measure, so a resize that lands between the two is still seen.
  • If the handler can't be set up, the loop falls back to polling every 500 ms. With a fixed width/height there's no watch at all.
  • Raw mode still uses stty, twice per run, because Lean has no termios API. It is requested upstream in Terminal raw mode and window size for IO.FS.Handle / stdin leanprover/lean4#16868, and a comment says so.

Tests

  • tui_spec (73 passed) has a new "following resizes" group. It sends SIGWINCH to its own process and checks four things:
    • the signal is reported;
    • take clears it;
    • a second signal is reported too;
    • two signals in a row give one re-measure.
  • The headless Tui.Test harness is unchanged; its existing Session.resize tests still pass.
  • I ran tui_mouse in a pty, changed the size with TIOCSWINSZ from 60×20 to 120×30, and:
    • the next frame cleared the screen and drew row 30;
    • Ctrl+C then exited with status 0, so the signal doesn't break the blocking stdin read.
lake build tui_spec tui_mouse
./.lake/build/bin/tui_spec     # 73 passed, 0 failed

The app loop spawned `stty size` (6-15 ms) every 500 ms while idle to
notice window resizes. It now measures once at start and again only
after a SIGWINCH. `ResizeWatch` sets up a repeating
`Std.Async.Signal.Waiter`; each signal sets a flag, and the loop reads
and clears it each turn, so a burst of signals is one re-measure. The
waiter is armed before the first measure, so a resize in between is
not lost. Nothing blocks a thread: each round registers a callback on
the signal's task. If the handler cannot be set up, the loop polls
every 500 ms as before; with a fixed size there is no watch at all.

Raw mode still uses `stty`, twice per run: Lean has no termios API
(requested in leanprover/lean4#16868), noted in a comment.

tui_spec has a resize group that sends SIGWINCH to its own process:
reported, cleared on take, reported again, and a burst is one
re-measure. The headless Tui.Test harness is unchanged. Driven in a
pty, tui_mouse repaints at the new size after TIOCSWINSZ and exits
cleanly on Ctrl+C.
@junjihashimoto
junjihashimoto merged commit 98a44c9 into main Nov 29, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant