Select lists: horizontal scroll rides the highlighted row alone

The opt-in hscroll shifted every row (and separator) sideways together, so
reading one long line dragged the whole screen with it and ←/→ advertised
themselves even when nothing overflowed. Make the shift per-row: only the
highlighted row slides, and only as far as its own tail, under a pinned
pointer; every other row and each column header stays put. A row that fits
the width can't shift at all, and moving the highlight to another row (or
editing the filter) drops the scroll back to the start — each row scrolls
on its own.

Surface the ←→ atom from the list itself, inserted right after the move
atom only while the highlighted row actually overflows, so the key shows
up exactly when it would do something. Watchtower's alert log — the one
hscroll user — drops the hardcoded "←→ scroll" from its footer and lets
the list add it on demand.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Jean-Pierre MartineauandClaude Opus 4.8 committed 2026-07-19 14:09:09 -04:00
1 parent f164201ed6
commit 79fb014c52
3 files changed
+114 -41

No files matched your search

+77 -25
View File
@@ -80,6 +80,18 @@ def _splice_hint(base: str, segment: str) -> str:
return f"{base[:idx]} · {segment}{base[idx:]}"
def _insert_atom(base: str, atom: str, index: int = 1) -> str:
"""Insert ``atom`` as the ``index``-th ` · `-separated atom of a footer hint.
Surfaces a conditional *navigation* atom (the ←→ per-row scroll) right after the
leading move atom, keeping the hint's navigation-then-actions-then-``Esc`` shape — where
:func:`_splice_hint` instead places an *action* atom just before the trailing ``Esc``.
"""
parts = base.split(" · ")
parts.insert(min(index, len(parts)), atom)
return " · ".join(parts)
@dataclass
class Separator:
"""A non-selectable row between choices.
@@ -100,6 +112,14 @@ class Separator:
Item = "Choice | Separator"
#: The navigation actions that move the highlight to another row, so an ``hscroll`` list
#: drops the current row's horizontal shift (each row scrolls on its own — see
#: :meth:`SelectScreen.handle`). The filter edits reset it in their own branches.
_HSHIFT_RESET_ACTIONS = frozenset({
"up", "down", "pageup", "pagedown", "home", "ctrl_home", "end", "ctrl_end",
"ctrl_pageup", "ctrl_pagedown",
})
class SelectScreen(Screen):
"""A grouped, filterable, single-choice list.
@@ -123,6 +143,7 @@ class SelectScreen(Screen):
filterable: bool = True,
wrap: bool = True,
hscroll: bool = False,
hscroll_hint: str = "←→ scroll",
) -> None:
"""Build a select screen.
@@ -143,16 +164,24 @@ class SelectScreen(Screen):
wrap: Whether the highlight wraps around the ends (Down from the last row jumps
to the first, and vice versa). Off for grouped lists where wrapping across the
section headings reads as a jarring jump rather than continuing to scroll.
hscroll: Whether ←/→ shift every row sideways so an over-long line can be read
to its end (the Watchtower's alert log). Off by default — rows simply
ellipsize at the right edge and ←/→ stay inert, exactly as before. The
shift survives ↑↓ moves (you scrolled to a column; walking rows keeps it)
and resets when the filter is edited.
hscroll: Whether ←/→ horizontally scroll the *highlighted* row so an over-long
line can be read to its end (the Watchtower's alert log). Off by default —
rows simply ellipsize at the right edge and ←/→ stay inert. Only a row that
actually overflows the width scrolls; a short row (and every separator or
column header) stays put, and the shift resets to the start whenever the
highlight moves to another row or the filter is edited — each row scrolls
on its own, independently of the rest of the screen.
hscroll_hint: The footer atom surfaced (as the second ` · ` atom, right after
the move atom) while ``hscroll`` is on and the highlighted row overflows —
so ←→ advertises itself exactly when it would do something. Ignored when
``hscroll`` is off.
"""
super().__init__()
self.title = title
self._hscroll = hscroll
self._hscroll_hint = hscroll_hint
self._hshift = 0
self._last_width = 0 # the last render width, for the footer's overflow probe
if footer_hint is None:
footer_hint = (
"↑↓ move · type to filter · Enter select · Esc back"
@@ -250,19 +279,40 @@ class SelectScreen(Screen):
@property
def footer_hint(self) -> str:
"""The footer key hint, gaining the :attr:`_delete_hint` atom on a deletable row.
"""The footer key hint, gaining dynamic atoms only where their keys would act.
Fixed to the base hint unless a ``delete_hint`` was supplied *and* the highlighted
row opted into removal — then the atom is spliced in just before the trailing ``Esc``
clause (see :func:`_splice_hint`), so the removal key shows up on the bottom border
precisely while the cursor is on a row it can act on.
Two conditional atoms fold in exactly where they apply, so the bottom border never
advertises a key that would do nothing:
* the :attr:`_delete_hint` atom, spliced just before the trailing ``Esc`` clause
(see :func:`_splice_hint`) while the highlighted row is :attr:`Choice.deletable`;
* the :attr:`_hscroll_hint` atom (``hscroll`` lists only), inserted right after the
move atom (see :func:`_insert_atom`) while the highlighted row overflows the width
— a short row scrolls nowhere, so ←→ stays hidden on it.
"""
base = self._footer_base
current = self._current_choice()
if self._delete_hint and current is not None and current.deletable:
return _splice_hint(base, self._delete_hint)
base = _splice_hint(base, self._delete_hint)
if self._hscroll and self._hscroll_hint and self._selected_overflows():
base = _insert_atom(base, self._hscroll_hint)
return base
def _selected_overflows(self) -> bool:
"""Whether the highlighted row's label is too wide for the last render width.
The gate for both the ←→ scroll and its footer atom: a row that fits has nothing to
scroll. Measured against the row's content area (the width less the 2-cell pointer),
using the width the last :meth:`render_body` saw (``0`` before the first paint, so
nothing reads as overflowing until a real width is known).
"""
if not self._hscroll or self._last_width <= 0:
return False
current = self._current_choice()
if current is None:
return False
return cell_len(_plain(current.label)) > max(1, self._last_width - 2)
@property
def sizing_footer_hint(self) -> str:
"""The fullest the footer can get, for stable box sizing (see :attr:`footer_hint`).
@@ -304,15 +354,15 @@ class SelectScreen(Screen):
self._index = max(0, min(self._index, len(choices) - 1)) if choices else 0
selected = choices[self._index] if choices else None
# Clamp an active horizontal shift to the widest row, so → stops at the point
# where the longest line's tail has come into view (measured fresh each paint —
# callable titles may have changed width).
# Horizontal scroll rides only the *highlighted* row, and only as far as its own
# tail: → stops once that row's end is in view, and a short (or unselected) row
# can't shift at all. Measured fresh each paint — a callable title may have changed
# width — against the row's content area (width less the 2-cell pointer).
self._last_width = width
if self._hscroll and self._hshift:
widest = 0
for item in rows:
label = item.title if isinstance(item, Separator) else item.label
widest = max(widest, cell_len(_plain(label)) + 2)
self._hshift = max(0, min(self._hshift, widest - width))
avail = max(1, width - 2)
sel_len = cell_len(_plain(selected.label)) if selected is not None else 0
self._hshift = max(0, min(self._hshift, sel_len - avail))
lines: list[str] = []
# A prompt (when set) sits above the list, offsetting every row below it; the cursor
@@ -332,11 +382,10 @@ class SelectScreen(Screen):
for item in rows:
if isinstance(item, Separator):
# A Text title carries its own spans (a two-colour column header); a plain
# string is drawn uniformly in the separator's style.
# string is drawn uniformly in the separator's style. Separators never
# h-scroll — the shift rides the highlighted choice row alone.
title = item.title
heading = title if isinstance(title, Text) else Text(title, style=item.style)
if self._hscroll and self._hshift:
heading = crop_cells(heading, self._hshift, width)
sep = render_to_ansi(heading, width)
self._sticky_headers.append((len(lines), sep))
lines.append(sep)
@@ -350,8 +399,9 @@ class SelectScreen(Screen):
# keeps its colour. A plain string is styled uniformly as before.
text = Text(pointer, style=style)
label_text = label if isinstance(label, Text) else Text(label)
if self._hscroll and self._hshift:
# The 2-cell pointer stays pinned; only the label slides under it.
if self._hscroll and self._hshift and is_sel:
# Only the highlighted row slides, and only its label — the 2-cell pointer
# stays pinned. Every other row (and separator) renders unshifted.
label_text = crop_cells(label_text, self._hshift, max(1, width - 2))
text.append_text(label_text)
text.style = style
@@ -376,6 +426,8 @@ class SelectScreen(Screen):
def handle(self, action: str, data: str = "") -> None:
"""Move the highlight, edit the filter, or commit/cancel the selection."""
choices = self._choices()
if self._hscroll and action in _HSHIFT_RESET_ACTIONS:
self._hshift = 0 # moving off a row abandons its scroll — each row scrolls alone
if action == "up":
if choices:
self._index = (self._index - 1) % len(choices) if self._wrap else max(
@@ -409,7 +461,7 @@ class SelectScreen(Screen):
elif action == "left" and self._hscroll:
self._hshift = max(0, self._hshift - self._HSCROLL_STEP)
elif action == "right" and self._hscroll:
self._hshift += self._HSCROLL_STEP # clamped to the widest row at render
self._hshift += self._HSCROLL_STEP # clamped to the highlighted row's tail at render
elif action == "escape":
super().handle("escape")
elif action == "backspace" and self._filterable:
+3 -1
View File
@@ -120,7 +120,9 @@ async def open_watchtower(ctx: "AppContext") -> Optional[dict[str, Any]]:
default=cursor,
wrap=False,
hscroll=True,
footer_hint="↑↓ move · ←→ scroll · Enter select/acknowledge · Esc back",
# ←→ scroll is surfaced by the list itself, but only while the highlighted
# alert actually overflows the width (see SelectScreen.hscroll_hint).
footer_hint="↑↓ move · Enter select/acknowledge · Esc back",
)
menu.future = loop.create_future()
session.push(menu)
+34 -15
View File
@@ -1850,38 +1850,57 @@ def _row_plains(screen, width: int) -> list[str]:
return [re.sub(r"\x1b\[[0-9;]*m", "", ln) for ln in screen.render_body(width)]
def test_select_hscroll_shifts_rows_and_pins_the_pointer() -> None:
"""→ slides row content left under the pinned pointer; ← slides it back."""
screen = _hscroll_screen()
def test_select_hscroll_shifts_only_the_highlighted_row() -> None:
"""→ slides the highlighted row under its pinned pointer; other rows and headers hold."""
screen = _hscroll_screen() # the long "row-one" is highlighted by default
before = _row_plains(screen, 40)
assert any("row-one-" in ln for ln in before)
screen.handle("right")
shifted = _row_plains(screen, 40)
assert not any("row-one-" in ln for ln in shifted) # the head scrolled off
assert any(ln.startswith("❯ ") for ln in shifted) # the pointer stays pinned
assert any("HEAD-" not in ln and "hhh" in ln for ln in shifted) # headers slide too
# The highlighted row's head scrolled off, under the still-pinned pointer…
assert any(ln.startswith("❯ ") for ln in shifted)
assert not any("row-one-" in ln for ln in shifted)
# …but the section header did not move, and the short row is untouched.
assert any(ln.strip().startswith("HEAD-") for ln in shifted)
assert any("short" in ln for ln in shifted)
screen.handle("left")
assert any("row-one-" in ln for ln in _row_plains(screen, 40))
def test_select_hscroll_clamps_at_the_widest_row() -> None:
"""→ stops once the longest row's tail is in view instead of scrolling to blank."""
def test_select_hscroll_clamps_at_the_highlighted_rows_tail() -> None:
"""→ stops once the *highlighted* row's own end is in view, not the widest row's."""
screen = _hscroll_screen()
for _ in range(50):
screen.handle("right")
plains = _row_plains(screen, 40)
assert any("-tail" in ln for ln in plains) # the longest row's end is visible
assert screen._hshift <= 60 + len("row-one--tail") + 2
plains = _row_plains(screen, 40) # the render clamps the shift
assert any("-tail" in ln for ln in plains) # the highlighted row's end is visible
row_len = len("row-one-" + "x" * 60 + "-tail")
assert screen._hshift == row_len - (40 - 2) # clamped to its tail (width less pointer)
def test_select_hscroll_survives_moves_and_resets_on_filter() -> None:
"""The shift holds across ↑↓ (a chosen column) and resets when the filter edits."""
def test_select_hscroll_resets_when_the_highlight_moves() -> None:
"""The shift is per-row: moving the highlight (or editing the filter) drops it to the start."""
screen = _hscroll_screen()
screen.handle("right")
screen.handle("down")
assert screen._hshift > 0
screen.handle("text", "r")
screen.handle("down") # moving to another row abandons that row's scroll
assert screen._hshift == 0
screen.handle("right")
screen.handle("text", "r") # a filter edit resets it too
assert screen._hshift == 0
def test_select_hscroll_only_acts_on_an_overflowing_row() -> None:
"""←→ and its footer atom appear only while the highlighted row overflows the width."""
screen = _hscroll_screen()
screen.render_body(40) # the long row-one is highlighted and overflows 40 cells
assert "←→ scroll" in screen.footer_hint
screen.handle("down") # the short row fits — nothing to scroll
screen.render_body(40)
assert "←→ scroll" not in screen.footer_hint
screen.handle("right")
screen.render_body(40)
assert screen._hshift == 0 # a row that fits can't shift
def test_select_without_hscroll_ignores_left_right() -> None: