The archive preview leads each contact with its type icon, as the Contacts list does
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
4ba0a29691
commit
899bb84830
3 files changed
+88
-8
No files matched your search
@@ -74,7 +74,7 @@ from .menus import Lane, column_header, fit_cells, section_heading
|
||||
from .theme import name_style
|
||||
from .tui import Choice, Separator
|
||||
from .tui.screen import CANCEL
|
||||
from .widgets import _recency_style, format_age
|
||||
from .widgets import DEFAULT_GLYPH, NODE_GLYPHS, _recency_style, format_age
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from ..context import AppContext
|
||||
@@ -118,6 +118,10 @@ _GAP = 2
|
||||
#: the PicoCalc's 53 columns.
|
||||
_NAME_W = 22
|
||||
|
||||
#: The type-mark lane ahead of the name: the node's glyph and its space, which is how the
|
||||
#: Contacts list leads every row (see :func:`~meshterm.ui.contactlist._lane`).
|
||||
_MARK_W = 2
|
||||
|
||||
|
||||
def _count_cell(tally: int | None) -> tuple[str, str]:
|
||||
"""A tally lane's value: the number muted, or a faint ``—`` where there is nothing.
|
||||
@@ -212,11 +216,17 @@ def _preview_header(widths: tuple[int, ...], width: int) -> str:
|
||||
last = len(_EVIDENCE) - 1
|
||||
for index, (lane, lane_w) in enumerate(zip(_EVIDENCE, widths, strict=True)):
|
||||
lanes.append(Lane(f"{lane.label:>{lane_w}}", 0 if index == last else lane_w + _GAP))
|
||||
return column_header(lanes, width)
|
||||
# Past the pointer and the type mark, so ``NAME`` sits over the name, not the glyph.
|
||||
return column_header(lanes, width, indent=2 + _MARK_W)
|
||||
|
||||
|
||||
def _victim_row(scored: ScoredContact, widths: tuple[int, ...]) -> Text:
|
||||
"""One preview line: the contact's name in its own hue, then its evidence, lane by lane.
|
||||
"""One preview line: the contact's type mark and name, then its evidence, lane by lane.
|
||||
|
||||
The row leads the way the Contacts list's does — the shared type mark in its own colour
|
||||
(``▲`` repeater, ``■`` room, ``◉`` sensor, ``●`` plain node), then the name — so the
|
||||
contact reads as the same row here as on the list the reader came from, and a repeater
|
||||
about to be archived says so before its name is read.
|
||||
|
||||
The name keeps its key-derived colour like everywhere else in the app — this is a list of
|
||||
nodes, and a reader picking one out of thirty rows should not have to read it letter by
|
||||
@@ -234,7 +244,10 @@ def _victim_row(scored: ScoredContact, widths: tuple[int, ...]) -> Text:
|
||||
"""
|
||||
contact = scored.contact
|
||||
name = contact.name or contact.key_prefix or "?"
|
||||
mark, mark_style = NODE_GLYPHS.get(contact.node_type, DEFAULT_GLYPH)
|
||||
row = Text()
|
||||
row.append(mark, style=mark_style)
|
||||
row.append(" " * (_MARK_W - cell_len(mark)))
|
||||
row.append(
|
||||
fit_cells(name, _NAME_W - _GAP) + " " * _GAP,
|
||||
style=name_style(name, contact.public_key or contact.key_prefix),
|
||||
@@ -507,9 +520,9 @@ def _preview_items(victims: list[ScoredContact]) -> list:
|
||||
"""The preview's rows: a pinned column header, one line per victim, then Apply/Back.
|
||||
|
||||
Each contact row is ``deletable``, so ``Delete`` lifts it out of the sweep, and pins its
|
||||
**name** out of the ``←→`` scroll (``hscroll_from``): the evidence lanes are what a
|
||||
narrow terminal cuts, and sliding the name away to read them would cost the row the one
|
||||
thing that says which contact is being read.
|
||||
type mark and **name** out of the ``←→`` scroll (``hscroll_from``): the evidence lanes
|
||||
are what a narrow terminal cuts, and sliding the name away to read them would cost the
|
||||
row the one thing that says which contact is being read.
|
||||
|
||||
The lanes are measured here, across every victim, which is also why a spared row rebuilds
|
||||
the rows through this function rather than dropping one: the widths it leaves behind may
|
||||
@@ -519,7 +532,12 @@ def _preview_items(victims: list[ScoredContact]) -> list:
|
||||
return [
|
||||
Separator(lambda w: _preview_header(widths, w), pinned=True),
|
||||
*(
|
||||
Choice(title=_victim_row(v, widths), value=v, deletable=True, hscroll_from=_NAME_W)
|
||||
Choice(
|
||||
title=_victim_row(v, widths),
|
||||
value=v,
|
||||
deletable=True,
|
||||
hscroll_from=_MARK_W + _NAME_W,
|
||||
)
|
||||
for v in victims
|
||||
),
|
||||
# The same shape ``exit_rows`` draws, in this flow's own words: Apply has no key of
|
||||
|
||||
@@ -526,6 +526,55 @@ async def test_archive_sweeps_under_a_progress_bar_and_reports_in_a_dialog() ->
|
||||
assert sorted(archived) == ["Old0", "Old1", "Old2"]
|
||||
|
||||
|
||||
def test_a_preview_row_leads_with_the_contacts_type_mark() -> None:
|
||||
"""Each contact the sweep would take leads with its type mark, as the Contacts list's do.
|
||||
|
||||
The mark is a lane of its own ahead of the name, in the type's colour rather than the
|
||||
name's. The header moves over with it — ``NAME`` over the name, every evidence word still
|
||||
over its values — and ``←→`` keeps the mark pinned beside the name it belongs to.
|
||||
"""
|
||||
from meshterm.core.contact_score import ContactSignals, ScoredContact
|
||||
from meshterm.core.models import NODE_TYPE_REPEATER, Contact
|
||||
from meshterm.ui.sweep_screen import (
|
||||
_MARK_W,
|
||||
_NAME_W,
|
||||
_lane_widths,
|
||||
_preview_header,
|
||||
_preview_items,
|
||||
_victim_row,
|
||||
)
|
||||
from meshterm.ui.theme import name_style
|
||||
|
||||
def scored(name: str, key: str, node_type: int | None) -> ScoredContact:
|
||||
return ScoredContact(
|
||||
contact=Contact(name=name, public_key=key, key_prefix=key[:12], node_type=node_type),
|
||||
signals=ContactSignals(node=key[:12], packets=3),
|
||||
score=1.0,
|
||||
percentile=1,
|
||||
)
|
||||
|
||||
victims = [scored("Hilltop", "3d" * 32, NODE_TYPE_REPEATER), scored("Walker", "5e" * 32, None)]
|
||||
widths = _lane_widths(victims)
|
||||
hilltop, walker = (_victim_row(v, widths) for v in victims)
|
||||
assert hilltop.plain.startswith("▲ Hilltop")
|
||||
assert walker.plain.startswith("● Walker") # never advertised a type: the plain node
|
||||
|
||||
def styles_at(row, index: int) -> list[str]: # noqa: ANN001
|
||||
return [str(span.style) for span in row.spans if span.start <= index < span.end]
|
||||
|
||||
assert styles_at(hilltop, 0) == ["type.repeater"]
|
||||
assert styles_at(hilltop, hilltop.plain.index("Hilltop")) == [name_style("Hilltop", "3d" * 32)]
|
||||
|
||||
# The header is drawn from the pointer column's edge and a row from just past it.
|
||||
header = _preview_header(widths, 72)
|
||||
assert header.index("NAME") == 2 + hilltop.plain.index("Hilltop")
|
||||
assert header.index("PKTS") + len("PKTS") - 1 == 2 + hilltop.plain.index("3")
|
||||
|
||||
items = _preview_items(victims)
|
||||
rows = [c for c in items if isinstance(getattr(c, "value", None), ScoredContact)]
|
||||
assert len(rows) == 2 and all(c.hscroll_from == _MARK_W + _NAME_W for c in rows)
|
||||
|
||||
|
||||
async def test_sparing_a_row_shrinks_the_sweep_without_leaving_the_preview() -> None:
|
||||
"""Delete on a preview row lifts that contact out of the sweep, in place.
|
||||
|
||||
|
||||
+14
-1
@@ -40,7 +40,9 @@ from meshterm.core.advert_store import AdvertPolicy
|
||||
from meshterm.core.channels import DEFAULT_PUBLIC_SECRET, derive_secret
|
||||
from meshterm.core.courier_store import CourierStore
|
||||
from meshterm.core.models import (
|
||||
NODE_TYPE_CHAT,
|
||||
NODE_TYPE_REPEATER,
|
||||
NODE_TYPE_SENSOR,
|
||||
ChatMessage,
|
||||
Contact,
|
||||
Conversation,
|
||||
@@ -233,7 +235,18 @@ def _archive_ranked(): # noqa: ANN201
|
||||
("Lakeside", "3d" * 32, dict(heard_age_days=30.0, packets=22, known_days=250.0, hops=1.0)),
|
||||
("sensor-2", "7c" * 32, dict(heard_age_days=210.0, packets=2, known_days=260.0)),
|
||||
]
|
||||
contacts = [Contact(name=n, public_key=k, key_prefix=k[:12]) for n, k, _ in specs]
|
||||
# Typed as their names say, so the preview draws a repeater's ``▲`` and a sensor's ``◉``
|
||||
# beside the plain ``●`` of ``hop-9``, which never advertised a type.
|
||||
types = {
|
||||
"Alice": NODE_TYPE_CHAT,
|
||||
"A Rather Long Repeater Name For Width": NODE_TYPE_REPEATER,
|
||||
"Lakeside": NODE_TYPE_REPEATER,
|
||||
"sensor-2": NODE_TYPE_SENSOR,
|
||||
}
|
||||
contacts = [
|
||||
Contact(name=n, public_key=k, key_prefix=k[:12], node_type=types.get(n))
|
||||
for n, k, _ in specs
|
||||
]
|
||||
signals = {k[:12]: ContactSignals(node=k[:12], **sig) for _, k, sig in specs}
|
||||
return rank_contacts(contacts, signals)
|
||||
|
||||
|
||||
Reference in new issue
Block a user