Repository navigation
refactor(picker): read terminal size once for layout sizing - #3210
Merged
Merged
Conversation
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]>
worktrunk-bot
approved these changes
Jun 24, 2026
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 thenum_items_estimatecap, once each insideto_preview_window_specandpreview_dimensions, once for the speculative pre-compute, and once forhalf_page.to_preview_window_specre-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_pickernow readsterminal_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_forformats 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), andto_preview_window_spec(which re-read and recomputed).preview.rsno longer reads the terminal at all — the read lives solely inhandle_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, andhalf_pageon that fallback still evaluates to10((available_height(24) / 2).max(5)=(21 / 2).max(5)=10), identical to the old.unwrap_or(10). The pre-existingdimensions_forscenario/edge tests pass unchanged; the one spec-formatting test was retargeted atspec_forwith exact-string assertions (strictly stronger), and a redundant duplicate of it inmod.rswas removed.cargo run -- hook pre-merge --yesis green (4181 tests, clippy, fmt).