Skip to content

refactor(picker): read terminal size once from a canonical reader - #3219

Merged
max-sixty merged 3 commits into
mainfrom
picker-canonical-terminal-reader
Jun 25, 2026
Merged

max-sixty merged 3 commits into
mainfrom
picker-canonical-terminal-reader

Conversation

@max-sixty

@max-sixty max-sixty commented Jun 25, 2026 •

Copy link
Copy Markdown
Owner

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

The picker's startup path read terminal dimensions from two 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.

Add terminal_dimensions() -> Option<(usize, Option<usize>)> as the canonical
reader (stderr, then stdout, then COLUMNS); terminal_width() becomes its width
projection, so all its existing callers are unaffected. The height is Option
because COLUMNS has no height counterpart. The picker now reads it once and
derives both the layout and skim_list_width from that single snapshot.

skim_list_width is byte-identical (same chain, same usize::MAX fallback). The
only behavior change is the layout honoring the stderr/COLUMNS width when
stdout isn't a TTY (redirected stdout, or the headless dry-run/bench paths),
which is exactly the divergence being closed.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
max-sixty and others added 2 commits June 24, 2026 19:44
The canonical-reader switch left two picker fallback arms unexercised by the
dry-run suite, because the test harness always sets COLUMNS=150 (a width is
always detected, and 150 always selects the Right layout): the `None => (80,
24)` full fallback and the `PreviewLayout::Down` list-width arm. Add two
focused dry-run tests — one with COLUMNS removed (no width at all → default
dimensions) and one with a narrow COLUMNS (→ Down layout).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Reconciles the canonical terminal-reader change 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.

Also refines the layout fallback: a width-only `COLUMNS` reading (height
`None`) now falls back to 80x24 for the layout, exactly as before — the layout
trusts `term_dims` only when a real terminal supplied both dimensions, while
`skim_list_width` still uses the `COLUMNS` width. This keeps the change a no-op
for real terminals and preserves the prior headless behavior, so the two added
dry-run tests (which targeted the now-removed COLUMNS-drives-layout arms) are
dropped — existing PTY and dry-run tests cover both layout arms.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@max-sixty
max-sixty merged commit 546ea8f into main Jun 25, 2026
36 checks passed
@max-sixty
max-sixty deleted the picker-canonical-terminal-reader branch June 25, 2026 06:29
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