Menus: ←→ slide a description out from under the right edge
A tool's description is written to say what the tool is for, not to fit whatever column is left after the longest name — so it was read to the right edge and no further. The list screen has had per-row h-scrolling for a while (the Trophy case, the Watchtower log); the label+description lists just had no way to ask for it, because the builders that lay them out never see the screen they will be opened in. So the row asks. menu_rows and the main menu pin their label lane as each row's hscroll_from, and a list turns its scrolling on when any row declares one — a head block is only meaningful as the part that stays put, so a row that pins one is a row built to scroll. The name holds its place, the description slides under it, and a list whose rows all fit still ignores ←→ and says nothing about them in its footer. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
3376a18271
commit
70ccb6b31d
4 files changed
+59
-5
No files matched your search
+3
-1
@@ -478,7 +478,9 @@ async def _menu_loop(ctx: AppContext, session: TuiSession) -> None:
|
||||
row = Text(label)
|
||||
row.append(" " * (name_w - cell_len(label) + 2))
|
||||
row.append(tool.help, style="muted")
|
||||
items.append(Choice(title=row, value=tool.name))
|
||||
# The tool's name is the row's identity and always fits; only the description
|
||||
# runs long, so ←→ slide it alone under a pinned name (Choice.hscroll_from).
|
||||
items.append(Choice(title=row, value=tool.name, hscroll_from=name_w + 2))
|
||||
items.append(Separator(" "))
|
||||
items.append(Choice(title=command_label("🚪 Quit"), value="__quit__"))
|
||||
|
||||
|
||||
@@ -183,7 +183,9 @@ def menu_rows(rows: Iterable[tuple[Union[str, Text], str, Any]]) -> list:
|
||||
|
||||
Each label passes through :func:`command_label` first, so a platform that draws no
|
||||
icon lane loses it *before* the lane is measured — the description column moves left
|
||||
with the labels rather than going ragged behind them.
|
||||
with the labels rather than going ragged behind them. Each row also pins that column
|
||||
as its :attr:`~meshterm.ui.tui.select.Choice.hscroll_from`, so on an ``hscroll`` list
|
||||
←→ slide the description under a label that stays put.
|
||||
|
||||
Args:
|
||||
rows: ``(label, description, value)`` triples. A :class:`Text` label keeps its
|
||||
@@ -205,7 +207,9 @@ def menu_rows(rows: Iterable[tuple[Union[str, Text], str, Any]]) -> list:
|
||||
row.append_text(label)
|
||||
row.append(" " * (width - cell_len(label.plain) + 2))
|
||||
row.append(help_text, style="muted")
|
||||
items.append(Choice(title=row, value=value))
|
||||
# The label lane is the row's identity; only the description overflows, so the
|
||||
# h-scroll rides it alone and the name stays pinned (see Choice.hscroll_from).
|
||||
items.append(Choice(title=row, value=value, hscroll_from=width + 2))
|
||||
return items
|
||||
|
||||
|
||||
|
||||
@@ -69,7 +69,10 @@ class Choice:
|
||||
reader's place in the list, and sliding them off to read the walk costs the row
|
||||
its identity and gains nothing (the columns are the part that already fits).
|
||||
Set it to the cell width of that fixed block, measured from the row itself, so
|
||||
the scroll rides only the run that overflows.
|
||||
the scroll rides only the run that overflows. Setting it at all also *turns the
|
||||
list's scrolling on* (see :class:`SelectScreen`): a head block is only meaningful
|
||||
as the part that stays put, so a row that pins one is a row built to scroll,
|
||||
wherever the builder's list ends up being shown.
|
||||
"""
|
||||
|
||||
title: Union[str, Text, Callable[[], Union[str, Text]], Callable[[int], Union[str, Text]]]
|
||||
@@ -306,7 +309,15 @@ class SelectScreen(Screen):
|
||||
"""
|
||||
super().__init__()
|
||||
self.title = title
|
||||
self._hscroll = hscroll
|
||||
# A row that pins a head block (:attr:`Choice.hscroll_from`) is a row built to
|
||||
# scroll — the head is only meaningful as "the part that stays put" — so declaring
|
||||
# one turns the list's scrolling on without the builder having to reach the screen
|
||||
# it will be shown in. That is what makes every label+description list
|
||||
# (:func:`~meshterm.ui.menus.menu_rows`, the main menu) scroll its description
|
||||
# wherever it is opened, ``ctx.ui.select`` included.
|
||||
self._hscroll = hscroll or any(
|
||||
getattr(item, "hscroll_from", 0) > 0 for item in items
|
||||
)
|
||||
self._hscroll_hint = hscroll_hint
|
||||
self._hshift = 0
|
||||
self._last_width = 0 # the last render width, for the footer's overflow probe
|
||||
|
||||
@@ -2480,6 +2480,43 @@ def test_select_hscroll_from_pins_the_rows_head_and_slides_only_its_run() -> Non
|
||||
assert end.startswith("❯ " + lanes) and "-end" in end # the tail is reachable
|
||||
|
||||
|
||||
def test_select_scrolls_for_a_row_that_pins_a_head_without_being_told() -> None:
|
||||
"""A row declaring ``hscroll_from`` turns its list's scrolling on by itself.
|
||||
|
||||
The builders that lay out label+description rows (:func:`~meshterm.ui.menus.menu_rows`,
|
||||
the main menu) never see the screen their list is opened in — ``ctx.ui.select`` builds
|
||||
it — so the row is where the intent has to live.
|
||||
"""
|
||||
from meshterm.ui.tui.select import Choice, SelectScreen
|
||||
|
||||
lanes = "Send advert "
|
||||
row = Choice(lanes + "Announce this node " + "and then some " * 6, 1,
|
||||
hscroll_from=len(lanes))
|
||||
screen = SelectScreen("menu", [row]) # no hscroll= anywhere
|
||||
screen.handle("right")
|
||||
drawn = _row_plains(screen, 40)[0]
|
||||
assert drawn.startswith("❯ " + lanes) # the name stays pinned…
|
||||
assert "Announce this node" not in drawn # …and the description has slid under it
|
||||
# A list of plain rows still ignores ←→ entirely.
|
||||
plain = SelectScreen("menu", [Choice("x" * 80, 1)])
|
||||
before = _row_plains(plain, 40)[0]
|
||||
plain.handle("right")
|
||||
assert _row_plains(plain, 40)[0] == before
|
||||
|
||||
|
||||
def test_menu_rows_pin_their_label_lane_so_only_the_description_slides() -> None:
|
||||
"""The shared label+description builder hands each row its own head block."""
|
||||
from meshterm.ui.menus import menu_rows
|
||||
|
||||
rows = menu_rows([("Sync clock…", "Set the device clock", 1),
|
||||
("Reboot device…", "Restart the companion", 2)])
|
||||
lane = rows[0].hscroll_from
|
||||
assert lane and all(row.hscroll_from == lane for row in rows)
|
||||
# The head block ends exactly where the descriptions start, on every row.
|
||||
for row in rows:
|
||||
assert row.label.plain[lane:].startswith(("Set the", "Restart the"))
|
||||
|
||||
|
||||
def test_select_hscroll_hint_is_gated_on_the_run_not_the_whole_row() -> None:
|
||||
"""A row whose *run* fits earns no ←→ atom, however wide its pinned head makes it.
|
||||
|
||||
|
||||
Reference in new issue
Block a user