diff --git a/meshterm/ui/menu.py b/meshterm/ui/menu.py index 2bec28e..f72b5e3 100644 --- a/meshterm/ui/menu.py +++ b/meshterm/ui/menu.py @@ -25,7 +25,6 @@ from rich.text import Text from .. import __version__ from ..context import AppContext from ..persistence.logging import get_logger -from ..persistence.repository import ACTIVITY_BUCKETS from ..tools import all_tools from .surface import TuiUi from .widgets import activity_sparkline @@ -114,33 +113,36 @@ def _silence_console_logging() -> Iterator[None]: logger.addHandler(handler) -#: Packet counts a five-minute bucket must reach for each extra dot of the header +#: Packet counts a one-minute bucket must reach for each extra dot of the header #: indicator's bar height. Higher-shouldered than the channel sparkline's thresholds #: because this counts *every* packet the hub hears (adverts, telemetry, messages, -#: acks), not one conversation: 1 packet lights a dot, 5 light two, 21 light three, -#: and 89 or more max the column out. -_HEADER_ACTIVITY_LEVELS = (1, 5, 21, 89) +#: acks), not one conversation: 1 packet lights a dot, 3 light two, 8 light three, +#: and 21 or more max the column out. +_HEADER_ACTIVITY_LEVELS = (1, 3, 8, 21) -def _header(ctx: AppContext, cache: dict) -> Text: +def _header(ctx: AppContext, cache: dict, width: int) -> Text: """Build the persistent one-line header: who's connected, unread mail, mesh pulse. Left to right: the app mark, the connected node's own name with where it's reached (``(COM5)`` / ``(BLE)``), an unread-message badge (channels and direct alike, shown only when something is waiting, in the red-dot language of the channel list and the - conversation picker), and a two-hour braille activity indicator counting every - packet the hub hears — the same drawing as the channel manager's sparklines, with a - leading ●/○ live-light for whether the hub is pumping yet. + conversation picker), and a braille activity indicator counting every packet the + hub hears at one minute per dot column — stretched to fill every remaining cell of + the row, so a wider terminal simply shows deeper history — with a leading ●/○ + live-light for whether the hub is pumping yet. Args: ctx: The shared application context, read live on every repaint. cache: Scratch owned by the session (see :func:`_device_label`) so the device-name lookup doesn't re-read the registry file per repaint. + width: The terminal width in columns; the sparkline soaks up whatever the + fixed segments leave of it. Returns: A Rich :class:`Text` shown at the top of every screen. The frame crops it to a - single line (see ``frame.compose_base``), so a narrow terminal chops the tail - rather than wrapping. + single line (see ``frame.compose_base``), so a too-narrow terminal chops the + tail rather than wrapping. """ header = Text() header.append("MeshTerm", style="brand") @@ -161,9 +163,14 @@ def _header(ctx: AppContext, cache: dict) -> Text: header.append(" · ") header.append("●" if ctx.events.active else "○", style="ok" if ctx.events.active else "muted") header.append(" ") - header.append_text( - activity_sparkline(ctx.monitor.activity_histogram(), _HEADER_ACTIVITY_LEVELS, ACTIVITY_BUCKETS) - ) + # Two dot columns per cell: every cell left of the row's edge shows two minutes. + room = width - header.cell_len + if room > 0: + header.append_text( + activity_sparkline( + ctx.monitor.activity_histogram(), _HEADER_ACTIVITY_LEVELS, room * 2 + ) + ) return header @@ -229,7 +236,7 @@ async def run_menu(ctx: AppContext) -> None: calibrate_emoji_width() header_cache: dict = {} - session = TuiSession(header=lambda: _header(ctx, header_cache)) + session = TuiSession(header=lambda cols: _header(ctx, header_cache, cols)) ctx.ui = TuiUi(session) async def main() -> None: diff --git a/meshterm/ui/tui/session.py b/meshterm/ui/tui/session.py index 5fc0e42..c114368 100644 --- a/meshterm/ui/tui/session.py +++ b/meshterm/ui/tui/session.py @@ -97,7 +97,7 @@ class TuiSession: def __init__( self, - header: Optional[Callable[[], RenderableType]] = None, + header: Optional[Callable[[int], RenderableType]] = None, *, input: Any = None, # noqa: A002 - matches prompt_toolkit's Application(input=) name output: Any = None, @@ -105,15 +105,17 @@ class TuiSession: """Create a session. Args: - header: Callable returning the persistent header renderable (banner + live - status), re-invoked on every repaint. Defaults to a plain title. + header: Callable taking the terminal width in columns and returning the + persistent header renderable (banner + live status), re-invoked on + every repaint — the width lets it stretch trailing content (the + activity sparkline) to fill the row exactly. Defaults to a plain title. input: Optional prompt_toolkit input to drive the app from (tests use a pipe); defaults to the real terminal. output: Optional prompt_toolkit output to render to (tests use a dummy); defaults to the real terminal. """ self._stack: list[Screen] = [] - self._header = header or (lambda: Text("MeshTerm", style="brand")) + self._header = header or (lambda cols: Text("MeshTerm", style="brand")) self._app: Optional[Application] = None self._input = input self._output = output @@ -707,7 +709,7 @@ class TuiSession: from .render import render_lines cols, rows = self._size() - header_h = len(render_lines(self._header(), cols, no_wrap=True)) + header_h = len(render_lines(self._header(cols), cols, no_wrap=True)) viewport = max(1, rows - header_h - 1 - 2) # minus footer(1) and panel border(2) return cols - 4, viewport @@ -737,7 +739,7 @@ class TuiSession: if not base.chrome: return ANSI(frame.compose_startup(base, cols, rows)) footer = self.top.footer_hint if self.top else base.footer_hint - return ANSI(frame.compose_base(self._header(), base, footer, cols, rows)) + return ANSI(frame.compose_base(self._header(cols), base, footer, cols, rows)) def _render_float(self) -> ANSI: """Render the top screen as a centered dialog (only when floating).""" diff --git a/tests/test_header.py b/tests/test_header.py new file mode 100644 index 0000000..7536c46 --- /dev/null +++ b/tests/test_header.py @@ -0,0 +1,56 @@ +"""Header tests: the one-line status bar's width-filling activity sparkline.""" + +from __future__ import annotations + +from types import SimpleNamespace + +from meshterm.ui.menu import _HEADER_ACTIVITY_LEVELS, _header + + +def _ctx(histogram=(), unread=0, active=True) -> SimpleNamespace: + """A minimal stand-in exposing only what the header reads (simulator path).""" + return SimpleNamespace( + mock=True, + chat=SimpleNamespace(unread_total=lambda: unread), + events=SimpleNamespace(active=active), + monitor=SimpleNamespace(activity_histogram=lambda: tuple(histogram)), + ) + + +def test_header_sparkline_fills_the_row_exactly() -> None: + """Every cell the fixed segments leave is sparkline — no short rows, no overflow.""" + for width in (60, 100): + header = _header(_ctx(histogram=(21,) * 360), {}, width) + assert header.cell_len == width + assert "⣿" in header.plain # maxed-out cells all the way to the edge + + +def test_header_wider_terminal_shows_deeper_history() -> None: + """A burst 90 minutes ago is off a narrow header but visible on a wide one. + + One minute per dot column: 90 minutes back needs 45 cells of sparkline, so it + renders as a lit cell only when the row leaves that much room. + """ + histogram = tuple(0 if i != 90 else 21 for i in range(360)) + narrow = _header(_ctx(histogram=histogram), {}, 60).plain + wide = _header(_ctx(histogram=histogram), {}, 120).plain + full_bar = chr(0x2800 | 0x47) # a maxed-out lone left dot column + assert full_bar not in narrow + assert full_bar in wide + + +def test_header_thresholds_step_the_bar_heights() -> None: + """The documented 1/3/8/21 shoulders map counts to 1–4 dots.""" + assert _HEADER_ACTIVITY_LEVELS == (1, 3, 8, 21) + histogram = (1, 0, 3, 0, 8, 0, 21, 0) + (0,) * 352 + header = _header(_ctx(histogram=histogram), {}, 80).plain + spark = header[header.index("●") + 2 :] # after the live-light and its space + left = (0x00, 0x40, 0x44, 0x46, 0x47) + assert [spark[i] for i in range(4)] == [chr(0x2800 | left[h]) for h in (1, 2, 3, 4)] + + +def test_header_survives_a_sliver_terminal() -> None: + """When the fixed segments already overflow the width, the sparkline just stays out.""" + header = _header(_ctx(histogram=(9,) * 360), {}, 10) + # No room left: no braille at all, nothing stretched past the edge. + assert all(not (0x2800 <= ord(ch) <= 0x28FF) for ch in header.plain)