Skip to content

refactor(picker): read terminal size once for layout sizing - #3210

Merged
max-sixty merged 1 commit into
mainfrom
picker-single-terminal-read
Jun 24, 2026
Merged

max-sixty merged 1 commit into
mainfrom
picker-single-terminal-read

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

What

Collapses the interactive picker's repeated terminal_size() reads into a single read, threaded explicitly through the layout-sizing code.

Why

PR #3205 made the picker's Down-layout list height adapt to the terminal, but left the startup path reading the terminal size 3–4 times per launch — once in auto_detect (layout), once for the num_items_estimate cap, once each inside to_preview_window_spec and preview_dimensions, once for the speculative pre-compute, and once for half_page. to_preview_window_spec re-read the terminal and recomputed the Down spec internally, so the Down preview dimensions were computed twice. Beyond the redundant syscalls, the estimate cap and the actual layout could observe different terminal sizes if the window was resized mid-startup — a benign but real race.

How

handle_picker now reads terminal_size::terminal_size() once and threads (term_width, term_height) into every sizing site: layout detection (PreviewLayout::for_dimensions), the visible-row cap (max_visible_items(available_height(term_height))), the preview dimensions (dimensions_for), the speculative pre-compute, and the half-page scroll. dimensions_for — already pure and unit-tested — is the single entry; spec_for formats the skim preview-window spec from the already-computed dims rather than recomputing them.

This retires three terminal-reading methods on PreviewLayout: auto_detect (folded into the single read + for_dimensions), preview_dimensions (the live-terminal reader), and to_preview_window_spec (which re-read and recomputed). preview.rs no longer reads the terminal at all — the read lives solely in handle_picker. crate::display::terminal_width() (a separate stderr-first width probe for the skim list column) is left as-is; it isn't part of the layout-sizing path.

Behavior

No user-facing change. Fallbacks are preserved at every site — the single read falls back to (80, 24), matching the prior per-call fallbacks, and half_page on that fallback still evaluates to 10 ((available_height(24) / 2).max(5) = (21 / 2).max(5) = 10), identical to the old .unwrap_or(10). The pre-existing dimensions_for scenario/edge tests pass unchanged; the one spec-formatting test was retargeted at spec_for with exact-string assertions (strictly stronger), and a redundant duplicate of it in mod.rs was removed.

cargo run -- hook pre-merge --yes is green (4181 tests, clippy, fmt).

This was written by Claude Code on behalf of max

The picker startup path read terminal_size() 3-4 times per launch and
computed the Down preview spec twice. Read the terminal once in
handle_picker and thread (term_width, term_height) into layout detection,
the visible-row cap, the preview dimensions, the speculative pre-compute,
and the half-page scroll — so the estimate cap and the actual layout can
never observe different terminal sizes across a resize.

dimensions_for is now the single pure entry; spec_for formats the skim
preview-window spec from already-computed dims rather than recomputing.
This folds away PreviewLayout::auto_detect and preview_dimensions (both
read the live terminal) and to_preview_window_spec (which re-read and
recomputed the dims internally).

No user-facing behavior change; fallbacks are preserved at every site.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@max-sixty
max-sixty merged commit 93cfa78 into main Jun 24, 2026
36 checks passed
@max-sixty
max-sixty deleted the picker-single-terminal-read branch June 24, 2026 19:29
max-sixty added a commit that referenced this pull request Jun 25, 2026
)

PR #3210 collapsed the picker's repeated `terminal_size()` reads into
one, but left the startup path reading dimensions from two different
sources: the layout sizing used `terminal_size::terminal_size()`
(stdout-first, `(80, 24)` fallback) while `skim_list_width` used
`display::terminal_width()` (stderr-first, `COLUMNS` fallback). On a
terminal where stdout and stderr point to different places, the preview
sizing and the list-column width could observe different widths.

This adds `terminal_dimensions() -> Option<(usize, Option<usize>)>` in
`styling/mod.rs` as the single canonical reader, with the existing probe
chain: stderr, then stdout, then `COLUMNS`. `terminal_width()` becomes
its width projection (`terminal_dimensions().map(|(w, _)| w)`), so its
external contract and all six other callers are untouched. The height is
`Option` because `COLUMNS` supplies a width with no height counterpart.

The picker reads the canonical source once as `term_dims` and derives
both the layout and `skim_list_width` from that one snapshot.

## The height carries the "real terminal detected" signal

The layout needs both width and height, so it trusts the snapshot only
when a real terminal supplied both — `Some((w, Some(h)))`. A width-only
`COLUMNS` reading (`Some((w, None))`), or no reading at all, falls back
to `80x24` for the layout, exactly as the old stdout-only read did.
`skim_list_width` needs only a width, so it still uses the `COLUMNS`
width via the same snapshot. This resolves the `COLUMNS`/height
asymmetry cleanly: the same read serves both callers, the
`Option<height>` is load-bearing, and the prior fallback behavior is
preserved.

## Behavior

For a real terminal — the picker requires a TTY, so the normal case —
both the layout and `skim_list_width` see the detected dimensions, so
the change is a no-op. `terminal_width()` is byte-identical for every
input, leaving its callers (`progress`, `help`, `styling::format`,
`commands::mod`, `list::layout`, `list::collect`) unaffected. The one
observable change is the rare split where stdout is not a TTY but stderr
is: the layout now tracks the real terminal via the stderr probe
(matching `skim_list_width` and what skim renders on) instead of falling
back to `80x24`.

`list::progressive_table` keeps its own stdout-only height probe: `wt
list` renders to stdout, so it must detect a real stdout TTY rather than
a stderr/`COLUMNS` fallback; folding it into the stderr-first reader
would change its behavior, so it stays separate.

This branch also merges `main`, reconciling with #3214 (the picker now
lays its table out at full width regardless of preview layout):
`skim_list_width` takes the full width from the same `term_dims`
snapshot, dropping the obsolete Right/Down split.

Both layout arms and the width derivation are covered by the existing
PTY picker tests (real terminal) and dry-run tests (`COLUMNS`-only
fallback); two unit tests lock the `terminal_width` ↔
`terminal_dimensions` delegation. The full local gate is green apart
from `test_switch_picker_alt_l_does_not_hscroll`, a picker PTY snapshot
that is environment-sensitive on macOS (it fails the same way on `main`
locally) and passes on CI — the change is output-neutral for it.

> _This was written by Claude Code on behalf of max_

---------

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
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.

2 participants