The map's find filter also matches a node's place
MapMarker carries a place, open_map reads it for each marker, and both the screen and the renderer match label or place through MapMarker.matches. Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7dd7cb5a62
commit
538b0a5a69
4 files changed
+94
-3
No files matched your search
@@ -411,6 +411,9 @@ class MapMarker:
|
||||
key: The key of the node in hex (the longest form that the caller has). The hue
|
||||
of the label comes from this key. ``None`` leaves the label in the muted grey
|
||||
for a node with no key.
|
||||
place: The full label of the place of the node (``Villeray, Montréal``), or ``""``
|
||||
when the caller has none. The map does not draw it. The find filter of the map
|
||||
matches it, in the same way as it matches the label.
|
||||
"""
|
||||
|
||||
label: str
|
||||
@@ -420,6 +423,16 @@ class MapMarker:
|
||||
is_self: bool = False
|
||||
detail: str = ""
|
||||
key: str | None = None
|
||||
place: str = ""
|
||||
|
||||
def matches(self, needle: str) -> bool:
|
||||
"""Whether the find filter ``needle`` matches the label or the place of this node.
|
||||
|
||||
Args:
|
||||
needle: The text that the user typed, already case-folded. An empty ``needle``
|
||||
matches each node.
|
||||
"""
|
||||
return not needle or needle in self.label.casefold() or needle in self.place.casefold()
|
||||
|
||||
def _rank(self) -> int:
|
||||
"""Draw order: our node above the repeaters, and the repeaters above leaf nodes."""
|
||||
|
||||
@@ -347,7 +347,7 @@ def render_map(
|
||||
markers: The mesh nodes to put on top.
|
||||
max_labels: The maximum number of basemap labels to place, to keep the map
|
||||
readable.
|
||||
find: A live node-name filter. Markers whose label contains it (case-insensitive)
|
||||
find: A live node filter. Markers whose label or place contains it (case-insensitive)
|
||||
draw with bright white labels, and the other markers dim to context with no
|
||||
label. When it is empty, each node draws normally.
|
||||
ghost: Ground from an earlier raster to put down first, reprojected onto this
|
||||
@@ -796,7 +796,7 @@ def _draw_nodes(
|
||||
ix, iy = int(round(x)), int(round(y))
|
||||
if not (0 <= ix < viewport.dot_w and 0 <= iy < viewport.dot_h):
|
||||
continue
|
||||
matched = not needle or needle in marker.label.casefold()
|
||||
matched = marker.matches(needle)
|
||||
placed.append((marker, ix, iy, matched))
|
||||
|
||||
# Matches draw after (over) the dimmed nodes that do not match, at any rank. Thus the
|
||||
|
||||
@@ -33,6 +33,7 @@ a blank grid. The map still works, but it has no streets.
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import dataclasses
|
||||
import math
|
||||
from collections import OrderedDict
|
||||
from collections.abc import Callable
|
||||
@@ -51,6 +52,7 @@ from .tui.screen import Screen
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from ..context import AppContext
|
||||
from ..core.places import Place
|
||||
|
||||
#: The fraction of the viewport that one (coarse) pan key press moves.
|
||||
_PAN_STEP = 0.30
|
||||
@@ -704,7 +706,7 @@ class MapScreen(Screen):
|
||||
needle = self._filter.strip().casefold()
|
||||
if not needle:
|
||||
return self._markers
|
||||
return [m for m in self._markers if needle in m.label.casefold()]
|
||||
return [m for m in self._markers if m.matches(needle)]
|
||||
|
||||
def _title(self, vp: Viewport) -> str:
|
||||
"""A compact status title: the zoom, the node count (or the find matches), and the scale.
|
||||
@@ -1462,6 +1464,26 @@ def basemap_source(ctx: AppContext) -> BasemapSource:
|
||||
return ctx.basemap_source
|
||||
|
||||
|
||||
def with_places(
|
||||
markers: list[MapMarker], place_of: Callable[[str | None], Place | None]
|
||||
) -> list[MapMarker]:
|
||||
"""Return copies of ``markers`` that carry the place of each node (the find filter reads it).
|
||||
|
||||
Args:
|
||||
markers: The located nodes. The function does not change them.
|
||||
place_of: Gives the place of a key or a key prefix, or ``None`` (for example,
|
||||
:meth:`~meshterm.services.places.PlaceService.get`).
|
||||
|
||||
Returns:
|
||||
The new markers, in the same order. A node with no place has an empty ``place``.
|
||||
"""
|
||||
out = []
|
||||
for marker in markers:
|
||||
place = place_of(marker.key) if marker.key else None
|
||||
out.append(dataclasses.replace(marker, place=(place.label if place else "")))
|
||||
return out
|
||||
|
||||
|
||||
async def open_map(
|
||||
ctx: AppContext,
|
||||
markers: list[MapMarker],
|
||||
@@ -1499,6 +1521,9 @@ async def open_map(
|
||||
if not isinstance(ctx.ui, TuiUi): # pragma: no cover - guarded by the menu-only caller
|
||||
raise RuntimeError("the interactive map is only available in the menu")
|
||||
session = ctx.ui.session
|
||||
# The find filter also matches the place of a node. The places are read now, so that a
|
||||
# place that the finder stored since the caller made the markers is in the filter.
|
||||
markers = with_places(markers, ctx.places.get)
|
||||
source = basemap_source(ctx)
|
||||
# Resolve the tile template and zoom in a worker thread, so the UI thread never blocks.
|
||||
max_zoom = await asyncio.to_thread(lambda: source.max_zoom)
|
||||
|
||||
@@ -300,3 +300,56 @@ def test_plain_face_has_a_place_column_before_key() -> None:
|
||||
assert "Villeray, Montréal" in alice
|
||||
assert alice.index("Villeray") < alice.index(_ALICE)
|
||||
assert carl.split()[-2] == "-" and carl.split()[-1] == _CARL
|
||||
|
||||
|
||||
# -- part 4: the map filter -----------------------------------------------------------
|
||||
|
||||
|
||||
def test_marker_matches_its_label_or_its_place() -> None:
|
||||
"""The find filter of the map matches the label or the place, in any case."""
|
||||
from meshterm.core.models import MapMarker
|
||||
|
||||
marker = MapMarker("Alice", 45.5, -73.6, place="Villeray, Montréal")
|
||||
assert marker.matches("")
|
||||
assert marker.matches("ali")
|
||||
assert marker.matches("villeray")
|
||||
assert marker.matches("montréal")
|
||||
assert not marker.matches("verdun")
|
||||
assert not MapMarker("Alice", 45.5, -73.6).matches("villeray")
|
||||
|
||||
|
||||
def test_with_places_stamps_a_copy_of_each_marker() -> None:
|
||||
"""``with_places`` reads the place of each key, and does not change the given markers."""
|
||||
from meshterm.core.models import MapMarker
|
||||
from meshterm.ui.map_screen import with_places
|
||||
|
||||
original = [
|
||||
MapMarker("Alice", 45.5, -73.6, key=_ALICE),
|
||||
MapMarker("Carl", 45.6, -73.7, key=_CARL),
|
||||
MapMarker("Nokey", 45.7, -73.8),
|
||||
]
|
||||
stamped = with_places(original, _places().get)
|
||||
assert [m.place for m in stamped] == ["Villeray, Montréal", "", ""]
|
||||
assert [m.place for m in original] == ["", "", ""]
|
||||
assert [m.label for m in stamped] == ["Alice", "Carl", "Nokey"]
|
||||
|
||||
|
||||
def test_map_find_matches_a_place() -> None:
|
||||
"""Typed text narrows the map to the nodes whose place matches it."""
|
||||
from meshterm.core.models import MapMarker
|
||||
from meshterm.ui.map_screen import MapScreen, with_places
|
||||
from tests.test_map import _StubSession, _StubSource
|
||||
|
||||
markers = with_places(
|
||||
[
|
||||
MapMarker("Alice", 45.5, -73.6, key=_ALICE),
|
||||
MapMarker("Bob", 45.6, -73.7, key=_BOB),
|
||||
MapMarker("Carl", 45.7, -73.8, key=_CARL),
|
||||
],
|
||||
_places().get,
|
||||
)
|
||||
screen = MapScreen(_StubSession(80, 24), markers, _StubSource(), 14)
|
||||
screen.render_body(80) # the first paint makes the viewport that a key needs
|
||||
for char in "villeray":
|
||||
screen.handle("text", char)
|
||||
assert [m.label for m in screen._matches()] == ["Alice"] # noqa: SLF001
|
||||
Reference in new issue
Block a user