Navigate the TUI like a text editor
Make sticky headers a reusable Screen feature and lift paging and section jumps onto the base Screen, so every scrollable surface shares one implementation: the frame records each render's body height and viewport, and the shared helpers page by a screenful, clamp to the content, and walk between the boundaries a screen records in _sticky_headers (a select list's group dividers, the chat transcript's day dividers). Remap keys to match a good editor: - PageUp/PageDown scroll by a screenful rather than a fixed step. - Ctrl+Home/End reach the top/bottom (the live tail in chat). - Ctrl+PageUp/PageDown jump to the current/previous and next section. - In chat, Home/End and Ctrl+Left/Right act on the compose line; word motion (start of current word, else previous/next) is added to the shared line editor, so every text prompt gains it too. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
8906e4ed53
commit
bdbe5cce30
8 files changed
+506
-103
No files matched your search
@@ -789,6 +789,117 @@ def test_channel_transcript_groups_by_sender() -> None:
|
||||
assert stamps[0] != stamps[1] # grouped Alice messages show distinct times
|
||||
|
||||
|
||||
def _two_day_messages():
|
||||
"""Two days of direct messages, long enough to overflow a small viewport."""
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
base = datetime(2026, 7, 5, 9, 0, tzinfo=timezone.utc)
|
||||
day1 = [ChatMessage(text=f"day1-{i}", peer="d4e5f6a7", created_at=base + timedelta(minutes=i))
|
||||
for i in range(4)]
|
||||
day2 = [ChatMessage(text=f"day2-{i}", peer="d4e5f6a7",
|
||||
created_at=base + timedelta(days=1, minutes=i)) for i in range(4)]
|
||||
return day1 + day2
|
||||
|
||||
|
||||
def test_chat_sticky_header_pins_the_governing_day_divider() -> None:
|
||||
"""The day divider above the top row pins there once it scrolls off (like the picker)."""
|
||||
screen = _screen(_StubSession(), send=None, messages=_two_day_messages())
|
||||
screen.render_body(60)
|
||||
(idx0, div0), (idx1, div1) = screen._sticky_headers
|
||||
assert screen.sticky_header(0) is None # first divider is itself the top row
|
||||
assert screen.sticky_header(idx1 - 1) == div0 # still within day one — its divider pins
|
||||
assert screen.sticky_header(idx1) is None # day two's divider is now the top row
|
||||
assert screen.sticky_header(idx1 + 1) == div1 # scrolled past it — day two's pins
|
||||
|
||||
|
||||
def test_chat_frame_pins_a_day_divider_when_stuck_to_the_newest() -> None:
|
||||
"""Rendered through the frame at the tail, a day divider occupies the pinned top row."""
|
||||
from meshterm.ui.tui import frame
|
||||
|
||||
screen = _screen(_StubSession(), send=None, messages=_two_day_messages())
|
||||
lines = screen.render_body(60) # sticks to the newest message, scrolling early days off
|
||||
visible, above, _below = frame._visible_slice(screen, lines, 6)
|
||||
top = _strip_ansi(visible[0]).strip()
|
||||
assert top.startswith("──") and "Jul" in top # a day divider is pinned to the top row
|
||||
assert above is True # and the frame flags there's more above the pin
|
||||
|
||||
|
||||
def test_chat_home_end_and_word_keys_move_the_compose_cursor() -> None:
|
||||
"""In a chat, Home/End and Ctrl+←/→ act on the compose line, not the transcript scroll."""
|
||||
screen = _screen(_StubSession(), send=None, messages=_two_day_messages())
|
||||
for ch in "hello world":
|
||||
screen.handle("text", ch)
|
||||
assert screen._editor.cursor == 11
|
||||
screen.handle("home")
|
||||
assert screen._editor.cursor == 0 # line start, not scroll-to-top
|
||||
screen.handle("end")
|
||||
assert screen._editor.cursor == 11
|
||||
screen.handle("ctrl_left")
|
||||
assert screen._editor.cursor == 6 # start of "world"
|
||||
|
||||
|
||||
def test_chat_scroll_keys_detach_and_reattach_to_the_tail() -> None:
|
||||
"""PageUp/Ctrl+Home detach from the live tail; Ctrl+End snaps back to it."""
|
||||
screen = _screen(_StubSession(), send=None, messages=_two_day_messages())
|
||||
lines = screen.render_body(60)
|
||||
screen.note_metrics(total=len(lines), viewport=5)
|
||||
screen.handle("pageup")
|
||||
assert screen._stick is False # a screenful up detaches from the tail
|
||||
screen.handle("ctrl_home")
|
||||
assert screen.scroll == 0 and screen._stick is False
|
||||
screen.handle("ctrl_end")
|
||||
assert screen._stick is True # re-attached to the newest message
|
||||
|
||||
|
||||
def test_chat_ctrl_page_scrolls_between_day_dividers() -> None:
|
||||
"""Ctrl+PageDown/PageUp move the transcript scroll between day dividers."""
|
||||
screen = _screen(_StubSession(), send=None, messages=_two_day_messages())
|
||||
lines = screen.render_body(60)
|
||||
screen.note_metrics(total=len(lines), viewport=5)
|
||||
(idx0, _), (idx1, _) = screen._sticky_headers
|
||||
screen.scroll = 0
|
||||
screen.handle("ctrl_pagedown")
|
||||
assert screen.scroll == idx1 and screen._stick is False # to the second day's divider
|
||||
screen.handle("ctrl_pageup")
|
||||
assert screen.scroll == idx0 # back to the first day's divider
|
||||
|
||||
|
||||
def _two_day_channel_messages():
|
||||
"""Channel messages spanning two local days (one sender), for section-jump tests."""
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
base = datetime(2026, 7, 5, 9, 0, tzinfo=timezone.utc)
|
||||
day1 = [ChatMessage(text=f"Alice: d1-{i}", is_channel=True, channel_idx=0,
|
||||
created_at=base + timedelta(minutes=i)) for i in range(3)]
|
||||
day2 = [ChatMessage(text=f"Alice: d2-{i}", is_channel=True, channel_idx=0,
|
||||
created_at=base + timedelta(days=1, minutes=i)) for i in range(3)]
|
||||
return day1 + day2
|
||||
|
||||
|
||||
def test_chat_channel_ctrl_page_selects_across_days() -> None:
|
||||
"""In a channel, Ctrl+PageDown/PageUp move the reply selection to day boundaries."""
|
||||
screen = _channel_screen(_two_day_channel_messages())
|
||||
starts = screen._day_start_indices()
|
||||
assert starts == [0, 3]
|
||||
screen.handle("ctrl_home")
|
||||
assert screen._selected == 0
|
||||
screen.handle("ctrl_pagedown")
|
||||
assert screen._selected == starts[1] # first message of the second day
|
||||
screen.handle("ctrl_pageup")
|
||||
assert screen._selected == starts[0] # back to the first day
|
||||
|
||||
|
||||
def test_chat_channel_home_moves_the_compose_cursor() -> None:
|
||||
"""A channel's Home key edits the compose line rather than jumping the selection."""
|
||||
screen = _channel_screen(_two_day_channel_messages())
|
||||
for ch in "reply":
|
||||
screen.handle("text", ch)
|
||||
assert screen._editor.cursor == 5
|
||||
screen.handle("home")
|
||||
assert screen._editor.cursor == 0
|
||||
assert screen._selected is None # touching the compose line clears any reply selection
|
||||
|
||||
|
||||
def test_channel_self_style_keyed_on_concept_not_label() -> None:
|
||||
"""Our white 'self' style follows the message being outbound, not the 'you' label.
|
||||
|
||||
|
||||
+103
-7
@@ -19,7 +19,7 @@ from meshterm.ui.tui import frame
|
||||
from meshterm.ui.tui.progress import ProgressScreen
|
||||
from meshterm.ui.tui.prompt import AutocompleteScreen, ConfirmScreen, TextScreen
|
||||
from meshterm.ui.tui.render import render_lines, render_to_ansi
|
||||
from meshterm.ui.tui.screen import CANCEL, ScrollScreen
|
||||
from meshterm.ui.tui.screen import CANCEL, Screen, ScrollScreen
|
||||
from meshterm.ui.tui.select import Choice, SelectScreen, Separator
|
||||
from meshterm.ui.tui.session import TuiSession
|
||||
|
||||
@@ -153,17 +153,17 @@ def test_select_no_wrap_clamps_at_the_ends() -> None:
|
||||
assert _run(screen, "enter") == 3
|
||||
|
||||
|
||||
def test_select_pageup_pagedown_jump_by_a_page() -> None:
|
||||
"""PageDown/PageUp move the highlight a page at a time, clamped to the choice range."""
|
||||
from meshterm.ui.tui.select import _PAGE
|
||||
|
||||
def test_select_pageup_pagedown_jump_by_a_screenful() -> None:
|
||||
"""PageDown/PageUp move the highlight a screenful at a time, clamped to the choice range."""
|
||||
items = [Choice(f"c{i}", i) for i in range(30)]
|
||||
screen = SelectScreen("pick", items) # starts on the first choice
|
||||
screen.note_metrics(total=30, viewport=11) # a screenful is viewport - 1 = 10 rows
|
||||
screen.handle("pagedown")
|
||||
assert _run(screen, "enter") == _PAGE # advanced one page down
|
||||
assert _run(screen, "enter") == 10 # advanced one page (viewport - 1) down
|
||||
screen = SelectScreen("pick", items, default=25)
|
||||
screen.note_metrics(total=30, viewport=11)
|
||||
screen.handle("pageup")
|
||||
assert _run(screen, "enter") == 25 - _PAGE # and one page back up
|
||||
assert _run(screen, "enter") == 25 - 10 # and one page back up
|
||||
|
||||
|
||||
def _grouped_menu(default: object = None) -> SelectScreen:
|
||||
@@ -211,9 +211,87 @@ def test_select_pinned_heading_keeps_the_last_row_reachable() -> None:
|
||||
assert below is False # and we know we're at the bottom
|
||||
|
||||
|
||||
def test_screen_sticky_header_picks_the_governing_recorded_header() -> None:
|
||||
"""The base Screen.sticky_header logic is generic over any recorded headers list.
|
||||
|
||||
Both the select list and the chat transcript reuse it by populating ``_sticky_headers``;
|
||||
this exercises the shared rule directly: pin the last header at or above the offset, unless
|
||||
it *is* the top row or none sits above it.
|
||||
"""
|
||||
screen = Screen()
|
||||
screen._sticky_headers = [(0, "A"), (5, "B"), (12, "C")]
|
||||
assert screen.sticky_header(0) is None # header A is itself the top row
|
||||
assert screen.sticky_header(3) == "A" # scrolled past A, before B → A governs
|
||||
assert screen.sticky_header(5) is None # header B is now the top row
|
||||
assert screen.sticky_header(20) == "C" # below every header → the last one pins
|
||||
assert Screen().sticky_header(9) is None # no recorded headers → nothing to pin
|
||||
|
||||
|
||||
def test_select_ctrl_page_jumps_between_sections() -> None:
|
||||
"""Ctrl+PageDown lands on the next section's first choice; Ctrl+PageUp walks back up."""
|
||||
screen = _grouped_menu() # Channels (6) then Direct (8), highlight on the first choice
|
||||
screen.handle("ctrl_pagedown")
|
||||
assert _run(screen, "enter") == ("d", 0) # jumped to the first Direct choice
|
||||
screen.handle("ctrl_pageup")
|
||||
assert _run(screen, "enter") == ("c", 0) # already atop Direct → back to Channels' first
|
||||
screen.handle("ctrl_pagedown")
|
||||
assert _run(screen, "enter") == ("d", 0) # and forward to Direct again
|
||||
|
||||
|
||||
# --- scroll ------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_screen_scroll_helpers_page_and_clamp_to_metrics() -> None:
|
||||
"""The shared scroll helpers page by a screenful and clamp to the recorded body/viewport."""
|
||||
screen = Screen()
|
||||
screen.note_metrics(total=100, viewport=10)
|
||||
screen.scroll_pages(1)
|
||||
assert screen.scroll == 9 # a page is viewport - 1
|
||||
screen.scroll_to_bottom()
|
||||
assert screen.scroll == 90 # total - viewport
|
||||
screen.scroll_lines(50)
|
||||
assert screen.scroll == 90 # clamped, never past the bottom
|
||||
screen.scroll_to_top()
|
||||
assert screen.scroll == 0
|
||||
|
||||
|
||||
def test_screen_section_scroll_walks_recorded_headers() -> None:
|
||||
"""Ctrl+PageUp/PageDown move the scroll offset between recorded section boundaries."""
|
||||
screen = Screen()
|
||||
screen.note_metrics(total=100, viewport=10)
|
||||
screen._sticky_headers = [(0, "A"), (20, "B"), (60, "C")]
|
||||
screen.scroll_to_next_section()
|
||||
assert screen.scroll == 20 # from the top → start of section B
|
||||
screen.scroll_to_next_section()
|
||||
assert screen.scroll == 60 # → start of C
|
||||
screen.scroll_to_next_section()
|
||||
assert screen.scroll == 90 # no section past C → clamp to the bottom
|
||||
screen.scroll = 40 # mid-section B
|
||||
screen.scroll_to_section_start()
|
||||
assert screen.scroll == 20 # up to B's start
|
||||
screen.scroll_to_section_start()
|
||||
assert screen.scroll == 0 # already atop B → previous section (A at the top)
|
||||
|
||||
|
||||
def test_scroll_screen_ctrl_edges_and_sectionless_fallback() -> None:
|
||||
"""Ctrl+Home/End reach the edges; with no sections Ctrl+PageUp/PageDown do too."""
|
||||
body = Text("\n".join(f"line {i}" for i in range(100)))
|
||||
screen = ScrollScreen(body, title="log")
|
||||
screen.render_body(40) # sets total = 100
|
||||
screen.note_viewport(10)
|
||||
screen.handle("ctrl_end")
|
||||
assert screen.scroll == 90
|
||||
screen.handle("ctrl_home")
|
||||
assert screen.scroll == 0
|
||||
screen.handle("ctrl_pagedown") # no sections recorded → falls through to the bottom
|
||||
assert screen.scroll == 90
|
||||
screen.handle("ctrl_pageup")
|
||||
assert screen.scroll == 0
|
||||
|
||||
|
||||
# --- scroll (existing) -------------------------------------------------------
|
||||
|
||||
|
||||
def test_scroll_screen_paging_and_clamp() -> None:
|
||||
"""PageDown advances by a page, End jumps to the bottom, both clamped to content."""
|
||||
body = Text("\n".join(f"line {i}" for i in range(100)))
|
||||
@@ -308,6 +386,24 @@ def test_text_screen_edits_and_validates() -> None:
|
||||
assert _run(screen, "enter") == "ok"
|
||||
|
||||
|
||||
def test_line_editor_word_motion() -> None:
|
||||
"""Ctrl+Left/Right hop by word — to the current word's start, else the previous/next."""
|
||||
from meshterm.ui.tui.prompt import _LineEditor
|
||||
|
||||
editor = _LineEditor("the quick brown fox") # cursor at the end (len 20)
|
||||
editor.edit("ctrl_left")
|
||||
assert editor.cursor == 17 # start of "fox"
|
||||
editor.edit("ctrl_left")
|
||||
assert editor.cursor == 11 # skips the double space → start of "brown"
|
||||
editor.edit("ctrl_left")
|
||||
assert editor.cursor == 4 # start of "quick"
|
||||
editor.edit("ctrl_right")
|
||||
assert editor.cursor == 11 # forward over "quick" and the spaces → start of "brown"
|
||||
editor.cursor = 13 # mid-"brown"
|
||||
editor.edit("ctrl_left")
|
||||
assert editor.cursor == 11 # to the current word's start, not the previous word
|
||||
|
||||
|
||||
def test_text_screen_password_masks() -> None:
|
||||
"""A password field renders bullets, not the typed characters."""
|
||||
screen = TextScreen("pw?", password=True)
|
||||
|
||||
Reference in new issue
Block a user