^N shows the places of a path's nodes on Message paths
Place mode changes a finished path line's hop labels to each node's place (ui/placemode.py); a hop keeps its key and colour, a named hop with no place reads (Name), an unnamed hop keeps its hash, our ★ stays. The node resolver now also tells which node it named, so a shared one-byte hash finds the same node's place. Message paths is the first screen: ^N, its hint, and a lane chip. Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d541c6d3fd
commit
366fff0d4a
7 files changed
+192
-11
No files matched your search
@@ -130,6 +130,20 @@ def ask_nominatim(base_url: str, lat: float, lon: float) -> object | None:
|
||||
return None
|
||||
|
||||
|
||||
#: The service of the session, for a screen that has no context to reach it through (refer
|
||||
#: to :func:`current`).
|
||||
_CURRENT: PlaceService | None = None
|
||||
|
||||
|
||||
def current() -> PlaceService | None:
|
||||
"""The place service of the session, or ``None`` before a context made one.
|
||||
|
||||
A path screen gets only its display data, not the context. Place mode reads the places
|
||||
through this function, the same as ``preferences.current()``.
|
||||
"""
|
||||
return _CURRENT
|
||||
|
||||
|
||||
class PlaceService:
|
||||
"""The places of the nodes: a read side from memory, and a background job that finds them."""
|
||||
|
||||
@@ -140,6 +154,8 @@ class PlaceService:
|
||||
ctx: The shared application context (the repository, the stores, the
|
||||
preferences, and the tile source).
|
||||
"""
|
||||
global _CURRENT
|
||||
_CURRENT = self
|
||||
self._ctx = ctx
|
||||
self._found: dict[str, tuple[Place, float, float]] | None = None
|
||||
self._revision = 0
|
||||
|
||||
@@ -77,28 +77,40 @@ def make_node_resolver(
|
||||
# paint (for each hop, each row, and each frame). ``entries`` does not change during
|
||||
# the life of this closure. Thus the resolver scans each different label exactly one
|
||||
# time.
|
||||
memo: dict[str, str | None] = {}
|
||||
memo: dict[str, tuple[str | None, str | None]] = {}
|
||||
|
||||
def resolve(label: str | None) -> str | None:
|
||||
if not label:
|
||||
return label
|
||||
def match(label: str) -> tuple[str | None, str | None]:
|
||||
"""The name and the 12-hex node id of the first entry that matches a label."""
|
||||
if label in memo:
|
||||
return memo[label]
|
||||
needle = label.lower().removeprefix("0x")
|
||||
result = label
|
||||
result: tuple[str | None, str | None] = (label, None)
|
||||
for name, pub, prefix in entries:
|
||||
# The hash is a prefix of the key of the node. Match it against the full
|
||||
# public key, or against the stored prefix (which can be shorter) in either
|
||||
# direction.
|
||||
if pub and pub.startswith(needle):
|
||||
result = name
|
||||
result = (name, pub[:12])
|
||||
break
|
||||
if prefix and (prefix.startswith(needle) or needle.startswith(prefix)):
|
||||
result = name
|
||||
result = (name, prefix[:12])
|
||||
break
|
||||
memo[label] = result
|
||||
return result
|
||||
|
||||
def resolve(label: str | None) -> str | None:
|
||||
return label if not label else match(label)[0]
|
||||
|
||||
def node_of(label: str | None) -> str | None:
|
||||
"""The node id of the node that ``resolve`` named for this label, or ``None``.
|
||||
|
||||
Place mode reads it (refer to :mod:`meshterm.ui.placemode`), so the place of a hop
|
||||
is the place of the same node whose name the hop shows, also when two nodes share
|
||||
a one-byte hash.
|
||||
"""
|
||||
return None if not label else match(label)[1]
|
||||
|
||||
resolve.node_of = node_of # type: ignore[attr-defined]
|
||||
return resolve
|
||||
|
||||
|
||||
|
||||
@@ -72,6 +72,8 @@ from .pathline import (
|
||||
hops_atom,
|
||||
path_line,
|
||||
)
|
||||
from .placemode import ACTION as PLACES_ACTION
|
||||
from .placemode import PlaceToggle, node_of, placed, places_of_session
|
||||
from .theme import snr_style
|
||||
from .tui.render import crop_cells, render_lines, render_to_ansi
|
||||
from .tui.screen import ListWindow, Screen
|
||||
@@ -179,6 +181,8 @@ class MessagePathsScreen(Screen):
|
||||
self._arrivals = arrivals
|
||||
self._matched = matched
|
||||
self._resolve = resolve
|
||||
#: ^N: the path lines show the node names, or the places of the nodes.
|
||||
self._places = PlaceToggle()
|
||||
self._prefix_bytes = prefix_bytes
|
||||
self._self_name = self_name
|
||||
self._summary = summary
|
||||
@@ -234,7 +238,7 @@ class MessagePathsScreen(Screen):
|
||||
parts = ["↑↓ move"]
|
||||
if self._list_hidden:
|
||||
parts.append("PgUp/PgDn scroll")
|
||||
parts.extend(("←→ scroll line", "Esc close"))
|
||||
parts.extend(("←→ scroll line", self._places.hint, "Esc close"))
|
||||
return " · ".join(parts)
|
||||
|
||||
@property
|
||||
@@ -246,9 +250,11 @@ class MessagePathsScreen(Screen):
|
||||
exactly when there is more than one arrival to move between, also when the list
|
||||
window hides no arrivals.
|
||||
"""
|
||||
from .tui.fkeys import default_lane
|
||||
from .tui.fkeys import FPair, default_lane
|
||||
|
||||
return default_lane(nav=len(self._arrivals) > 1)
|
||||
lane = list(default_lane(nav=len(self._arrivals) > 1))
|
||||
lane[0] = FPair(self._places.chip, PLACES_ACTION)
|
||||
return tuple(lane)
|
||||
|
||||
def handle(self, action: str, data: str = "") -> None:
|
||||
"""Move the highlight, move the selected line to the side, or close the dialog."""
|
||||
@@ -280,6 +286,8 @@ class MessagePathsScreen(Screen):
|
||||
self._hshift = max(0, self._hshift - _HSTEP)
|
||||
elif action == "right":
|
||||
self._hshift = min(self._hmax, self._hshift + _HSTEP)
|
||||
elif action == PLACES_ACTION:
|
||||
self._places.flip()
|
||||
elif action in ("escape", "enter"):
|
||||
self.resolve(None)
|
||||
|
||||
@@ -440,7 +448,10 @@ class MessagePathsScreen(Screen):
|
||||
self_name=self._self_name,
|
||||
hash_as_name=True,
|
||||
).hops
|
||||
return PathLine([self._origin_hop(), *relays, self._far_hop()]).text()
|
||||
line = PathLine([self._origin_hop(), *relays, self._far_hop()])
|
||||
if self._places.on:
|
||||
line = placed(line, places_of_session(), node_of(self._resolve))
|
||||
return line.text()
|
||||
|
||||
def _far_hop(self) -> PathHop:
|
||||
"""The node at the end of the path: the addressee of the message, not always our node.
|
||||
|
||||
@@ -649,6 +649,17 @@ class PathLine:
|
||||
|
||||
# --- the three shapes ---------------------------------------------------------
|
||||
|
||||
def with_hops(self, hops: Sequence[PathHop]) -> PathLine:
|
||||
"""The same line, drawn the same way, with other hops (refer to ``ui.placemode``)."""
|
||||
return PathLine(
|
||||
hops,
|
||||
mode=self._mode,
|
||||
separator=self._separator,
|
||||
empty=self._empty,
|
||||
from_origin=self._from_origin,
|
||||
to_destination=self._to_destination,
|
||||
)
|
||||
|
||||
def text(self) -> Text:
|
||||
"""The full one-line rendering. For overflow, the caller decides for each surface.
|
||||
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
"""Place mode: a path line that names each hop by its place, and not by its node name.
|
||||
|
||||
Many node names say nothing about where a node is (``VE2XYZ``, ``RAK-1W``). On a path
|
||||
screen, ^N changes the path lines between the node names (the default) and the places of
|
||||
the nodes (refer to :mod:`meshterm.core.places`). Each screen keeps its own choice, and
|
||||
opens with node names.
|
||||
|
||||
The change is applied in one place, to a finished :class:`~meshterm.ui.pathline.PathLine`
|
||||
(:func:`placed`). Thus no builder of a path line has to know about places. A hop keeps its
|
||||
key, so it keeps its colour: only its label changes. A chip shows the fine part of the place
|
||||
(``Villeray``). A hop with a name but no known place shows its name in parentheses
|
||||
(``(RAK-1W)``), so that the user can see that it is not a place. A hop with no name keeps
|
||||
its hash, and our own node keeps its ``★``.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable
|
||||
from dataclasses import replace
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from .pathline import PathHop, PathLine
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from ..services.places import PlaceService
|
||||
|
||||
#: The action of the ^N chord (refer to ``session._CTRL_LETTER_CHORDS``).
|
||||
ACTION = "places"
|
||||
|
||||
#: Gives the 12-hex node id for the hash of a hop, or ``None``.
|
||||
NodeOf = Callable[[str], "str | None"]
|
||||
|
||||
|
||||
def places_of_session() -> PlaceService | None:
|
||||
"""The places of the session (``ctx.places``), for a screen that has no context."""
|
||||
from ..services.places import current
|
||||
|
||||
return current()
|
||||
|
||||
|
||||
def node_of(resolve: object) -> NodeOf | None:
|
||||
"""The node-id half of a resolver that :func:`make_node_resolver` built, or ``None``."""
|
||||
return getattr(resolve, "node_of", None)
|
||||
|
||||
|
||||
class PlaceToggle:
|
||||
"""The ^N choice of one path screen: node names (the default) or places."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
"""Start with node names."""
|
||||
self.on = False
|
||||
|
||||
def flip(self) -> None:
|
||||
"""Change between node names and places."""
|
||||
self.on = not self.on
|
||||
|
||||
@property
|
||||
def hint(self) -> str:
|
||||
"""The hint atom: what ^N does now."""
|
||||
return "^N names" if self.on else "^N places"
|
||||
|
||||
@property
|
||||
def chip(self) -> str:
|
||||
"""The label of the lane chip: what the key does now."""
|
||||
return "Names" if self.on else "Places"
|
||||
|
||||
|
||||
def _hop(hop: PathHop, places: PlaceService, find: NodeOf | None) -> PathHop:
|
||||
"""One hop in place mode."""
|
||||
if hop.you or hop.gap or hop.cursor or not hop.key or not hop.label:
|
||||
return hop
|
||||
key = hop.key.lower().removeprefix("0x")
|
||||
if hop.label.lower().removeprefix("0x") == key:
|
||||
return hop # a hop with no name keeps its hash
|
||||
node = key[:12] if len(key) >= 12 else (find(hop.key) if find is not None else None)
|
||||
place = places.get(node) if node else None
|
||||
label = place.short if place is not None and place.short else f"({hop.label})"
|
||||
return replace(hop, label=label)
|
||||
|
||||
|
||||
def placed_hops(
|
||||
hops: list[PathHop], places: PlaceService | None, find: NodeOf | None = None
|
||||
) -> list[PathHop]:
|
||||
"""The hops with each node name changed to its place (refer to the module docstring)."""
|
||||
if places is None:
|
||||
return hops
|
||||
return [_hop(hop, places, find) for hop in hops]
|
||||
|
||||
|
||||
def placed(line: PathLine, places: PlaceService | None, find: NodeOf | None = None) -> PathLine:
|
||||
"""The same path line, with each node name changed to its place.
|
||||
|
||||
Args:
|
||||
line: The finished path line.
|
||||
places: The places of the nodes (``ctx.places``), or ``None`` to keep the line.
|
||||
find: Gives the node id for a short hash key (:func:`node_of` of the resolver that
|
||||
named the hops). A hop whose key is a full key prefix needs no help.
|
||||
"""
|
||||
if places is None:
|
||||
return line
|
||||
return line.with_hops(placed_hops(line.hops, places, find))
|
||||
@@ -110,6 +110,10 @@ _CTRL_LETTER_CHORDS: dict[str, str] = {
|
||||
# has no binding for it.
|
||||
"k": "lock",
|
||||
"l": "locate",
|
||||
# ^N changes the path lines of a path screen between the node names and the places of
|
||||
# the nodes (refer to meshterm.ui.placemode). N for names. No terminal or multiplexer
|
||||
# takes it in raw mode.
|
||||
"n": "places",
|
||||
# ^O opens the node page of the author of the selected message. The raw mode of
|
||||
# prompt_toolkit clears IEXTEN, so a terminal on macOS or BSD does not take ^O to
|
||||
# discard output.
|
||||
|
||||
@@ -340,3 +340,28 @@ def test_no_answer_makes_the_node_wait(tmp_path: Path, monkeypatch: pytest.Monke
|
||||
_run(service.step())
|
||||
assert len(calls) == 1 # the second step does not ask again at once
|
||||
assert service.get("aaaaaaaaaaaa") is None
|
||||
|
||||
|
||||
# -- place mode ---------------------------------------------------------------
|
||||
|
||||
|
||||
def test_place_mode_changes_names_to_places(tmp_path: Path) -> None:
|
||||
from meshterm.services.trace_runner import make_node_resolver
|
||||
from meshterm.ui.pathline import PathHop, path_line
|
||||
from meshterm.ui.placemode import PlaceToggle, node_of, placed
|
||||
|
||||
service, repo = _service(tmp_path)
|
||||
repo.save_node_place("3daaaaaaaaaa", 45.0, -73.0, Place("Villeray", "Montréal", "tiles"))
|
||||
resolve = make_node_resolver(None, {"3daaaaaaaaaa": "VE2XYZ", "a1bbbbbbbbbb": "RAK-1W"})
|
||||
line = path_line([None, "3d", "a1", "ff"], resolve)
|
||||
hops = placed(line, service, node_of(resolve)).hops
|
||||
assert [h.label for h in hops[1:]] == ["Villeray", "(RAK-1W)", "ff"]
|
||||
assert hops[1].key == "3d" # the hop keeps its key, so it keeps its colour
|
||||
assert hops[0] == line.hops[0] # our own end does not change
|
||||
assert placed(line, None).hops == line.hops
|
||||
lone = PathHop("Bob", key="3daaaaaaaaaa") # a full key prefix needs no resolver
|
||||
assert placed(line.with_hops([lone]), service).hops[0].label == "Villeray"
|
||||
toggle = PlaceToggle()
|
||||
assert (toggle.hint, toggle.chip) == ("^N places", "Places")
|
||||
toggle.flip()
|
||||
assert (toggle.hint, toggle.chip) == ("^N names", "Names")
|
||||
Reference in new issue
Block a user