diff --git a/meshterm/ui/node_detail_screen.py b/meshterm/ui/node_detail_screen.py index ae32774..c44f27a 100644 --- a/meshterm/ui/node_detail_screen.py +++ b/meshterm/ui/node_detail_screen.py @@ -458,8 +458,16 @@ class NodeDetailScreen(Screen): if not rv.routes or rv.glyph_of is None: return render_lines(Text(rv.note or "no route observed yet", style="muted"), width) sel = self._route_sel if 0 <= self._route_sel < len(rv.routes) else 0 + # Priority is fixed by evidence order (route 0, the best-evidence route, is always the + # spine) so the fan's geometry never moves as the selection changes — only emphasis + # (which route is drawn white and on top) and the label muting below follow the pick. layers = [ - PathLayer(hops=route.draw, color=_WHITE if i == sel else _GREY, priority=3 if i == sel else 2) + PathLayer( + hops=route.draw, + color=_WHITE if i == sel else _GREY, + priority=len(rv.routes) - i, + emphasis=1 if i == sel else 0, + ) for i, route in enumerate(rv.routes) ] # A node keeps its name hue only while it sits on the selected route (its endpoints diff --git a/meshterm/ui/pathgraph.py b/meshterm/ui/pathgraph.py index 3ccf660..7ce9d6b 100644 --- a/meshterm/ui/pathgraph.py +++ b/meshterm/ui/pathgraph.py @@ -15,9 +15,12 @@ now draws the fan as a **multilane highway**: a node always sits on a level plat lane, and a route changes lane only *between* nodes, easing across on a single gentle shift the way a car drifts one lane over and then stays there. Concretely: -* **lanes** — the highest-priority (drawn last, e.g. the selected or best-evidence) path holds - the flow's spine, its relays in a straight run, and the alternatives fan above and below it a - fixed pitch of text rows apart. Rather than give every route a full-width lane of its own — +* **lanes** — the highest-*priority* path (the spine, e.g. the best-evidence route) holds the + flow's centre, its relays in a straight run, and the alternatives fan above and below it a + fixed pitch of text rows apart. Priority fixes the geometry; a separate *emphasis* rank picks + which route is drawn highlighted (on top, winning any shared cell) without moving a marker, so + a caller can light a different route without the picture reflowing. Rather than give every + route a full-width lane of its own — which stacks the band as tall as the route count even where the routes barely overlap — the lanes are *packed by column*: the spine keeps its own relays, and each other node slides to the innermost free row above or below the spine *in its own column*, so a column with one off-spine @@ -130,10 +133,18 @@ _ORDER_SWEEPS = 8 #: keep the side the jog order handed them. The widget's real inputs sit far under this. _MAX_BALANCE_ROUTES = 16 -#: The glyph used for the flow arrow embedded in the trunk just before us, +#: The glyph used for the flow arrow embedded in the trunk just before us, #: so the whole flow reads better as a directed run node → us (not a map you wander). -#_ARROW_GLYPH = "▶" -_ARROW_GLYPH = "" +#_ARROW_GLYPH = "▶" +_ARROW_GLYPH = "" + +#: How far a layer's emphasis outranks its layout priority when edges compete for a cell. +#: Emphasis is the draw-time highlight — the selected path drawn on top and winning any shared +#: cell — and must beat any spread of priority values, so it is scaled well past the small +#: priorities the widget ever sees. Layout geometry ignores emphasis entirely; only the colour +#: and z-order of the drawn edges follow it (see :func:`_draw_rank`), so re-emphasising a +#: different path repaints the same picture in new colours rather than relaying it. +_EMPHASIS_BOOST = 1_000_000 #: A node's graph marker: the glyph and its ``#rrggbb`` colour (the shared node-glyph @@ -155,14 +166,22 @@ class PathLayer: hops: The relay node ids between the endpoints, in walk order (empty = the path runs endpoint to endpoint straight across). color: The edge colour the path draws in. - priority: Draw priority — where paths share a cell, the highest priority - keeps it (its edges are also drawn last), and the highest-priority path - takes the straight centre lane. + priority: **Layout** rank — which path is the spine. The highest-priority path takes + the straight centre lane and owns any relay it shares (weaker paths jog to meet it), + so it fixes every node's column and lane. Geometry depends on this alone, never on + :attr:`emphasis`: a caller that wants the drawn picture to hold still while it + re-highlights keeps each path's priority constant. + emphasis: **Draw** rank — the highlight, layered over the fixed geometry. Where edges + share a cell (or cross), the higher-emphasis path wins the colour and draws on top; + it moves no marker. Defaults to ``0`` (all paths equal, so the colour falls to + :attr:`priority` as before). A caller that highlights by selection varies *this*, + not priority, so the layout stays put and only the colours change. """ hops: tuple[str, ...] color: RGB priority: int + emphasis: int = 0 def _mid_row(y_dot: float) -> int: @@ -190,7 +209,10 @@ def _collapse(layers: Sequence[PathLayer]) -> list[PathLayer]: by_hops[layer.hops] = len(drawn) drawn.append(layer) elif layer.priority > drawn[at].priority: - drawn[at] = PathLayer(layer.hops, layer.color, layer.priority) + drawn[at] = PathLayer( + layer.hops, layer.color, layer.priority, + max(layer.emphasis, drawn[at].emphasis), + ) return drawn @@ -233,7 +255,10 @@ def _coalesce_prefixes(layers: Sequence[PathLayer]) -> list[PathLayer]: return hop return [ - PathLayer(tuple(resolved(hop) for hop in layer.hops), layer.color, layer.priority) + PathLayer( + tuple(resolved(hop) for hop in layer.hops), + layer.color, layer.priority, layer.emphasis, + ) for layer in layers ] @@ -267,6 +292,17 @@ def _is_hex(value: str) -> bool: return bool(value) and all(char in _HEX_DIGITS for char in value) +def _draw_rank(layer: PathLayer) -> int: + """A layer's edge draw rank: emphasis dominates, layout priority breaks ties. + + Governs only which colour wins a shared cell and which edge draws on top — never node + placement (that is :attr:`PathLayer.priority` alone). So a caller can re-emphasise a + different path (bump its :attr:`~PathLayer.emphasis`) and the graph repaints in new colours + over the very same layout, rather than reflowing because the spine changed. + """ + return layer.priority + layer.emphasis * _EMPHASIS_BOOST + + def render_path_graph( layers: Sequence[PathLayer], width: int, @@ -281,10 +317,11 @@ def render_path_graph( """Draw the diverge/converge route-flow graph and return its ANSI lines. Args: - layers: The paths to draw, in draw order (later layers, and higher priorities, - win shared cells; the highest priority takes the straight centre lane). Layers - with identical hop sequences collapse to one drawn path owned by the highest - priority among them. + layers: The paths to draw. The highest ``priority`` takes the straight centre lane and + fixes the geometry; a separate ``emphasis`` (default ``0``) decides which path is + drawn highlighted — on top, winning any shared cell — without moving a marker, so + re-emphasising a path repaints the same layout in new colours. Layers with identical + hop sequences collapse to one drawn path owned by the highest priority among them. width: Canvas width in character cells. glyph_of: Marker glyph + colour per node id (endpoints keyed by :data:`SRC_NODE` / :data:`DST_NODE`). @@ -414,20 +451,25 @@ def render_path_graph( # -- Edges. Collect every edge once, keyed by its unordered node pair: an edge two routes # share — or a pair walked in *both* directions — must draw a single time, else it silts up # as a doubled line a dot off itself (two routes' Bresenham runs never land on the exact - # same dots). Each pair keeps the colour and priority of the strongest route through it; a - # two-way pair is flagged so it draws as the one honest vertical rather than a lane change. + # same dots). Each pair keeps the colour and draw rank of the strongest route through it — + # by draw rank, so the *emphasised* (highlighted) route wins a shared edge over a merely + # higher-priority spine, and the highlight paints the whole selected route rather than + # dropping out where it overlaps another. A two-way pair is flagged so it draws as the one + # honest vertical rather than a lane change. bidir = {frozenset((u, v)) for (u, v) in edges if (v, u) in edges} edge_style: dict[frozenset[str], tuple[int, RGB]] = {} - for layer, seq in sorted(zip(drawn, seqs), key=lambda ls: ls[0].priority): + for layer, seq in sorted(zip(drawn, seqs), key=lambda ls: _draw_rank(ls[0])): + rank = _draw_rank(layer) for u, v in zip(seq, seq[1:]): key = frozenset((u, v)) prev = edge_style.get(key) - if prev is None or layer.priority > prev[0]: - edge_style[key] = (layer.priority, layer.color) - # Draw ascending by priority so the strongest route's colour wins any cell two edges share. - for key, (priority, color) in sorted(edge_style.items(), key=lambda kv: kv[1][0]): + if prev is None or rank > prev[0]: + edge_style[key] = (rank, layer.color) + # Draw ascending by draw rank so the strongest/most-emphasised route's colour wins any cell + # two edges share and sits on top. + for key, (rank, color) in sorted(edge_style.items(), key=lambda kv: kv[1][0]): u, v = tuple(key) - canvas.draw_line(_route(u, v, pos, key in bidir), color, priority) + canvas.draw_line(_route(u, v, pos, key in bidir), color, rank) # -- An arrow embedded in the trunk just before us, so the whole flow reads as a directed # run node → us (not a map you wander). A single glyph in the spine's own colour: it reserves diff --git a/tests/test_pathgraph.py b/tests/test_pathgraph.py index a024f04..163a91b 100644 --- a/tests/test_pathgraph.py +++ b/tests/test_pathgraph.py @@ -69,6 +69,41 @@ def test_identical_paths_collapse_to_the_top_layer() -> None: assert _sgr(YELLOW) not in joined +def test_emphasis_moves_the_highlight_not_the_layout() -> None: + """Emphasis re-colours the drawn fan on top of a fixed geometry: the same layout, painted + in different colours, so a caller can light a different route without it reflowing.""" + # A two-route fan whose spine (priority) is pinned to route 0. Only emphasis differs + # between the two renders — were emphasis to drive geometry (as raising priority would), + # route 1 becoming the highlight would seize the centre lane and shuffle the markers. + def fan(emph: int): + return [ + PathLayer(("aa",), WHITE if emph == 0 else GREY, 2, emphasis=1 if emph == 0 else 0), + PathLayer(("bb",), WHITE if emph == 1 else GREY, 1, emphasis=1 if emph == 1 else 0), + ] + + lit_spine = _render(fan(0)) + lit_alt = _render(fan(1)) + # Geometry is identical — strip the colour and the two frames are the same picture. + assert _ANSI.sub("", "\n".join(lit_spine)) == _ANSI.sub("", "\n".join(lit_alt)) + # But the colouring moved: each frame still lights one route white, and the frames differ. + assert _sgr(WHITE) in "\n".join(lit_spine) and _sgr(WHITE) in "\n".join(lit_alt) + assert lit_spine != lit_alt + + +def test_emphasis_wins_a_shared_edge_over_a_higher_priority_spine() -> None: + """A highlighted alternative paints its whole run — even the edge it shares with the spine — + rather than dropping out where the higher-priority spine would otherwise own the colour.""" + # Both routes leave SRC through the same first relay (aa), so they share the SRC–aa edge; + # the spine (priority 2, grey) would win that shared edge by priority, but the emphasised + # alternative (priority 1, white) must win it by draw rank so its highlight stays unbroken. + layers = [ + PathLayer(("aa", "bb"), GREY, 2), + PathLayer(("aa", "cc"), WHITE, 1, emphasis=1), + ] + joined = "\n".join(_render(layers)) + assert _sgr(WHITE) in joined # the emphasised route drew, shared edge and all + + def test_distinct_layers_draw_in_their_own_colours() -> None: """Three different routes keep three edge colours on one canvas.""" layers = [