diff --git a/meshterm/ui/theme.py b/meshterm/ui/theme.py index bba5073..c93849a 100644 --- a/meshterm/ui/theme.py +++ b/meshterm/ui/theme.py @@ -55,6 +55,9 @@ MESH_THEME = Theme( # A step darker than ``muted`` for placeholder dashes (a node's missing packet count / # age) that should recede below the real, muted values around them. "faint": "#64748b", + # A further step darker than ``faint``, for a meter's unlit track (the SNR quality + # bars): dark enough to read as background, not as a dimmer version of the reading. + "track": "#334155", "snr.good": "bold #4ade80", "snr.ok": "bold #fbbf24", "snr.bad": "bold #f87171", diff --git a/meshterm/ui/trace_screen.py b/meshterm/ui/trace_screen.py index c631120..49e1127 100644 --- a/meshterm/ui/trace_screen.py +++ b/meshterm/ui/trace_screen.py @@ -124,25 +124,29 @@ def snr_bar(snr: Optional[float], width: int = _BAR_WIDTH) -> Text: Packs two fill steps into each character — a half step (:data:`_BAR_HALF`) then a full step (:data:`_BAR_FULL`) — for 16-step resolution in ``width`` characters - instead of needing one cell per step. + instead of needing one cell per step. The unlit remainder draws in the same + :data:`_BAR_FULL` glyph, dimmed to the ``track`` style — a background the reading + fills in, not a run of unrelated dots. The one glyph a single colour can't split + is the boundary half-step itself: it carries the reading's colour, not the + track's, since it's still part of what was actually measured. Args: - snr: The reading in dB, or ``None`` (renders as an empty, muted track). + snr: The reading in dB, or ``None`` (renders as an entirely unlit track). width: Bar track width in characters (each worth two fill steps). Returns: - A :class:`Text` of filled braille cells over a faint dotted track, + A :class:`Text` of filled braille cells over a dark, same-glyph track, coloured by :func:`~meshterm.ui.theme.snr_style`. """ if snr is None: - return Text("·" * width, style="faint") + return Text(_BAR_FULL * width, style="track") span = _BAR_SNR_MAX - _BAR_SNR_MIN frac = min(1.0, max(0.0, (snr - _BAR_SNR_MIN) / span)) steps = max(1, round(frac * width * 2)) full, half = divmod(steps, 2) filled = _BAR_FULL * full + _BAR_HALF * half bar = Text(filled, style=snr_style(snr)) - bar.append("·" * (width - len(filled)), style="faint") + bar.append(_BAR_FULL * (width - len(filled)), style="track") return bar diff --git a/tests/test_live_screens.py b/tests/test_live_screens.py index ed8d58a..bb0b253 100644 --- a/tests/test_live_screens.py +++ b/tests/test_live_screens.py @@ -13,6 +13,7 @@ from datetime import timedelta import pytest from meshterm.core.models import Hop, TraceResult, TraceStats, TxLevelResult, TxOptResult, utcnow +from meshterm.ui.theme import snr_style from meshterm.ui.trace_screen import _BAR_FULL, _BAR_HALF, _BAR_WIDTH, TraceScreen, snr_bar from meshterm.ui.tx_screen import TxSweepScreen @@ -53,35 +54,63 @@ def _plain(lines: list[str]) -> str: # --- snr_bar ------------------------------------------------------------------- -def _filled_steps(bar: str) -> int: +def _lit_plain(bar) -> str: # noqa: ANN001 + """The reading's own coloured prefix, stripped of the dimmed unlit track. + + The unlit remainder reuses :data:`_BAR_FULL` too (dimmed to the ``track`` style + instead of a distinct glyph), so telling lit from unlit means reading which + style each character actually landed in, not just which glyph it is. + """ + if bar.style == "track": + return "" + track_spans = [s for s in bar.spans if s.style == "track"] + end = min((s.start for s in track_spans), default=len(bar.plain)) + return bar.plain[:end] + + +def _filled_steps(bar) -> int: # noqa: ANN001 """Count a rendered bar's fill steps — two per full braille cell, one per half.""" - return bar.count(_BAR_FULL) * 2 + bar.count(_BAR_HALF) + plain = _lit_plain(bar) + return plain.count(_BAR_FULL) * 2 + plain.count(_BAR_HALF) def test_snr_bar_scales_with_signal_quality() -> None: - """A stronger signal fills more of the track; None renders an empty muted track.""" - weak = snr_bar(-12.0).plain - strong = snr_bar(8.0).plain + """A stronger signal fills more of the track; None renders an entirely unlit one.""" + weak = snr_bar(-12.0) + strong = snr_bar(8.0) assert _filled_steps(weak) < _filled_steps(strong) - assert len(weak) == len(strong) == _BAR_WIDTH # the track width is constant - assert _filled_steps(snr_bar(None).plain) == 0 + assert len(weak.plain) == len(strong.plain) == _BAR_WIDTH # track width is constant + none_bar = snr_bar(None) + assert _filled_steps(none_bar) == 0 + assert none_bar.style == "track" # the whole track dims, not a separate faint dot run def test_snr_bar_clamps_out_of_range_readings() -> None: """Readings beyond the display range clamp to the ends instead of over/underflowing.""" - assert _filled_steps(snr_bar(99.0).plain) == _filled_steps(snr_bar(10.0).plain) - assert _filled_steps(snr_bar(-99.0).plain) == 1 # a heard hop always shows something + assert _filled_steps(snr_bar(99.0)) == _filled_steps(snr_bar(10.0)) + assert _filled_steps(snr_bar(-99.0)) == 1 # a heard hop always shows something def test_snr_bar_packs_two_steps_per_character() -> None: """16 steps of resolution pack into 8 characters: full cells, then one trailing half.""" - one_step = snr_bar(-13.4375).plain # frac = 1/16 of the -15..+10 span - assert one_step[0] == _BAR_HALF - assert one_step[1:] == "·" * (_BAR_WIDTH - 1) + one_step = snr_bar(-13.4375) # frac = 1/16 of the -15..+10 span + assert one_step.plain[0] == _BAR_HALF + assert one_step.plain[1:] == _BAR_FULL * (_BAR_WIDTH - 1) # unlit track, same glyph + track_spans = [(s.start, s.end) for s in one_step.spans if s.style == "track"] + assert track_spans == [(1, _BAR_WIDTH)] - three_steps = snr_bar(-10.3125).plain # frac = 3/16 → one full cell, one half - assert three_steps[:2] == _BAR_FULL + _BAR_HALF - assert three_steps[2:] == "·" * (_BAR_WIDTH - 2) + three_steps = snr_bar(-10.3125) # frac = 3/16 → one full cell, one half + assert three_steps.plain[:2] == _BAR_FULL + _BAR_HALF + assert three_steps.plain[2:] == _BAR_FULL * (_BAR_WIDTH - 2) + + +def test_snr_bar_unlit_track_dims_to_a_distinct_style() -> None: + """The unlit track renders in ``track``, not the reading's colour or old ``faint`` dots.""" + bar = snr_bar(-10.3125) + assert "·" not in bar.plain # no more plain-dot placeholder + track_span = next(s for s in bar.spans if s.style == "track") + assert bar.plain[track_span.start : track_span.end] == _BAR_FULL * (_BAR_WIDTH - 2) + assert bar.style == snr_style(-10.3125) # the lit prefix still carries the reading's colour assert snr_bar(10.0).plain == _BAR_FULL * _BAR_WIDTH # top of range: every cell full