Reorder screen grows Apply/Back action rows below the list

Follow the device-config editor's exit-group pattern: once the order
has actually changed, an ok-tinted Apply joins an err-tinted
"Back — discard changes"; untouched, a lone Back. Enter on a list row
now grabs/drops like Space (matching Enter-activates-the-row), and
committing happens on the Apply row instead of Enter-anywhere.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Jean-Pierre MartineauandClaude Fable 5 committed 2026-07-10 03:14:34 -04:00
1 parent 1d62691599
commit 8c75aea3b0
3 files changed
+107 -28

No files matched your search

+61 -18
View File
@@ -292,14 +292,20 @@ class SelectScreen(Screen):
class ReorderScreen(Screen):
"""A list whose rows the user rearranges in place with the arrow keys.
Move the cursor with ↑/↓; press Space to *grab* the highlighted row, then ↑/↓ carry it
up and down the list; press Space again to *drop* it. Enter commits (OK), resolving
with the final order as a list of the original row indices (so ``[2, 0, 1]`` means
"the row that started third is now first"); Esc cancels, resolving :data:`CANCEL` so
the caller keeps the original order — matching the Enter-accepts/Esc-cancels contract
of the other dialogs.
Move the cursor with ↑/↓; press Space (or Enter) on a row to *grab* it, then ↑/↓ carry
it up and down the list; press Space (or Enter) again to *drop* it. Below the list sit
the action rows, following the config editor's pattern: once the order has actually
changed, an ok-tinted *Apply* joins an err-tinted *Back — discard*; while it is
untouched there is only a plain *Back*. Enter on Apply commits, resolving with the
final order as a list of the original row indices (so ``[2, 0, 1]`` means "the row
that started third is now first"); Enter on Back — like Esc anywhere — resolves
:data:`CANCEL` so the caller keeps the original order.
"""
#: Action-row sentinels (kept distinct from list positions, which are ints).
_APPLY = "apply"
_BACK = "back"
def __init__(self, title: str, labels: list[str]) -> None:
"""Build a reorder screen.
@@ -319,11 +325,28 @@ class ReorderScreen(Screen):
def footer_hint(self) -> str: # type: ignore[override]
"""Key hint, phrased for whether a row is currently grabbed."""
if self._grabbed:
return "↑↓ move row · Space drop · Enter OK · Esc cancel"
return "↑↓ choose · Space grab · Enter OK · Esc cancel"
return "↑↓ move row · Space drop · Esc cancel"
return "↑↓ move · Space grab · Enter select · Esc cancel"
def _dirty(self) -> bool:
"""Whether the rows have actually left their original order."""
return self._order != list(range(len(self._order)))
def _actions(self) -> list[tuple[str, Text]]:
"""The action rows below the list, matching the config editor's exit group:
Apply joins Back only once there is a change to apply, and Back then spells
out the consequence of leaving.
"""
if self._dirty():
return [
(self._APPLY, Text.assemble(("✓ ", "ok"), "Apply new order")),
(self._BACK, Text.assemble(("✗ ", "err"), "Back — discard changes")),
]
return [(self._BACK, Text("Back"))]
def render_body(self, width: int) -> list[str]:
"""Render each row, marking the cursor (and, when grabbed, the moving row)."""
"""Render the rows, a blank spacer, then the action group, marking the cursor."""
n = len(self._order)
lines: list[str] = []
for pos, orig in enumerate(self._order):
is_cursor = pos == self._index
@@ -337,7 +360,19 @@ class ReorderScreen(Screen):
overflow="ellipsis")
text.truncate(width)
lines.append(render_to_ansi(text, width))
self._cursor = self._index
lines.append("")
for i, (_key, label) in enumerate(self._actions()):
if n + i == self._index:
# The cursor row goes full-brand like the list rows above it,
# trading the ✓/✗ tint for the highlight.
text = Text("❯ " + label.plain, style="brand", no_wrap=True)
else:
text = Text(" ", no_wrap=True)
text.append_text(label)
text.truncate(width, overflow="ellipsis")
lines.append(render_to_ansi(text, width))
# The spacer line offsets every action row by one on screen.
self._cursor = self._index if self._index < n else self._index + 1
return lines
def cursor_line(self) -> Optional[int]:
@@ -345,28 +380,36 @@ class ReorderScreen(Screen):
return getattr(self, "_cursor", None)
def handle(self, action: str, data: str = "") -> None:
"""Move the cursor, carry a grabbed row, toggle grab, commit on Enter, or cancel on Esc."""
"""Move the cursor, carry a grabbed row, grab/drop, run an action, or cancel on Esc."""
n = len(self._order)
total = n + len(self._actions())
if action == "up":
if self._grabbed and self._index > 0:
self._order[self._index - 1], self._order[self._index] = (
self._order[self._index], self._order[self._index - 1])
self._index -= 1
elif not self._grabbed and n:
self._index = (self._index - 1) % n
elif not self._grabbed and total:
self._index = (self._index - 1) % total
elif action == "down":
if self._grabbed and self._index < n - 1:
self._order[self._index + 1], self._order[self._index] = (
self._order[self._index], self._order[self._index + 1])
self._index += 1
elif not self._grabbed and n:
self._index = (self._index + 1) % n
elif not self._grabbed and total:
self._index = (self._index + 1) % total
elif action == "space" or (action == "text" and data == " "):
# The session delivers the spacebar as printable text; accept the normalized
# "space" action too for symmetry with the scroll screens.
self._grabbed = not self._grabbed
# "space" action too for symmetry with the scroll screens. Grabbing only
# means anything on a list row.
if self._index < n:
self._grabbed = not self._grabbed
elif action == "enter":
self.resolve(list(self._order))
if self._index < n:
self._grabbed = not self._grabbed # Enter grabs/drops, like Space
elif self._actions()[self._index - n][0] == self._APPLY:
self.resolve(list(self._order))
else:
super().handle("escape") # Back resolves CANCEL, same as Esc
elif action == "escape":
super().handle("escape")
+3 -2
View File
@@ -396,8 +396,9 @@ class TuiSession:
async def reorder(self, title: str, labels: list[str]) -> list[int]:
"""Show a drag-with-arrows reorder screen; return the final order of row indices.
Enter commits the rearrangement; Esc cancels, which comes back as the original
(identity) order so the caller treats it as "no change".
The Apply action row below the list commits the rearrangement; Back (or Esc)
cancels, which comes back as the original (identity) order so the caller
treats it as "no change".
"""
result = await self.run_screen(ReorderScreen(title, labels))
return list(range(len(labels))) if result is CANCEL else result
+43 -8
View File
@@ -327,33 +327,68 @@ def test_scroll_screen_escape_resolves_none() -> None:
# --- reorder -----------------------------------------------------------------
def test_reorder_space_grabs_enter_commits() -> None:
"""Space grabs/drops a row (spacebar arrives as printable text) and Enter is OK."""
def test_reorder_apply_row_commits_new_order() -> None:
"""Space grabs/drops (spacebar arrives as printable text); Enter on Apply commits."""
screen = ReorderScreen("order", ["a", "b", "c"])
screen.handle("text", " ") # grab "a"
assert screen._grabbed and "Space drop" in screen.footer_hint
screen.handle("down") # carry it past "b"
screen.handle("space") # drop (the normalized action form also works)
assert not screen._grabbed and "Space grab" in screen.footer_hint
screen.handle("down") # cursor from position 1 past "c"…
screen.handle("down") # …onto the Apply row
assert _run(screen, "enter") == [1, 0, 2]
def test_reorder_enter_commits_a_still_grabbed_row_in_place() -> None:
"""OK while a row is grabbed commits it where it currently sits."""
def test_reorder_enter_grabs_and_drops_a_list_row() -> None:
"""Enter on a list row grabs/drops just like Space, so either key drives a move."""
screen = ReorderScreen("order", ["a", "b", "c"])
screen.handle("down") # cursor to "b"
screen.handle("text", " ") # grab
screen.handle("enter") # grab
assert screen._grabbed
screen.handle("down") # carry to the end
assert _run(screen, "enter") == [0, 2, 1]
screen.handle("enter") # drop
assert not screen._grabbed
assert screen._order == [0, 2, 1]
def test_reorder_escape_cancels_discarding_moves() -> None:
"""Esc resolves the sentinel even after rearranging, so the caller keeps the old order."""
def test_reorder_actions_follow_the_dirty_state() -> None:
"""Untouched order offers a lone Back; a change swaps in Apply plus discard-Back."""
screen = ReorderScreen("order", ["a", "b", "c"])
assert [key for key, _ in screen._actions()] == ["back"]
screen.handle("space")
screen.handle("down") # dirty now
assert [key for key, _ in screen._actions()] == ["apply", "back"]
screen.handle("up") # moved back home — clean again
assert [key for key, _ in screen._actions()] == ["back"]
def test_reorder_back_row_and_escape_cancel_discarding_moves() -> None:
"""Enter on Back — like Esc — resolves the sentinel, so the caller keeps the old order."""
screen = ReorderScreen("order", ["a", "b", "c"])
screen.handle("text", " ")
screen.handle("down")
assert _run(screen, "escape") is CANCEL
screen = ReorderScreen("order", ["a", "b", "c"])
screen.handle("space")
screen.handle("down")
screen.handle("space") # drop at position 1; the order is dirty
for _ in range(3): # cursor 1 → 2 → Apply → Back
screen.handle("down")
assert _run(screen, "enter") is CANCEL
def test_reorder_cursor_wraps_through_the_action_rows() -> None:
"""↑ from the first row lands on the last action row; ↓ from there wraps back to the top."""
screen = ReorderScreen("order", ["a", "b"])
screen.handle("up") # wrap: onto the lone Back row
assert screen._index == 2
screen.handle("space") # grabbing means nothing on an action row
assert not screen._grabbed
screen.handle("down") # wrap forward to the first list row
assert screen._index == 0
def test_reorder_ignores_other_typed_characters() -> None:
"""Printable keys other than the spacebar neither grab a row nor resolve the screen."""