The CLI had grown the menu's manners -- colour, boxes, section headings, glyph columns, a closing check mark -- and every one of them is in the way of the thing it is actually for. It now prints like a standard Unix utility: plain text, no escape sequences at all, no frames, one record per line, and nothing wrapped, so a long line runs off the right instead of folding its fields onto a second one. meshterm/ui/script.py is the whole vocabulary. Listings are an uppercase header and space-aligned records; key/value blocks are sysctl's shape, and a `get` prints the bare value alone. Names are quoted wherever they share a line, because a name can hold a space or a comma and a bare one is not a field. A route reads "Origin" (3d),"Relay" (f2),"Us" (a1) -- the hash outside the quotes, our own node a hop like any other rather than a star, since the hash is what carries the identity that colour carries on screen. Times are absolute local ISO-8601 and "-" is the one token for absent. The return value is now half the report: 0 ok, 1 failure, 2 usage, 3 no device, 4 the device was reached and the operation failed, 5 nothing to report. That last one is the point -- a command that ran fine and found nothing prints nothing and says so in $?, so a script never has to count output lines to tell an empty mesh from a full one. Errors and progress moved to stderr, and an expected failure no longer arrives as a traceback. Two things lose their scripted face rather than get a degraded one. The map is a picture whose nodes are told apart by colour: monochrome it would be unreadable, coloured it could not be piped anywhere useful, and the nodes themselves are still in `contacts`. The QR codes are a second rendering of a link the output already prints. ui.ack joins ui.note to draw the line the change needed: a note is output, an acknowledgement is reassurance a person wants and a script must filter out, so the menu prints it and the CLI drops it. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
141 lines
5.8 KiB
Python
141 lines
5.8 KiB
Python
"""The ``contacts`` tool: list the contacts this node knows about.
|
|
|
|
The menu opens the sortable Contacts screen, which leads with our own node; the CLI
|
|
prints the contact list alone, because our own node is not a contact — ``meshterm info``
|
|
reports it, in far more detail than a row could hold.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING, Any
|
|
|
|
import typer
|
|
|
|
from ..context import AppContext
|
|
from ..core import exitcodes
|
|
from ..core.models import NODE_TYPE_LABELS
|
|
from .base import Tool, ToolResult, register
|
|
|
|
if TYPE_CHECKING: # pragma: no cover - typing only
|
|
from rich.table import Table
|
|
|
|
from ..core.models import Contact
|
|
|
|
|
|
@register
|
|
class ContactsTool(Tool):
|
|
"""List this node and its known contacts, each with its path-hash prefix."""
|
|
|
|
name = "contacts"
|
|
title = "Contacts"
|
|
icon = "👥"
|
|
help = "Known contacts — last heard, packets, type"
|
|
category = "Message"
|
|
order = 40 # the address book the three above pick their recipient from
|
|
|
|
async def run(self, ctx: AppContext, params: dict[str, Any]) -> ToolResult:
|
|
"""Query the device and render the contacts list.
|
|
|
|
Args:
|
|
ctx: Shared application context.
|
|
params: Unused.
|
|
|
|
Returns:
|
|
A :class:`ToolResult` summarizing the number of known contacts.
|
|
"""
|
|
from ..ui.surface import TuiUi
|
|
from ..ui.widgets import ContactsSort, ordered_contacts
|
|
|
|
# Read through the session cache: on a busy node the contacts table is a slow
|
|
# round-trip, and re-fetching it (plus self-info) on every menu visit is a chief cause
|
|
# of sluggish navigation. The cache holds them for the session and refreshes contacts
|
|
# in the background (see :class:`~meshterm.services.device_state.DeviceState`).
|
|
info = await ctx.devstate.self_info()
|
|
contacts = await ctx.devstate.contacts()
|
|
|
|
# The path-hash prefix — the leading slice of a key a forced path addresses — is
|
|
# ``mode + 1`` bytes wide; highlight it in every key. The mode is an optional read,
|
|
# so fall back to no highlighting when the firmware doesn't report it.
|
|
try:
|
|
mode = await ctx.devstate.path_hash_mode()
|
|
except Exception: # noqa: BLE001 - optional read; absence just skips highlighting
|
|
mode = None
|
|
prefix_bytes = (mode + 1) if isinstance(mode, int) and 0 <= mode <= 3 else 0
|
|
|
|
# Overheard-packet tallies from background monitoring, keyed by node hash; a
|
|
# contact we've never passively overheard simply has no entry.
|
|
counts = {n.node: n.count for n in ctx.repo.heard_nodes() if n.node}
|
|
|
|
self_name = str(info.get("name") or "this node")
|
|
self_key = str(info.get("public_key") or "")
|
|
# Opens most-recently-heard first: the list's job is "who's out there right now",
|
|
# and a freshest-first order answers that on sight — an A→Z roll call doesn't. The
|
|
# Ctrl+arrows re-sort from there (and ``--sort`` picks the CLI's order).
|
|
sort_name = str(params.get("sort") or "heard")
|
|
|
|
# In the menu, hand the list to the interactive screen so the Ctrl+arrows re-sort it
|
|
# live — its ring spans the shared contact list's four columns (hash included); on the
|
|
# scripted CLI, print the plain listing once in the requested order.
|
|
if isinstance(ctx.ui, TuiUi):
|
|
from ..ui.contactlist import SORT_COLUMNS, SORT_OPENS_ASCENDING
|
|
from ..ui.contacts_screen import open_contacts
|
|
|
|
sort = ContactsSort.from_name(sort_name, SORT_COLUMNS, SORT_OPENS_ASCENDING)
|
|
await open_contacts(ctx, self_name, self_key, contacts, prefix_bytes, counts, sort)
|
|
return ToolResult(summary={"contacts": len(contacts)})
|
|
|
|
sort = ContactsSort.from_name(sort_name)
|
|
if contacts:
|
|
ctx.ui.show(_listing(ordered_contacts(contacts, counts, sort), counts))
|
|
return ToolResult(
|
|
summary={"contacts": len(contacts)},
|
|
exit_code=exitcodes.OK if contacts else exitcodes.NO_RESULT,
|
|
)
|
|
|
|
def register_cli(self, app: typer.Typer) -> None:
|
|
"""Register the ``contacts`` subcommand.
|
|
|
|
Args:
|
|
app: The Typer application.
|
|
"""
|
|
from ..cli import run_tool_command
|
|
|
|
@app.command(name=self.name, help=self.help)
|
|
def _contacts(
|
|
sort: str = typer.Option(
|
|
"heard", "--sort", "-s", help="Order contacts by: heard, name, packets"
|
|
),
|
|
) -> None:
|
|
run_tool_command(self, {"sort": sort})
|
|
|
|
|
|
def _listing(contacts: list[Contact], counts: dict[str, int]) -> Table:
|
|
"""The scripted contact list: one record per contact, one line each.
|
|
|
|
``NAME`` is quoted so a name holding a space or a comma stays one field; ``TYPE`` is
|
|
the node's advertised role in words, where the menu draws a coloured glyph; ``HEARD``
|
|
is an absolute local timestamp, where the menu draws a relative age in recency heat;
|
|
and ``KEY`` is the full public key, never elided — a truncated key is not something a
|
|
caller can pass back to ``--path`` or ``--to``.
|
|
|
|
Args:
|
|
contacts: The contacts, already in the requested order.
|
|
counts: Overheard-packet tallies keyed by node id (from passive monitoring).
|
|
|
|
Returns:
|
|
The scripted table (see :func:`meshterm.ui.script.columns`).
|
|
"""
|
|
from ..ui import script
|
|
from ..ui.widgets import _contact_pkts
|
|
|
|
table = script.columns("NAME", "TYPE", "HEARD", "PKTS", "KEY", right=("PKTS",))
|
|
for contact in contacts:
|
|
table.add_row(
|
|
script.quote(contact.name),
|
|
NODE_TYPE_LABELS.get(contact.node_type, "unknown"),
|
|
script.stamp(contact.last_seen),
|
|
script.number(_contact_pkts(contact, counts)),
|
|
(contact.public_key or contact.key_prefix or "").lower() or script.NONE,
|
|
)
|
|
return table
|