From 8330c5a5dbc152dd871d6b89b9fe08cbd6b07c12 Mon Sep 17 00:00:00 2001 From: Jean-Pierre Martineau Date: Sun, 12 Jul 2026 15:41:55 -0400 Subject: [PATCH] path_text grows the trace flavour: one widget for every path MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Hash annotations (Alice (3d63)), None hops for our own device at a route's endpoints, and dim_from to fade a boomerang's mirrored return leg — so the trace screens' walked/planned routes, the per-hop link tables, and the CLI trace output all render through THE path widget. Trace routes thereby join the app-wide colour rules (per-name hues, pure-white you) instead of their old flat brand/accent, and the live route line wraps under a hanging indent instead of folding to column 0. Co-Authored-By: Claude Fable 5 --- meshterm/ui/trace_screen.py | 90 +++++++---------- meshterm/ui/widgets.py | 191 +++++++++++++++++++----------------- tests/test_widgets.py | 22 +++++ 3 files changed, 158 insertions(+), 145 deletions(-) diff --git a/meshterm/ui/trace_screen.py b/meshterm/ui/trace_screen.py index 1544045..8fce36b 100644 --- a/meshterm/ui/trace_screen.py +++ b/meshterm/ui/trace_screen.py @@ -74,10 +74,10 @@ from ..services.topology import render_forced_spec from .braillechart import meter from .menus import back_rows from .theme import snr_style -from .tui.render import render_lines, render_to_ansi +from .tui.render import render_hanging, render_lines, render_to_ansi from .tui.screen import Screen from .tui.spinner import Spinner -from .widgets import NodeResolver, _link_text, _route_text, highlighted_hash +from .widgets import NodeResolver, _link_text, _route_text, highlighted_hash, path_text if TYPE_CHECKING: from ..context import AppContext @@ -523,9 +523,12 @@ class TraceScreen(Screen): """ stats = TraceStats.from_traces(self._target, self._traces) current = next((t for t in reversed(self._traces) if t.success), None) - lines = render_lines( - Group(self._route_line(current), Text(), self._summary(stats, current)), width + # The route hangs under its own value block when it wraps (the app-wide + # labelled-row rule), so a long walk never folds back to column zero. + lines = render_hanging( + Text("route ", style="muted"), self._route_line(current), width, indent=7 ) + lines.extend(render_lines(Group(Text(), self._summary(stats, current)), width)) lines.append("") self._cursor: Optional[int] = None for i, key in enumerate(self._actions): @@ -595,22 +598,19 @@ class TraceScreen(Screen): return text def _route_line(self, current: Optional[TraceResult]) -> Text: - """The route: live when a reply has landed, else planned, else the stored one.""" - line = Text("route ", style="muted") + """The route: live when a reply has landed, else planned, else the stored one. + + The label lane ("route ") is added by the caller, which renders this body + with a hanging indent so wrapped routes align under themselves. + """ if current is not None: - line.append_text( - _route_text(current, self._device_label, self._resolve, self._device_hash) - ) - return line + return _route_text(current, self._device_label, self._resolve, self._device_hash) planned = self._planned_route() if planned is not None: - line.append_text(planned) - return line + return planned if self._previous is not None: - line.append_text( - _route_text( - self._previous, self._device_label, self._resolve, self._device_hash - ) + line = _route_text( + self._previous, self._device_label, self._resolve, self._device_hash ) stamp = self._previous.timestamp.astimezone().strftime("%b %d %H:%M") # On its own line so a long route never squeezes the stamp off the @@ -618,10 +618,8 @@ class TraceScreen(Screen): line.append(f"\n(previous · {stamp})", style="faint") return line if self._mode == "path": - line.append("none — compose a path to walk", style="muted") - else: - line.append("unknown — press Enter to trace", style="muted") - return line + return Text("none — compose a path to walk", style="muted") + return Text("unknown — press Enter to trace", style="muted") def _effective_spec(self) -> tuple[str, bool]: """The wire spec the next Trace walks, and whether auto resolution supplied it. @@ -640,14 +638,15 @@ class TraceScreen(Screen): def _planned_route(self) -> Optional[Text]: """The route the next Trace walks as a preview, or ``None`` without one. - Renders the literal wire spec — the whole walk, since the trace protocol - has no separate return-path field. In target mode the spec is the symmetric - boomerang (outbound hops, the target, those hops mirrored): its second half - is dimmed, reading as "this part isn't yours to compose". A path walk's spec - is the whole route (hand-composed, or the last stored walk), so every hop - renders in full colour and only the automatic landing back on us stays - faint. An auto-resolved spec carries a faint provenance line naming where - the route came from. + Renders the literal wire spec through the shared path widget — the whole + walk, since the trace protocol has no separate return-path field. In target + mode the spec is the symmetric boomerang (outbound hops, the target, those + hops mirrored): its second half is dimmed via the widget's ``dim_from``, + reading as "this part isn't yours to compose". A path walk's spec is the + whole route (hand-composed, or the last stored walk), so every hop renders + in full colour and only the automatic landing back on us stays faint. An + auto-resolved spec carries a faint provenance line naming where the route + came from. """ spec, auto = self._effective_spec() tokens = [h.strip() for h in spec.split(",") if h.strip()] @@ -658,39 +657,20 @@ class TraceScreen(Screen): outbound, return_leg = tokens[: mid + 1], tokens[mid + 1 :] else: outbound, return_leg = tokens, [] - text = Text(self._device_label, style="accent") - for hop in outbound: - text.append(" → ", style="muted") - text.append_text(self._planned_hop_text(hop, dim=False)) - for hop in return_leg: - text.append(" → ", style="faint") - text.append_text(self._planned_hop_text(hop, dim=True)) - text.append(" → ", style="faint") - text.append(self._device_label, style="faint") + text = path_text( + [None, *outbound, *return_leg, None], + self._resolve, + prefix_bytes=8, # spec tokens are the addressed slices: light them whole + self_name=self._device_label, + show_hash=True, + dim_from=1 + len(outbound), + ) if auto and self._auto_source: # On its own line (like the previous-trace stamp) so a long route # never squeezes the provenance off the right edge. text.append(f"\n(auto · {self._auto_source})", style="faint") return text - def _planned_hop_text(self, hop: str, *, dim: bool) -> Text: - """One planned-path node: resolved name (with hash) or bare hash. - - Args: - hop: The hop's raw hex key prefix. - dim: Whether to render in the return leg's faint style rather than the - outbound leg's normal brand/muted styling. - """ - style = "faint" if dim else "brand" - text = Text() - named = self._resolve(hop) - if named and named != hop: - text.append(named, style=style) - text.append(f" ({hop})", style="faint" if dim else "muted") - else: - text.append(hop, style=style) - return text - def _displayed_hop_count(self, current: Optional[TraceResult]) -> Optional[int]: """How many nodes the displayed route passes through, endpoints excluded. diff --git a/meshterm/ui/widgets.py b/meshterm/ui/widgets.py index adf3e01..ca44a80 100644 --- a/meshterm/ui/widgets.py +++ b/meshterm/ui/widgets.py @@ -38,7 +38,7 @@ from ..core.models import ( utcnow, ) from .map_render import _NODE, _REPEATER, _SELF -from .theme import snr_style +from .theme import name_style, snr_style if TYPE_CHECKING: from ..core.discovery import DiscoveredDevice @@ -132,12 +132,12 @@ def _link_text( hash_bytes: Optional[int] = None, device_hash: Optional[str] = None, ) -> Text: - """Render an ``origin -> destination`` link as ``name (hash)`` nodes, arrow styled. + """Render an ``origin -> destination`` link through THE path widget. - Each end is rendered like the route line: a known node as ``name (hash)``, an - unknown one as its bare hash, and our own device as its label plus ``device_hash`` - when supplied. Hashes are truncated to ``hash_bytes`` so they match how the trace - command addressed each node. + A two-node :func:`path_text` in the trace presentation: a known node as + ``name (hash)``, an unknown one as its bare hash, our own device as its label + (plus ``device_hash`` when supplied). Hashes are truncated to ``hash_bytes`` so + they match how the trace command addressed each node. Args: origin: The transmitting node (``None`` = our device). @@ -150,12 +150,14 @@ def _link_text( Returns: A :class:`Text` like ``us (a1) → Alice (3d)`` with the arrow muted. """ - text = _route_node_text(origin, device_label, resolve, hash_bytes, device_hash) - text.append(" → ", style="muted") - text.append_text( - _route_node_text(destination, device_label, resolve, hash_bytes, device_hash) + ends = [ + None if not node or node == device_label else node + for node in (origin, destination) + ] + return path_text( + ends, resolve, prefix_bytes=hash_bytes or 8, self_name=device_label, + show_hash=True, hash_bytes=hash_bytes, device_hash=device_hash, ) - return text def traces_table( @@ -234,53 +236,108 @@ def traces_table( def path_text( - hops: Sequence[str], + hops: Sequence[Optional[str]], resolve: NodeResolver = _identity, *, prefix_bytes: int = 0, self_name: Optional[str] = None, empty: str = "direct", + show_hash: bool = False, + hash_bytes: Optional[int] = None, + device_hash: Optional[str] = None, + dim_from: Optional[int] = None, ) -> Text: - """Render a relay path compactly on one line — THE path widget. + """Render a hop sequence compactly on one line — THE path widget. - The one way MeshTerm shows a walked/relayed hop sequence wherever a single line has - to carry it (a packet's ``via`` row, a message's delivery paths, a feed note): each - hop as its resolved name — coloured in the app-wide per-name hue, our own node pure - white — or, unnamed, as its bare hash through :func:`highlighted_hash` (the prefix - lit, the rest muted; colour stays the "this is a name" signal). Hops are joined by - muted ``→`` arrows and no hash is repeated after a name, so the line stays terse - enough to survive a 72-column row. An empty path reads as ``empty``. + The one way MeshTerm shows a walked, relayed, or planned hop sequence, wherever one + appears: a packet's ``via`` row, a message's delivery paths, a feed note, a trace's + walked route or planned spec. Each hop renders as its resolved name — coloured in + the app-wide per-name hue, our own node pure white — or, unnamed, as its hash + through :func:`highlighted_hash` (the addressed prefix lit, the rest muted; colour + stays the "this is a name" signal). Hops are joined by muted ``→`` arrows and, by + default, no hash is repeated after a name, so the compact form survives a 72-column + row. The trace-flavoured options: ``show_hash`` annotates each named hop with the + hash it is addressed by (``Alice (3d63)``), a ``None`` hop is our own device at a + route's endpoints, and ``dim_from`` fades the tail a caller wants read as automatic + (a boomerang's mirrored return leg). An empty path reads as ``empty``. Args: - hops: The hop hashes in propagation order (already split; empties skipped). + hops: The hops in propagation order — hex hashes, with ``None`` marking our + own device (empty strings are skipped; ``dim_from`` counts rendered hops). resolve: Maps a hop hash to a friendly name when known. prefix_bytes: Path-hash width to light in unnamed hops' hashes (0 = none). - self_name: Our own node's name, drawn in the white ``you`` style when it - appears along the path. + self_name: Our own node's name — drawn in the white ``you`` style when a + resolved name matches it, and naming any ``None`` device hop. empty: The muted text shown when there are no hops (e.g. ``"direct"``). + show_hash: Annotate named hops (and, with ``device_hash``, our device) with + their hash in parentheses — the trace presentation. + hash_bytes: Truncate shown/annotated hashes to this byte width (the width the + hops were addressed at); ``None`` shows them whole. + device_hash: Our own device's key, annotated onto ``None`` hops when + ``show_hash`` is on. + dim_from: Render hops at/after this index — and the arrows into them — faint + (a planned route's return leg); ``None`` dims nothing. Returns: - A one-line :class:`Text`; callers ellipsize with ``no_wrap`` when space is - tighter than the path. + A one-line :class:`Text`. Space tighter than the path is the caller's call, + per surface: ellipsize (``no_wrap``), wrap under a hanging indent, or crop + with horizontal scrolling. """ - from .theme import name_style - - shown = [h for h in hops if h] + shown = [h for h in hops if h is None or h] if not shown: return Text(empty, style="muted") text = Text() for i, hop in enumerate(shown): + dim = dim_from is not None and i >= dim_from if i: - text.append(" → ", style="muted") - named = resolve(hop) - if named and named != hop: - style = "you" if self_name and named == self_name else name_style(named) - text.append(named, style=style) - else: - text.append_text(highlighted_hash(hop, prefix_bytes)) + text.append(" → ", style="faint" if dim else "muted") + text.append_text( + _path_node( + hop, resolve, prefix_bytes=prefix_bytes, self_name=self_name, + show_hash=show_hash, hash_bytes=hash_bytes, device_hash=device_hash, + dim=dim, + ) + ) return text +def _path_node( + hop: Optional[str], + resolve: NodeResolver, + *, + prefix_bytes: int, + self_name: Optional[str], + show_hash: bool, + hash_bytes: Optional[int], + device_hash: Optional[str], + dim: bool, +) -> Text: + """One node of :func:`path_text` (see there for the rendering rules).""" + note_style = "faint" if dim else "muted" + if hop is None: + text = Text(self_name or LOCAL_DEVICE_LABEL, style="faint" if dim else "you") + annotated = _shorten_hash(device_hash, hash_bytes) if device_hash else "" + if show_hash and annotated: + text.append(f" ({annotated})", style=note_style) + return text + named = resolve(hop) + if named and named != hop: + if dim: + style = "faint" + elif self_name and named == self_name: + style = "you" + else: + style = name_style(named) + text = Text(named, style=style) + if show_hash: + text.append(f" ({_shorten_hash(hop, hash_bytes)})", style=note_style) + return text + shown = _shorten_hash(hop, hash_bytes) + if dim: + return Text(shown, style="faint") + return highlighted_hash(shown, prefix_bytes) + + def highlighted_hash(value: str, prefix_bytes: int, width: Optional[int] = None) -> Text: """Render a hex key with its leading path-hash prefix highlighted — THE hash widget. @@ -333,61 +390,18 @@ def _shorten_hash(value: str, hash_bytes: Optional[int]) -> str: return raw[: hash_bytes * 2] if hash_bytes else raw -def _route_node_text( - label: Optional[str], - device_label: str, - resolve: NodeResolver, - hash_bytes: Optional[int], - device_hash: Optional[str] = None, -) -> Text: - """Render one route node as ``name (hash)``, the hash at the command's width. - - Our own device (``label`` is ``None`` or equal to ``device_label``) carries no - hash. A known node shows its friendly name followed by its key-prefix hash; an - unknown node shows just the hash. The hash is truncated to ``hash_bytes`` — the - per-hop width the trace command used — so it matches how the node was addressed. - - Args: - label: The node's raw hop hash, or ``None``/``device_label`` for our device. - device_label: The label used for our own device. - resolve: Maps a raw hop hash to a friendly contact name when known. - hash_bytes: Path-hash width (bytes) to truncate the shown hash to. - device_hash: Our own device's key/hash, shown alongside its label when known. - - Returns: - A styled :class:`Text` for the node, joined with plain spaces throughout — - never non-breaking ones, which prompt_toolkit renders as underscores. - """ - if not label or label == device_label: - text = Text(device_label, style="accent") - shown = _shorten_hash(device_hash, hash_bytes) if device_hash else "" - if shown: - text.append(" (", style="muted") - text.append(shown, style="muted") - text.append(")", style="muted") - return text - shown = _shorten_hash(label, hash_bytes) - named = resolve(label) - if not named or named == label: - return Text(shown, style="brand") # unknown node: hash only - text = Text(named, style="brand") - text.append(" (", style="muted") - text.append(shown, style="muted") - text.append(")", style="muted") - return text - - def _route_text( result: TraceResult, device_label: str = LOCAL_DEVICE_LABEL, resolve: NodeResolver = _identity, device_hash: Optional[str] = None, ) -> Text: - """Render a trace's route as a sequence of ``name (hash)`` nodes. + """Render a trace's walked route through THE path widget. Shows the path the trace actually walked — the forced path, or the route the - device resolved when auto-routing — with each node annotated by its hash at the - command's path-hash width, joined by muted arrows, e.g. + device resolved when auto-routing — as a :func:`path_text` in the trace + presentation: each node annotated by its hash at the command's path-hash width, + our own device bracketing both ends, e.g. ``Me (a1b2) → Alice (3d63) → Bob (f2a1) → Me (a1b2)``. Args: @@ -404,14 +418,11 @@ def _route_text( return Text("no hops recorded", style="muted") hash_bytes = result.path_hash_bytes nodes = [edges[0].origin] + [edge.destination for edge in edges] - text = Text() - for i, node in enumerate(nodes): - if i: - text.append(" → ", style="muted") - text.append_text( - _route_node_text(node, device_label, resolve, hash_bytes, device_hash) - ) - return text + hops = [None if not node or node == device_label else node for node in nodes] + return path_text( + hops, resolve, prefix_bytes=hash_bytes or 8, self_name=device_label, + show_hash=True, hash_bytes=hash_bytes, device_hash=device_hash, + ) def _hop_medians_table( diff --git a/tests/test_widgets.py b/tests/test_widgets.py index 01fde19..80f3753 100644 --- a/tests/test_widgets.py +++ b/tests/test_widgets.py @@ -58,3 +58,25 @@ def test_path_text_lights_the_hash_prefix() -> None: text = path_text(["77bb"], _resolve, prefix_bytes=1) brand = [text.plain[s.start : s.end] for s in text.spans if "brand" in str(s.style)] assert "77" in brand # the addressed prefix stands out within the hash + + +def test_path_text_trace_flavour_annotates_hashes_and_brackets_us() -> None: + """show_hash appends the addressed hash to names; None hops are our device.""" + text = path_text( + [None, "aa", "77bb", None], _resolve, self_name="Homestead", + show_hash=True, hash_bytes=1, device_hash="c0ffee", + ) + assert text.plain == "Homestead (c0) → Alice (aa) → 77 → Homestead (c0)" + + +def test_path_text_dims_the_tail_from_dim_from() -> None: + """The mirrored return leg (and the arrows into it) render faint.""" + text = path_text( + [None, "aa", "3d", "aa", None], _resolve, self_name="us", + show_hash=True, dim_from=3, + ) + styles = [(text.plain[s.start : s.end], str(s.style)) for s in text.spans] + assert ("us", "you") in styles # the departure keeps the white you + assert ("us", "faint") in styles # the landing back on us is faint + assert any(run.startswith("Alice") and style == "faint" for run, style in styles) + assert any(run.startswith("YUL") and style != "faint" for run, style in styles)