Two bits of chrome the screen had been going without. A column header, pinned above the rows. It needs none of the base screen's sticky-header machinery: the feed windows inside the space the chrome leaves, so the body never scrolls and the header cannot travel off the top of it. Nothing here sorts — a feed is a stream and its one order is the status line's to state — so no column carries the contact list's sort triangle or its lit active-column style. These are signposts, not controls. Getting a label to sit over its values meant the row and the header having to agree about the lanes, so both now measure them from one set of constants. That turned up a real defect: the RSSI lane rendered 11 cells when a packet carried a reading and 10 when it didn't, so rows were ragged by one and the class-label threshold was a cell short — at exactly its own width the row lost its last character. Both readings now right-align their number in a shared field, their labels right-align over the same field, and an empty lane pads to what it would have filled. And the highlight is the app's own at last: ❯ over a brand row, as every select list draws it, in place of the ▸/accent this one screen had invented. The lanes keep their colours over the tint — those are the content; the pointer is what says "this row". Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
595 lines
27 KiB
Python
595 lines
27 KiB
Python
"""The Live feed: every packet as it arrives, full-screen and newest first.
|
||
|
||
The interactive face of the ``livefeed`` tool — the dashboard's old feed panel,
|
||
promoted to a first-class screen. One always-repainting list streams the latest
|
||
packets, newest first: time, class (icon + label, icon alone on a narrow terminal),
|
||
node, SNR/RSSI, and a message's conversation. What a row deliberately does *not* carry
|
||
is the relay path a frame rode in on: a route is a shape, not a lane, and squeezing one
|
||
into the cells left at the right edge only ever produced a stub — so the route belongs
|
||
to the packet viewer, which has the room to draw it as a wrapped path line over its
|
||
graph. The feed's job is to say what arrived and how well it was heard; Enter says how
|
||
it got here.
|
||
|
||
Seeded from stored history so the screen opens full, then streamed
|
||
live off the event hub. ``↑``/``↓`` walk the rows, PgUp/PgDn/Home/End page and jump
|
||
them, ``←``/``→`` scroll the highlighted row sideways on a terminal too narrow to hold
|
||
it whole, and Enter opens the highlighted packet in the shared
|
||
:class:`~meshterm.ui.packet_viewer.PacketViewer` — which then pages through the feed
|
||
itself with the same ``↑``/``↓``, and, for an overheard channel-text packet naming a
|
||
channel we hold the key for, decrypts it.
|
||
|
||
The screen holds no subscriptions of its own — the opener (:func:`open_livefeed`)
|
||
wires the hub subscription and the once-a-second repaint, and tears them down when
|
||
the screen resolves. The newest packet is highlighted from the moment the screen
|
||
opens; Esc backs out directly.
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import asyncio
|
||
from collections import deque
|
||
from typing import TYPE_CHECKING, Any, Optional, Sequence
|
||
|
||
from rich.text import Text
|
||
|
||
from ..core.channels import split_channel_sender
|
||
from ..core.events import EventKind, MeshEvent
|
||
from ..core.models import Observation, utcnow
|
||
from ..persistence.repository import OBSERVATION_WINDOW
|
||
from .menus import fit_cells
|
||
from .packet_viewer import (
|
||
KIND_STYLES,
|
||
PacketEntry,
|
||
PacketViewer,
|
||
class_marks,
|
||
node_label,
|
||
)
|
||
from .theme import name_style, snr_style
|
||
from .tui.render import crop_cells, render_to_ansi
|
||
from .tui.screen import ListWindow, Screen
|
||
from .widgets import NameKeyResolver, TypeOf
|
||
|
||
if TYPE_CHECKING:
|
||
from ..context import AppContext
|
||
|
||
#: Seconds between full repaints while the feed is open (keeps the live-light and any
|
||
#: age-sensitive chrome honest even between hub events).
|
||
_REFRESH_S = 1.0
|
||
|
||
#: How many feed rows are kept (the feed windows within the screen, so this is
|
||
#: history depth, not layout).
|
||
_FEED_CAP = 100
|
||
|
||
#: The feed's fixed node-name lane width; longer names ellipsize so the columns hold.
|
||
_FEED_NAME_WIDTH = 18
|
||
|
||
#: The class lane's fixed width — wide enough for the longest class the app files a
|
||
#: packet under (``direct message``), so every label reads whole and the lanes hold.
|
||
_FEED_CLASS_WIDTH = 14
|
||
|
||
# The remaining lanes, as the row and its column header both measure them — one set of
|
||
# numbers so a header label can never drift off the values it names, and so a lane a
|
||
# packet left empty pads to exactly the width it would have filled.
|
||
#: Cells between adjacent lanes.
|
||
_LANE_GAP = 2
|
||
#: ``HH:MM:SS`` plus the gap that follows it.
|
||
_TIME_LANE = 8 + _LANE_GAP
|
||
#: The class icon and its trailing space (all the class lane keeps on a narrow terminal).
|
||
_ICON_LANE = 3
|
||
#: Cells a reception reading right-aligns its number in — the ``%+5.1f``/``%5.0f`` field
|
||
#: both lanes are built on, and the slot their column headers sit over.
|
||
_READING_W = 5
|
||
#: ``+13.2 dB`` — the number field, then its unit.
|
||
_SNR_LANE = _READING_W + len(" dB")
|
||
#: `` -61 dBm`` — the lane gap, the number field, then its unit.
|
||
_RSSI_LANE = _LANE_GAP + _READING_W + len(" dBm")
|
||
|
||
#: Terminal width below which the feed drops the textual class label and keeps only the
|
||
#: two-cell icon. It is exactly the width the full row occupies — with no route to make
|
||
#: room for, every lane fits a 72-column screen, so the label (the row's most informative
|
||
#: field now that it names the real class) is only ever dropped when it truly can't fit.
|
||
_FEED_LABEL_MIN_WIDTH = (
|
||
2 + _TIME_LANE + _ICON_LANE + _FEED_CLASS_WIDTH + _LANE_GAP
|
||
+ _FEED_NAME_WIDTH + _LANE_GAP + _SNR_LANE + _RSSI_LANE
|
||
)
|
||
|
||
#: Cells one ←/→ press shifts the highlighted row by — the app-wide select list's own
|
||
#: step (:attr:`~meshterm.ui.tui.select.SelectScreen._HSCROLL_STEP`), so a row here
|
||
#: scrolls at the rate a row anywhere else does.
|
||
_HSCROLL_STEP = 8
|
||
|
||
#: Moves that abandon the highlighted row's horizontal scroll: each row scrolls on its
|
||
#: own, exactly as an ``hscroll`` select list's rows do — landing on a new packet always
|
||
#: starts it at its own beginning.
|
||
_HSHIFT_RESET = frozenset({
|
||
"up", "down", "pageup", "pagedown", "space", "home", "ctrl_home", "end", "ctrl_end",
|
||
})
|
||
|
||
|
||
def _channel_sender(text: Optional[str]) -> Optional[str]:
|
||
"""The sender named by a channel message's ``Name: `` prefix, or ``None`` if absent.
|
||
|
||
The shared protocol-layer parse (see
|
||
:func:`~meshterm.core.channels.split_channel_sender`), so the feed's node lane names
|
||
exactly the sender the chat transcript would.
|
||
"""
|
||
name, _body = split_channel_sender(text or "")
|
||
return name
|
||
|
||
|
||
class LiveFeedScreen(Screen):
|
||
"""The full-screen packet stream. Renders state; the opener feeds it."""
|
||
|
||
floating = False
|
||
|
||
def __init__(
|
||
self,
|
||
*,
|
||
session: Any,
|
||
resolve: Any,
|
||
seed: list[Observation],
|
||
hub_active: Any,
|
||
prefix_bytes: int = 0,
|
||
self_name: Optional[str] = None,
|
||
channels: Sequence[tuple[str, bytes]] = (),
|
||
type_of: Optional[TypeOf] = None,
|
||
key_of: Optional[NameKeyResolver] = None,
|
||
) -> None:
|
||
"""Create the feed over its data feeds.
|
||
|
||
Args:
|
||
session: The running TUI session (for repaints and the packet viewer).
|
||
resolve: Maps a node hash to a friendly contact name when known.
|
||
seed: Stored observations to open with (oldest first, as the repository
|
||
returns them); the newest :data:`_FEED_CAP` become the opening feed.
|
||
hub_active: Zero-arg callable: whether the event hub is pumping.
|
||
prefix_bytes: The hash width to light in the packet viewer's keys.
|
||
self_name: Our own node's name, drawn white wherever it appears.
|
||
channels: The device's configured channels, as ``(name, secret)`` pairs,
|
||
handed to each opened :class:`~meshterm.ui.packet_viewer.PacketViewer`
|
||
so it can attempt to decrypt an overheard channel-text packet.
|
||
type_of: Maps a relay hash to its node type, handed to the packet viewer so a
|
||
relayed packet's route graph marks a repeater ``▲`` (etc.) over a dot.
|
||
key_of: Maps a sender's display name back to its node's key (see
|
||
:func:`~meshterm.services.trace_runner.make_name_key_resolver`), so a
|
||
channel sender the contacts or the recorder know takes its key-derived
|
||
hue; an unresolvable name stays muted.
|
||
"""
|
||
super().__init__()
|
||
self.title = "Live feed"
|
||
self._session = session
|
||
self._resolve = resolve
|
||
self._hub_active = hub_active
|
||
self._prefix_bytes = prefix_bytes
|
||
self._self_name = self_name
|
||
self._channels = channels
|
||
self._type_of = type_of
|
||
self._key_of: NameKeyResolver = key_of or (lambda name: None)
|
||
#: The feed: latest events of every class as data, newest first — rendered
|
||
#: fresh each paint (rows adapt to width) and handed whole to the viewer.
|
||
self._feed: deque[PacketEntry] = deque(maxlen=_FEED_CAP)
|
||
for obs in list(seed)[-_FEED_CAP:][::-1]:
|
||
self._feed.append(PacketEntry.from_observation(obs))
|
||
#: The highlighted feed row. Starts on the newest packet (``None`` only when
|
||
#: the feed is empty) so a lone Esc always backs straight out of the screen.
|
||
self._selected: Optional[int] = 0 if self._feed else None
|
||
#: The feed's window within the fixed screen (its rows scroll under the heading).
|
||
self._feed_window = ListWindow()
|
||
#: The highlighted row's horizontal scroll (cells shifted in, ``←/→``) and how
|
||
#: far it can shift — measured against the width of the last paint, so a resize
|
||
#: can only ever clamp an in-progress scroll back into range.
|
||
self._hshift = 0
|
||
self._hmax = 0
|
||
|
||
# --- live feed -----------------------------------------------------------------
|
||
|
||
def on_event(self, event: MeshEvent) -> None:
|
||
"""Fold one hub event into the feed and repaint."""
|
||
entry: Optional[PacketEntry] = None
|
||
obs = event.observation
|
||
if obs is not None:
|
||
entry = PacketEntry.from_observation(obs)
|
||
elif event.kind == EventKind.MESSAGE and event.message is not None:
|
||
msg = event.message
|
||
entry = PacketEntry(
|
||
when=utcnow(), kind="message", node=msg.sender, snr=msg.snr,
|
||
where=f"ch {msg.channel}" if msg.is_channel else "direct",
|
||
text=msg.text, raw=msg.raw,
|
||
)
|
||
elif event.kind == EventKind.ACK and event.ack is not None:
|
||
entry = PacketEntry(
|
||
when=utcnow(), kind="ack",
|
||
where=event.ack.code or "delivery confirmed",
|
||
)
|
||
if entry is not None:
|
||
self._feed.appendleft(entry)
|
||
# Keep the highlight on the same packet as new rows push it down.
|
||
if self._selected is not None:
|
||
self._selected = min(self._selected + 1, len(self._feed) - 1)
|
||
self._session.invalidate()
|
||
|
||
# --- input -----------------------------------------------------------------------
|
||
|
||
@property
|
||
def footer_hint(self) -> str: # type: ignore[override]
|
||
"""The key hint, gaining ``←→ scroll line`` exactly while it would do something.
|
||
|
||
A row that already fits scrolls nowhere, so the atom stays hidden on it — the
|
||
same "advertise a key only where it acts" rule the select list's own ``hscroll``
|
||
hint follows.
|
||
"""
|
||
base = "↑↓ PgUp/PgDn Home/End move · Enter open · Esc back"
|
||
if self._selected is not None and self._hmax > 0:
|
||
return "↑↓ PgUp/PgDn Home/End move · ←→ scroll line · Enter open · Esc back"
|
||
return base
|
||
|
||
def handle(self, action: str, data: str = "") -> None:
|
||
"""Walk the feed, open the highlighted packet, scroll, or dismiss."""
|
||
if action in _HSHIFT_RESET:
|
||
self._hshift = 0 # moving off a row abandons its scroll
|
||
if action == "up":
|
||
self._move_selection(-1)
|
||
elif action == "down":
|
||
self._move_selection(1)
|
||
elif action == "enter":
|
||
self._open_packet()
|
||
elif action == "pageup":
|
||
# With a feed row highlighted the page keys walk the selection (a
|
||
# windowful at a time), so the highlight travels with the window; with
|
||
# none, they slide the feed window itself under the pinned heading.
|
||
if self._selected is not None:
|
||
self._select_index(self._selected - self._feed_window.page)
|
||
else:
|
||
self._feed_window.top -= self._feed_window.page
|
||
self._session.invalidate()
|
||
elif action in ("pagedown", "space"):
|
||
if self._selected is not None:
|
||
self._select_index(self._selected + self._feed_window.page)
|
||
else:
|
||
self._feed_window.top += self._feed_window.page
|
||
self._session.invalidate()
|
||
elif action in ("home", "ctrl_home"):
|
||
# With the feed highlight active, Home jumps to the newest packet;
|
||
# otherwise it slides the window to the feed's newest end (End mirrors).
|
||
if self._selected is not None:
|
||
self._select_index(0)
|
||
else:
|
||
self._feed_window.top = 0
|
||
self._session.invalidate()
|
||
elif action in ("end", "ctrl_end"):
|
||
if self._selected is not None:
|
||
self._select_index(len(self._feed) - 1)
|
||
else:
|
||
self._feed_window.to_end()
|
||
self._session.invalidate()
|
||
elif action == "left":
|
||
self._scroll_line(-_HSCROLL_STEP)
|
||
elif action == "right":
|
||
self._scroll_line(_HSCROLL_STEP)
|
||
elif action == "escape":
|
||
self.resolve(None)
|
||
|
||
def _scroll_line(self, delta: int) -> None:
|
||
"""Shift the highlighted row sideways (clamped to its own tail) and repaint."""
|
||
shift = max(0, min(self._hshift + delta, self._hmax))
|
||
if shift != self._hshift:
|
||
self._hshift = shift
|
||
self._session.invalidate()
|
||
|
||
def _move_selection(self, delta: int) -> None:
|
||
"""Move the feed highlight (the first press lands on the newest packet)."""
|
||
if not self._feed:
|
||
return
|
||
if self._selected is None:
|
||
self._select_index(0)
|
||
else:
|
||
self._select_index(self._selected + delta)
|
||
|
||
def _select_index(self, index: int) -> None:
|
||
"""Highlight one feed row (clamped) and repaint."""
|
||
self._selected = max(0, min(index, len(self._feed) - 1))
|
||
self._session.invalidate()
|
||
|
||
def _open_packet(self) -> None:
|
||
"""Float the packet viewer over the highlighted feed row.
|
||
|
||
Paging inside the viewer walks the feed highlight in step (via
|
||
``on_navigate``), so closing it lands back on the packet last viewed.
|
||
"""
|
||
if self._selected is None or not self._feed:
|
||
return
|
||
|
||
def follow(entry: PacketEntry) -> None:
|
||
# The feed may have grown since the snapshot; find the entry itself.
|
||
for i, candidate in enumerate(self._feed):
|
||
if candidate is entry:
|
||
self._select_index(i)
|
||
return
|
||
|
||
viewer = PacketViewer(
|
||
list(self._feed), self._selected,
|
||
resolve=self._resolve, prefix_bytes=self._prefix_bytes,
|
||
self_name=self._self_name, on_navigate=follow,
|
||
channels=self._channels, type_of=self._type_of, key_of=self._key_of,
|
||
# The live feed itself (newest first), so the viewer keeps up with packets
|
||
# that arrive while it is open instead of freezing at this snapshot.
|
||
source=lambda: list(self._feed),
|
||
)
|
||
asyncio.ensure_future(self._session.run_screen(viewer))
|
||
|
||
# --- rendering ---------------------------------------------------------------------
|
||
|
||
def render_body(self, width: int) -> list[str]:
|
||
"""The status line and column header, then the feed's window under them.
|
||
|
||
Both chrome lines are pinned by construction rather than by the base screen's
|
||
sticky-header machinery: the feed windows *inside* the space they leave (see
|
||
:meth:`_feed_lines`), so the body never scrolls and the header can never travel
|
||
off the top of it.
|
||
"""
|
||
if self._selected is not None and self._feed:
|
||
self._selected = min(self._selected, len(self._feed) - 1)
|
||
else:
|
||
self._hmax = 0 # nothing highlighted scrolls, so nothing advertises ←→
|
||
show_label = width >= _FEED_LABEL_MIN_WIDTH
|
||
lines = [render_to_ansi(self._heading(), width, no_wrap=True)]
|
||
if self._feed: # a header over nothing is noise; the empty note speaks for itself
|
||
lines.append(render_to_ansi(self._column_header(show_label), width, no_wrap=True))
|
||
win = max(1, self._scroll_viewport - len(lines))
|
||
lines.extend(self._feed_lines(width, win, show_label))
|
||
self._scroll_total = max(1, len(lines))
|
||
return lines
|
||
|
||
def _heading(self) -> Text:
|
||
"""The feed's pinned status line, with a live-light for the hub."""
|
||
heading = Text("newest first · Enter opens a packet ", style="muted")
|
||
if self._hub_active():
|
||
heading.append("● live", style="ok")
|
||
else:
|
||
heading.append("○ waiting for a device", style="muted")
|
||
return heading
|
||
|
||
@staticmethod
|
||
def _column_header(show_label: bool) -> Text:
|
||
"""The lane names, in the app's uppercase muted column-header voice.
|
||
|
||
Laid out lane for lane against :meth:`_feed_row`, the pointer column included, so
|
||
every label sits over the values it names. Nothing here sorts — the feed is a
|
||
stream, and its one order (newest first) is the status line's to state — so no
|
||
column carries the contact list's sort triangle or its lit active-column style;
|
||
these are signposts, not controls. On a terminal too narrow for the class label
|
||
the header drops its ``CLASS`` too, leaving the icon lane unlabelled rather than
|
||
clipping a word into three cells.
|
||
"""
|
||
header = Text(" ", style="muted") # the pointer lane
|
||
header.append(fit_cells("TIME", _TIME_LANE))
|
||
header.append(
|
||
fit_cells("CLASS", _ICON_LANE + _FEED_CLASS_WIDTH + _LANE_GAP)
|
||
if show_label
|
||
else " " * _ICON_LANE
|
||
)
|
||
header.append(fit_cells("NODE", _FEED_NAME_WIDTH + _LANE_GAP))
|
||
# The two readings right-align their number, so their labels do too — each sits
|
||
# over the digits it names rather than over the sign column ahead of them.
|
||
header.append(fit_cells("SNR", _READING_W, align="right"))
|
||
header.append(" " * (_SNR_LANE - _READING_W))
|
||
header.append(" " * _LANE_GAP)
|
||
header.append(fit_cells("RSSI", _READING_W, align="right"))
|
||
header.append(" " * (_RSSI_LANE - _LANE_GAP - _READING_W))
|
||
return header
|
||
|
||
def _feed_lines(self, width: int, win: int, show_label: bool) -> list[str]:
|
||
"""The feed's windowed rows: newest first, ``↑/↓ n more`` at the edges."""
|
||
if not self._feed:
|
||
return [render_to_ansi(Text("nothing heard yet", style="muted"), width)]
|
||
entries = list(self._feed)
|
||
top, count = self._feed_window.fit(len(entries), win, self._selected)
|
||
out: list[str] = []
|
||
if top > 0:
|
||
out.append(render_to_ansi(ListWindow.marker(top, "above"), width))
|
||
for i in range(top, top + count):
|
||
row = self._feed_row(entries[i], i == self._selected, show_label, width)
|
||
out.append(render_to_ansi(row, width, no_wrap=True))
|
||
below = len(entries) - top - count
|
||
if below > 0:
|
||
out.append(render_to_ansi(ListWindow.marker(below, "below"), width))
|
||
return out
|
||
|
||
def _feed_row(
|
||
self, entry: PacketEntry, selected: bool, show_label: bool, width: int
|
||
) -> Text:
|
||
"""Lay one feed row out in fixed lanes: time, class, node, reception, detail.
|
||
|
||
The class lane says what the packet *is*, straight from
|
||
:func:`~meshterm.ui.packet_viewer.class_marks` — so a raw frame reads
|
||
``📻 channel text``, the class the viewer's card headlines, rather than the
|
||
``📦 packet`` event family it merely arrived in. Its icon always shows; the
|
||
textual label beside it is dropped wholesale on a narrow terminal
|
||
(``show_label``), keeping the lanes aligned either way. The node lane is then
|
||
free to be about the node: its name in the app-wide palette hue (our own node
|
||
white, a bare hash muted), and a dash where nobody identified themselves. No
|
||
relay path rides here — the whole row fits a 72-column screen precisely because
|
||
it doesn't try to, and the route is drawn properly one keypress away, in the
|
||
packet viewer.
|
||
|
||
The highlight is the app's own: the ``❯`` pointer and a brand base style under the
|
||
whole row, exactly as every select list draws its cursor. The lanes keep their own
|
||
colours over it — the SNR its quality hue, a name its palette hue — because those
|
||
colours are the content; the pointer and the base tint are what say "this row".
|
||
|
||
A row that runs past the right edge — a terminal narrower than the lanes need —
|
||
is read by scrolling it, not by growing it: the *highlighted* row slides under
|
||
``←→`` (:attr:`_hshift`), the app-wide h-scroll convention, with the ``❯``
|
||
pointer lane pinned so the cursor never scrolls away from the row it marks. The
|
||
shift bound is measured here, against the current width, so a resize can only
|
||
clamp it back into range.
|
||
"""
|
||
row = Text(no_wrap=True, overflow="ellipsis")
|
||
row.append("❯ " if selected else " ", style="brand" if selected else "")
|
||
avail = max(1, width - 2)
|
||
|
||
body = Text(no_wrap=True, overflow="ellipsis")
|
||
body.append(
|
||
fit_cells(entry.when.astimezone().strftime("%H:%M:%S"), _TIME_LANE),
|
||
style="muted",
|
||
)
|
||
icon, class_label = class_marks(entry)
|
||
body.append(fit_cells(icon, _ICON_LANE))
|
||
if show_label:
|
||
body.append(
|
||
fit_cells(class_label, _FEED_CLASS_WIDTH),
|
||
style=KIND_STYLES.get(entry.kind, "brand"),
|
||
)
|
||
body.append(" " * _LANE_GAP) # the lane's gutter — the longest class fills it
|
||
label, style = node_label(entry, self._resolve, self._self_name)
|
||
if label == "?":
|
||
label, style = self._feed_subject(entry) # no node identity: name what we can
|
||
body.append(fit_cells(label, _FEED_NAME_WIDTH), style=style)
|
||
body.append(" " * _LANE_GAP)
|
||
body.append(
|
||
f"{entry.snr:+{_READING_W}.1f} dB" if entry.snr is not None
|
||
else " " * _SNR_LANE,
|
||
style=snr_style(entry.snr) if entry.snr is not None else "muted",
|
||
)
|
||
body.append(
|
||
f"{'':{_LANE_GAP}}{entry.rssi:{_READING_W}.0f} dBm"
|
||
if entry.rssi is not None else " " * _RSSI_LANE,
|
||
style="muted",
|
||
)
|
||
note = self._feed_note(entry)
|
||
if note is not None:
|
||
body.append(" " * _LANE_GAP)
|
||
body.append_text(note)
|
||
|
||
if selected:
|
||
self._hmax = max(0, body.cell_len - avail)
|
||
self._hshift = min(self._hshift, self._hmax)
|
||
if self._hshift:
|
||
body = crop_cells(body, self._hshift, avail)
|
||
row.append_text(body)
|
||
if selected:
|
||
# The brand tint rides *under* the row's spans (the select list's own
|
||
# convention), so the lanes keep their colours and only the gaps take it.
|
||
row.style = "brand"
|
||
return row
|
||
|
||
def _feed_subject(self, entry: PacketEntry) -> tuple[str, str]:
|
||
"""Name the node lane when an entry carries no resolvable node identity.
|
||
|
||
Rather than a useless ``?``, the lane reads the most identifying thing the entry
|
||
still carries: the sender a channel message named itself with, then an ack's
|
||
code. What it deliberately does *not* do is stand in the packet's class — the
|
||
class lane two columns left already says that, straight from
|
||
:func:`~meshterm.ui.packet_viewer.class_marks`, and a row that spelled it twice
|
||
was reading ``packet`` beside ``channel text`` as though they were two facts.
|
||
A relayed flood names no origin, so its lane says so with a dash — the route that
|
||
would name nodes is the packet viewer's to draw, not a stub for this row to carry.
|
||
"""
|
||
if entry.kind == "message":
|
||
sender = _channel_sender(entry.text)
|
||
if sender:
|
||
ours = self._self_name and sender == self._self_name
|
||
# A resolvable sender takes its key-derived hue; a stranger stays
|
||
# muted — colour is reserved for keyed identities.
|
||
return sender, ("you" if ours else name_style(sender, self._key_of(sender)))
|
||
return "channel", "muted"
|
||
if entry.where:
|
||
return entry.where, "muted" # an ack's code, or any other stray context
|
||
return "—", "muted"
|
||
|
||
def _feed_note(self, entry: PacketEntry) -> Optional[Text]:
|
||
"""The row's trailing detail — a message's conversation, and nothing else.
|
||
|
||
A relayed frame's route used to sit here, and it never fitted: whatever the fixed
|
||
lanes left it was a handful of cells, so the chain arrived elided to a stub that
|
||
named one hop and hinted at the rest. A route is a shape rather than a lane, and
|
||
it is one keypress away — the packet viewer draws it as a wrapped path line over
|
||
the route graph, with the room to say the whole thing. So the feed says what
|
||
arrived; Enter says how it got here.
|
||
|
||
Args:
|
||
entry: The packet the row describes.
|
||
|
||
Returns:
|
||
The detail to append, or ``None`` when the class carries none.
|
||
"""
|
||
if entry.kind == "message" and entry.where:
|
||
return Text(entry.where, style="muted")
|
||
return None
|
||
|
||
|
||
async def open_livefeed(ctx: "AppContext") -> None:
|
||
"""Open the live feed and run it until dismissed.
|
||
|
||
Wires the screen to its feeds: stored recent observations seed it, a hub
|
||
subscription streams every new packet in, and a once-a-second ticker keeps the
|
||
live-light honest. Everything is torn down when the screen closes.
|
||
|
||
Args:
|
||
ctx: The shared application context (must be running the interactive TUI surface).
|
||
|
||
Raises:
|
||
RuntimeError: If called outside the interactive menu (no full-screen session).
|
||
"""
|
||
from ..services import trace_runner
|
||
from .surface import TuiUi
|
||
from .timemachine_screen import _routing_prefix_bytes
|
||
|
||
if not isinstance(ctx.ui, TuiUi): # pragma: no cover - guarded by the menu-only caller
|
||
raise RuntimeError("the live feed is only available in the menu")
|
||
session = ctx.ui.session
|
||
|
||
contacts = []
|
||
self_name: Optional[str] = None
|
||
channels: list[tuple[str, bytes]] = []
|
||
try:
|
||
if ctx.is_connected or ctx.settings.connect_on_start:
|
||
# Through the session cache: contacts and the channel-slot probe are the two
|
||
# slowest reads on a screen open, so reading them once (not per feed open)
|
||
# is what keeps navigation snappy over Bluetooth.
|
||
contacts = await ctx.devstate.contacts()
|
||
self_name = (await ctx.devstate.self_info()).get("name") or None
|
||
channels = [(s.name, s.secret) for s in await ctx.devstate.channel_slots()]
|
||
except Exception: # noqa: BLE001 - the feed renders fine without contact names
|
||
contacts = []
|
||
# Contacts first, every name the recorder ever overheard as the fallback — the
|
||
# app-wide rule that a node we can name never renders as a bare hash.
|
||
resolve = trace_runner.make_node_resolver(contacts, ctx.repo.node_names())
|
||
type_of = trace_runner.make_node_type_resolver(contacts)
|
||
key_of = trace_runner.make_name_key_resolver(contacts, ctx.repo.node_names())
|
||
prefix_bytes = await _routing_prefix_bytes(ctx)
|
||
|
||
seed = ctx.repo.recent_observations(since=utcnow() - OBSERVATION_WINDOW)
|
||
screen = LiveFeedScreen(
|
||
session=session,
|
||
resolve=resolve,
|
||
seed=seed,
|
||
hub_active=lambda: ctx.events.active,
|
||
prefix_bytes=prefix_bytes,
|
||
self_name=self_name,
|
||
channels=channels,
|
||
type_of=type_of,
|
||
key_of=key_of,
|
||
)
|
||
|
||
unsubscribe = ctx.events.subscribe(screen.on_event)
|
||
|
||
async def tick() -> None:
|
||
"""Repaint once a second so the live-light and clock-sensitive rows stay honest."""
|
||
while True:
|
||
await asyncio.sleep(_REFRESH_S)
|
||
session.invalidate()
|
||
|
||
ticker = asyncio.ensure_future(tick())
|
||
try:
|
||
await session.run_screen(screen)
|
||
finally:
|
||
unsubscribe()
|
||
ticker.cancel()
|
||
try:
|
||
await ticker
|
||
except asyncio.CancelledError:
|
||
pass
|
||
except Exception: # noqa: BLE001 - teardown must never surface a tick hiccup
|
||
pass
|