From 1d62691599f5689a308f97b387345c3069e39172 Mon Sep 17 00:00:00 2001 From: Jean-Pierre Martineau Date: Fri, 10 Jul 2026 03:02:30 -0400 Subject: [PATCH] Reorder screen gains OK/Cancel: Space grabs, Enter commits, Esc aborts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Esc used to be the only way out of the channel-reorder screen and it always committed, so there was no way to back out of an accidental shuffle. The screen now follows the other dialogs' Enter-accepts/ Esc-cancels contract: Space grabs and drops the highlighted row (arriving as printable text from the session, with the normalized "space" action accepted for symmetry), Enter resolves the final order — dropping a still-carried row where it sits — and Esc resolves CANCEL, which the session already maps back to the identity order so the caller treats it as "no change". Footer hints re-phrase per state. Co-Authored-By: Claude Fable 5 --- meshterm/ui/tui/select.py | 24 ++++++++++++++-------- meshterm/ui/tui/session.py | 6 +++++- tests/test_tui.py | 42 +++++++++++++++++++++++++++++++++++++- 3 files changed, 61 insertions(+), 11 deletions(-) diff --git a/meshterm/ui/tui/select.py b/meshterm/ui/tui/select.py index 4e93ce3..f6a6810 100644 --- a/meshterm/ui/tui/select.py +++ b/meshterm/ui/tui/select.py @@ -292,10 +292,12 @@ class SelectScreen(Screen): class ReorderScreen(Screen): """A list whose rows the user rearranges in place with the arrow keys. - Move the cursor with ↑/↓; press Enter to *grab* the highlighted row, then ↑/↓ carry it - up and down the list; press Enter again to *drop* it. The screen stays open until Esc, - at which point it resolves 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"). + 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. """ def __init__(self, title: str, labels: list[str]) -> None: @@ -317,8 +319,8 @@ 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 · Enter drop · Esc done" - return "↑↓ choose · Enter grab · Esc done" + return "↑↓ move row · Space drop · Enter OK · Esc cancel" + return "↑↓ choose · Space grab · Enter OK · Esc cancel" def render_body(self, width: int) -> list[str]: """Render each row, marking the cursor (and, when grabbed, the moving row).""" @@ -343,7 +345,7 @@ 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, or finish on Esc.""" + """Move the cursor, carry a grabbed row, toggle grab, commit on Enter, or cancel on Esc.""" n = len(self._order) if action == "up": if self._grabbed and self._index > 0: @@ -359,10 +361,14 @@ class ReorderScreen(Screen): self._index += 1 elif not self._grabbed and n: self._index = (self._index + 1) % n - elif action == "enter": + 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 - elif action == "escape": + elif action == "enter": self.resolve(list(self._order)) + elif action == "escape": + super().handle("escape") def _cursor_line(rows: list, selected: Optional[Choice], filtered: bool) -> Optional[int]: diff --git a/meshterm/ui/tui/session.py b/meshterm/ui/tui/session.py index d84138c..ecf4289 100644 --- a/meshterm/ui/tui/session.py +++ b/meshterm/ui/tui/session.py @@ -394,7 +394,11 @@ class TuiSession: return None if result is CANCEL else result 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.""" + """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". + """ result = await self.run_screen(ReorderScreen(title, labels)) return list(range(len(labels))) if result is CANCEL else result diff --git a/tests/test_tui.py b/tests/test_tui.py index b9296a2..01c1d4f 100644 --- a/tests/test_tui.py +++ b/tests/test_tui.py @@ -28,7 +28,7 @@ from meshterm.ui.tui.prompt import ( ) from meshterm.ui.tui.render import render_lines, render_to_ansi from meshterm.ui.tui.screen import CANCEL, Screen, ScrollScreen -from meshterm.ui.tui.select import Choice, SelectScreen, Separator +from meshterm.ui.tui.select import Choice, ReorderScreen, SelectScreen, Separator from meshterm.ui.tui.session import TuiSession _UNSET = object() @@ -324,6 +324,46 @@ def test_scroll_screen_escape_resolves_none() -> None: assert _run(ScrollScreen(Text("x")), "enter") is None +# --- reorder ----------------------------------------------------------------- + + +def test_reorder_space_grabs_enter_commits() -> None: + """Space grabs/drops a row (spacebar arrives as printable text) and Enter is OK.""" + 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 + 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.""" + screen = ReorderScreen("order", ["a", "b", "c"]) + screen.handle("down") # cursor to "b" + screen.handle("text", " ") # grab + screen.handle("down") # carry to the end + assert _run(screen, "enter") == [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.""" + screen = ReorderScreen("order", ["a", "b", "c"]) + screen.handle("text", " ") + screen.handle("down") + assert _run(screen, "escape") is CANCEL + + +def test_reorder_ignores_other_typed_characters() -> None: + """Printable keys other than the spacebar neither grab a row nor resolve the screen.""" + screen = ReorderScreen("order", ["a", "b"]) + screen.future = _Fut() + screen.handle("text", "x") + assert not screen._grabbed + assert not screen.future.done() + + # --- frame -------------------------------------------------------------------