diff --git a/meshterm/ui/tui/frame.py b/meshterm/ui/tui/frame.py index ebc6ae8..241731a 100644 --- a/meshterm/ui/tui/frame.py +++ b/meshterm/ui/tui/frame.py @@ -449,6 +449,41 @@ def _banner_lines(banner: Sequence[str], cols: int) -> list[str]: return _center(padded, cols) +#: The wordmark as the splash last drew it: ``(banner, cols)`` and what that came to. One +#: slot, because there is one splash and one terminal size at a time. +_BANNER_CACHE: tuple[tuple, tuple[list[str], int]] | None = None + + +def _fitted_banner(banner: Sequence[str], cols: int) -> tuple[list[str], int]: + """The wordmark drawn at ``cols`` — fitted, folded and centred — and the art's width. + + Fitting the mark is the dearest thing the splash draws: a mark wider than the terminal + sends :func:`~meshterm.ui.logo.load_logo` back to the disk for every size until one + fits, each one parsed and measured, and then every row is decoded again to centre it. + None of that moves from one paint to the next, and the splash paints on every keystroke + and every idle tick. Done fresh each time it was nine-tenths of a splash frame at + 53×26, which on the PicoCalc put a visible wait behind every ↑↓ on the device list. + + Args: + banner: The wordmark rows the screen names (usually the full-size mark). + cols: Terminal width. + + Returns: + The centred rows to draw — shared with the memo, so never mutate them — and the + display width of the mark actually chosen, for hanging the footnote off its edge. + """ + global _BANNER_CACHE + key = (tuple(banner), cols) + if _BANNER_CACHE is not None and _BANNER_CACHE[0] == key: + return _BANNER_CACHE[1] + raw = list(banner) + if raw and logo_width(raw) > cols: + raw = load_logo(cols) + fitted = (_banner_lines(raw, cols), logo_width(raw)) + _BANNER_CACHE = (key, fitted) + return fitted + + def fit_hint(hint: str, width: int, *, shed_first: Sequence[str] = ()) -> str: """Drop ``·`` atoms from a footer hint, right to left, until it fits ``width``. @@ -508,16 +543,11 @@ def compose_startup(screen: Screen, cols: int, rows: int) -> str: """ # A screen names its wordmark; the width to draw it at is only known here, so this is # where the size is chosen — a narrow terminal gets a narrow mark rather than a torn one. - raw_banner = screen.banner or [] - if raw_banner and logo_width(raw_banner) > cols: - raw_banner = load_logo(cols) - banner = _banner_lines(raw_banner, cols) + # ``logo_w`` is the chosen mark's own width, so the footnote below can hang off its right + # edge (the wordmark is centered as one block, so every row shares one left margin). + banner, logo_w = _fitted_banner(screen.banner or (), cols) banner_h = len(banner) gap = 1 if banner else 0 # the blank line under the banner - - # The logo's own left margin and width, so the footnote below can hang off its right edge - # (the wordmark is centered as one block, so every row shares this margin). - logo_w = logo_width(raw_banner) logo_right = max(0, (cols - logo_w) // 2) + logo_w # Size the box to its widest real row (probe at a generous width, then measure), never @@ -851,7 +881,8 @@ def _bind(platform: Platform) -> None: Registered at module bottom so the immediate first run (see :func:`~meshterm.platforms.on_platform`) finds every cache already defined. """ - global _BASE_BOX_CACHE + global _BASE_BOX_CACHE, _BANNER_CACHE _BASE_BOX_CACHE = None + _BANNER_CACHE = None _DIALOG_CACHE.clear() _OVERLAY_CACHE.clear() diff --git a/meshterm/ui/widgets.py b/meshterm/ui/widgets.py index e143eea..a69a4df 100644 --- a/meshterm/ui/widgets.py +++ b/meshterm/ui/widgets.py @@ -1187,16 +1187,23 @@ def _sort_header(label: str, column: str, sort: ContactsSort) -> str: return f"[cursor]{label} {triangle}[/]" +#: The legend's word for each node type: the type's one name (:data:`NODE_TYPE_LABELS`), +#: except where a shorter word is unmistakable beside its glyph — ``room`` for a room +#: server (JP, 2026-10-02). The name everywhere else, the CLI's included, stays whole. +_LEGEND_WORDS = {**NODE_TYPE_LABELS, NODE_TYPE_ROOM: "room"} + + def node_type_legend(indent: str = "", width: int | None = None) -> Text: """The key to the node-type marks: ``★ you ▲ repeater ● companion …``. Every glyph in its shared map colour (see :data:`NODE_GLYPHS`), each named muted after - it. THE legend for any surface that draws typed node markers — the contacts list under its - table, the route graph under its lanes — so one glyph means one thing app-wide. + it (:data:`_LEGEND_WORDS`). THE legend for any surface that draws typed node markers — + the contacts list under its table, the route graph under its lanes — so one glyph means + one thing app-wide. - One line where it fits. Where it does not — the full names run to 59 cells, and the - PicoCalc has 53 — it breaks onto a second line between entries, never inside one, so - a mark is never parted from its name and no name is cropped. + One line where it fits: 52 cells, inside the handhelds' 53. Where it does not — an + indent, a narrower box — it breaks onto a second line between entries, never inside + one, so a mark is never parted from its name and no name is cropped. Args: indent: Leading spaces to sit the legend under a table or graph body. @@ -1205,7 +1212,7 @@ def node_type_legend(indent: str = "", width: int | None = None) -> Text: entries = [Text.assemble((SELF_MARK[0], SELF_MARK[1]), (" you", "muted"))] for node_type in (NODE_TYPE_REPEATER, NODE_TYPE_CHAT, NODE_TYPE_ROOM, NODE_TYPE_SENSOR): glyph, color = NODE_GLYPHS[node_type] - entries.append(Text.assemble((glyph, color), (f" {NODE_TYPE_LABELS[node_type]}", "muted"))) + entries.append(Text.assemble((glyph, color), (f" {_LEGEND_WORDS[node_type]}", "muted"))) gap = " " legend = Text(indent) diff --git a/tests/test_records_screen.py b/tests/test_records_screen.py index 86636c0..996af9c 100644 --- a/tests/test_records_screen.py +++ b/tests/test_records_screen.py @@ -169,19 +169,24 @@ def test_record_dialog_shows_the_node_type_legend() -> None: assert "★ you" in body and "▲ repeater" in body and "◉ sensor" in body -def test_record_dialog_legend_breaks_between_entries_on_a_picocalc_width() -> None: - """At 53 cells the legend takes two lines, and no mark is parted from its name. +def test_record_dialog_legend_is_one_line_on_a_handheld_and_breaks_between_entries() -> None: + """At 53 cells the whole key fits one line; narrower, no mark is parted from its name. - The full names run to 59 cells on one line, so cropping would cut "sensor" off the - PicoCalc; breaking inside an entry would leave a glyph at the end of one line and its - name at the start of the next. + ``room`` rather than ``room server`` brings the key to 52 cells, inside the PicoCalc's + and the Cardputer's 53. Where it still has to break, it breaks between entries: inside + one would leave a glyph at the end of a line and its name at the start of the next. """ screen = _dialog(_record()) screen.handle("tab") # the Route tab - body = _plain(screen.render_body(53)) - for entry in ("★ you", "▲ repeater", "● companion", "■ room server", "◉ sensor"): - assert entry in body - assert all(cell_len(line) <= 53 for line in body.splitlines()) + entries = ("★ you", "▲ repeater", "● companion", "■ room", "◉ sensor") + body = _plain(screen.render_body(53)).splitlines() + assert any(all(entry in line for entry in entries) for line in body) + assert not any("room server" in line for line in body) + + narrow = _plain(screen.render_body(40)) + for entry in entries: + assert entry in narrow + assert all(cell_len(line) <= 40 for line in narrow.splitlines()) def test_record_graph_marks_a_repeater_relay_with_its_triangle() -> None: