Braille chart widget: one timeline renderer, now on the right, grey = zero

A new ui.braillechart module every value-over-time row will draw through,
carrying three app-wide rules: time flows left-to-right (the rightmost
column is now), a faint grey baseline always marks value zero (mid-chart
when a series goes negative, so positives rise and negatives hang), and
both braille dot columns are used for two readings per character cell.
Existing charts are rewired onto it in follow-up commits.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Jean-Pierre MartineauandClaude Fable 5 committed 2026-07-11 14:50:23 -04:00
1 parent d75a9aa2db
commit 219f121625
2 files changed
+398

No files matched your search

+249
View File
@@ -0,0 +1,249 @@
"""Scrolling braille bar charts: the one way MeshTerm draws a value-over-time row.
Every timeline in the app — the header's activity pulse, the channel manager's
per-channel sparklines, the dashboard's tall packet chart, the Time Machine's
histories — renders through this module, so they all share the same three rules:
* **Time flows left → right.** The rightmost column is *now*; history trails away
to the left. Chronological (oldest-first) series feed :func:`timeline_rows`
directly; the newest-first histograms the monitor and repository keep are passed
as-is to :func:`activity_sparkline`, which reverses them into place.
* **Grey is zero.** Every chart draws a faint one-dot baseline at value zero and
bars grow *from that line*, so a silent stretch reads as a flatline, never a
hole. When a series carries negative values (an SNR history, say) the baseline
sits at zero's height inside the chart — positive readings rise above it,
negative ones hang below — rather than being nailed to the chart floor.
* **Two readings per cell.** Braille offers two dot columns per character cell;
every chart uses both, so each cell shows two consecutive readings and the
chart draws at twice the horizontal resolution of the cells it occupies.
The low-level cell assembly is shared: bars are expressed as inclusive dot-row
spans anchored on the baseline, and each character cell takes the bar style when
any bar dot falls in it, the faint baseline style when only the zero line does,
and stays blank braille otherwise (blank braille, not a space, so the grid stays
monospace under fonts with slightly odd braille metrics).
"""
from __future__ import annotations
from bisect import bisect_right
from typing import Callable, Optional, Sequence, Union
from rich.text import Text
#: Braille dot bit for each dot row counted from the *bottom* of a cell (row 0 is
#: the cell's lowest dot), left and right columns. The Unicode braille block
#: numbers its rows top-down (dots 1,2,3,7 left / 4,5,6,8 right); these tables
#: flip that so chart math can stay in bottom-up "height" space throughout.
_LEFT_BITS = (0x40, 0x04, 0x02, 0x01)
_RIGHT_BITS = (0x80, 0x20, 0x10, 0x08)
#: How a lit cell is styled: a fixed Rich style name, or a callable given the
#: cell's present readings (one or two values) returning the style — the hook the
#: Time Machine uses to colour an SNR band by each slice's quality.
CellStyle = Union[str, Callable[[list[float]], str]]
def chart_span(
values: Sequence[Optional[float]], span: Optional[tuple[float, float]] = None
) -> tuple[float, float]:
"""The vertical range a chart of ``values`` draws over, zero always included.
The grey baseline *is* zero, so the span is the data's extent folded around it:
all-positive data spans ``0 → peak`` (the baseline on the floor), all-negative
data ``floor → 0`` (the baseline on the ceiling, bars hanging), mixed data both.
Callers that caption their chart's scale should quote this same span so the
label and the drawing can never disagree.
Args:
values: The chart's readings (``None`` marks an empty slot).
span: An optional wider range to honour (it too is folded around zero).
Returns:
``(lo, hi)`` with ``lo <= 0 <= hi``; ``(0.0, 0.0)`` for an empty series.
"""
present = [v for v in values if v is not None]
if span is not None:
present = [*present, *span]
lo = min([0.0, *present])
hi = max([0.0, *present])
return lo, hi
def timeline_rows(
values: Sequence[Optional[float]],
*,
rows: int = 1,
span: Optional[tuple[float, float]] = None,
style: CellStyle = "ok",
baseline_style: str = "faint",
) -> list[Text]:
"""Render a chronological series as a braille bar chart, newest at the right.
``values`` is oldest-first, one reading per dot column (two per character
cell), scaled onto :func:`chart_span`'s zero-folded range: the series' peak
fills the space above the baseline, its floor the space below, and any
non-zero reading lights at least one dot so a lone packet never vanishes.
``None`` (no reading) and ``0`` alike draw only the faint zero baseline.
Args:
values: Per-slot readings, oldest first (the rightmost is "now"). An odd
count is padded with one silent column so whole cells always render.
rows: How many braille rows tall the chart is (four dot rows each).
span: A wider range to scale against (see :func:`chart_span`), so several
charts — or a chart and its caption — can share one scale.
style: Style for lit cells: a Rich style name, or a callable given each
cell's present readings (for per-slice colouring).
baseline_style: Style for the zero line where nothing covers it.
Returns:
``rows`` :class:`Text` lines, top row first, ``ceil(len(values)/2)``
characters wide.
"""
lo, hi = chart_span(values, span)
total = rows * 4
base = _baseline_row(lo, hi, total)
up = total - base # dot rows available to a full-scale positive bar
down = base + 1 # …and to a full-scale negative one (baseline row included)
bars: list[Optional[tuple[int, int]]] = []
for value in values:
if value is None or value == 0:
bars.append(None)
elif value > 0:
height = max(1, round(value / hi * up))
bars.append((base, base + height - 1))
else:
height = max(1, round(value / lo * down))
bars.append((base - height + 1, base))
return _assemble(bars, list(values), rows, base, style, baseline_style)
def activity_sparkline(
histogram: Sequence[int],
levels: tuple[int, ...],
buckets: int,
*,
style: str = "ok",
) -> Text:
"""A one-row activity sparkline over a newest-first histogram, "now" rightmost.
Bar heights are absolute, stepped at ``levels`` (the count a bucket must reach
for each extra dot), so the same traffic always draws the same bar regardless
of what else is on screen and a lone packet never vanishes. The histogram
arrives newest-first — the natural order the monitor and repository keep — and
is reversed here, so the current bucket lands on the right edge and traffic
slides *left* as it ages, like every other MeshTerm timeline.
Args:
histogram: Per-bucket counts, newest first; padded/cropped to ``buckets``.
levels: Ascending count thresholds for bar heights 1..4.
buckets: How many buckets to draw (half this many characters).
style: Style for lit cells.
Returns:
A styled Rich :class:`Text` of ``buckets / 2`` braille characters.
"""
window = (tuple(histogram) + (0,) * buckets)[:buckets]
bars: list[Optional[tuple[int, int]]] = []
for count in reversed(window):
height = bisect_right(levels, count)
bars.append((0, height - 1) if height else None)
return _assemble(bars, [float(c) for c in reversed(window)], 1, 0, style, "faint")[0]
def _baseline_row(lo: float, hi: float, total: int) -> int:
"""The dot row (from the chart bottom) the zero baseline sits on.
All-positive data pins it to the floor, all-negative to the ceiling; a mixed
span places it proportionally, clamped one row in from either edge so both
directions keep at least one dot row to draw in.
Args:
lo: The span's floor (``<= 0``).
hi: The span's ceiling (``>= 0``).
total: The chart's height in dot rows.
Returns:
The baseline's dot row, ``0 .. total - 1``.
"""
if lo == 0:
return 0
if hi == 0:
return total - 1
return min(total - 2, max(1, round((total - 1) * -lo / (hi - lo))))
def _column_bits(bar: Optional[tuple[int, int]], floor: int, table: tuple[int, ...]) -> int:
"""The braille bits one column's bar lights within a cell row.
Args:
bar: The bar's inclusive dot-row span (chart coordinates), or ``None``.
floor: The cell row's bottom dot row in chart coordinates.
table: :data:`_LEFT_BITS` or :data:`_RIGHT_BITS`.
Returns:
The OR of the covered dots' bits (0 when the bar misses this cell row).
"""
if bar is None:
return 0
lo, hi = max(bar[0], floor), min(bar[1], floor + 3)
bits = 0
for dot in range(lo, hi + 1):
bits |= table[dot - floor]
return bits
def _assemble(
bars: list[Optional[tuple[int, int]]],
values: list[Optional[float]],
rows: int,
base: int,
style: CellStyle,
baseline_style: str,
) -> list[Text]:
"""Assemble bar spans into styled braille rows (the shared cell walk).
Every bar span includes the baseline row by construction, so a half-silent
cell keeps the zero line continuous inside the lit character; a fully silent
cell shows just the faint baseline dots on whichever row holds them; anything
else stays blank braille to keep the grid monospace.
Args:
bars: Per-column inclusive dot-row spans (``None`` = no bar).
values: The readings behind the columns, aligned with ``bars`` (fed to a
callable ``style`` two at a time, per cell).
rows: Chart height in braille rows.
base: The zero baseline's dot row.
style: Fixed style, or per-cell callable (see :data:`CellStyle`).
baseline_style: Style for baseline-only cells.
Returns:
``rows`` :class:`Text` lines, top row first.
"""
if len(bars) % 2:
bars = [*bars, None]
values = [*values, None]
lines: list[Text] = []
for row in range(rows):
floor = (rows - 1 - row) * 4
base_bit_left = _LEFT_BITS[base - floor] if floor <= base <= floor + 3 else 0
base_bit_right = _RIGHT_BITS[base - floor] if floor <= base <= floor + 3 else 0
line = Text()
for i in range(0, len(bars), 2):
left = _column_bits(bars[i], floor, _LEFT_BITS)
right = _column_bits(bars[i + 1], floor, _RIGHT_BITS)
if left or right:
mask = left | right | base_bit_left | base_bit_right
if callable(style):
present = [v for v in values[i : i + 2] if v is not None]
cell_style = style(present) if present else baseline_style
else:
cell_style = style
line.append(chr(0x2800 | mask), style=cell_style)
elif base_bit_left:
line.append(chr(0x2800 | base_bit_left | base_bit_right), style=baseline_style)
else:
line.append(chr(0x2800))
lines.append(line)
return lines
+149
View File
@@ -0,0 +1,149 @@
"""Braille chart widget tests: orientation, the zero baseline, scaling, and styles.
The module is pure Text-in/Text-out, so every behaviour is asserted straight off
the rendered braille bitmasks.
"""
from __future__ import annotations
from meshterm.ui.braillechart import activity_sparkline, chart_span, timeline_rows
#: Dot bits for a column filled bottom-up to height 0–4 (left / right column).
_L = (0x00, 0x40, 0x44, 0x46, 0x47)
_R = (0x00, 0x80, 0xA0, 0xB0, 0xB8)
_BASE = chr(0x2800 | 0x40 | 0x80) # the two-bottom-dot zero flatline
_BLANK = chr(0x2800)
def _ch(*bits: int) -> str:
mask = 0
for b in bits:
mask |= b
return chr(0x2800 | mask)
# --- chart_span ---------------------------------------------------------------------
def test_chart_span_always_folds_zero_in() -> None:
"""Positive, negative, and mixed series all keep zero inside the span."""
assert chart_span([3, 7]) == (0.0, 7.0)
assert chart_span([-3, -7]) == (-7.0, 0.0)
assert chart_span([-3, 7]) == (-3.0, 7.0)
assert chart_span([None, None]) == (0.0, 0.0)
assert chart_span([1], span=(-5, 10)) == (-5.0, 10.0)
# --- timeline_rows: all-positive series ------------------------------------------------
def test_timeline_peak_fills_the_height() -> None:
"""The series' peak fills every dot row; readings pair up two per cell."""
rows = timeline_rows([12, 12, 0, 0], rows=3)
assert len(rows) == 3
assert all(len(r.plain) == 2 for r in rows)
assert rows[0].plain[0] == _ch(_L[4], _R[4]) # the peak pair tops out
assert rows[0].plain[1] == _BLANK
assert rows[2].plain[1] == _BASE # the silent pair keeps the zero line
def test_timeline_each_dot_column_has_its_own_height() -> None:
"""Two readings sharing a cell rise independently — braille's full resolution."""
rows = timeline_rows([12, 6], rows=3)
assert rows[0].plain[0] == _ch(_L[4])
assert rows[1].plain[0] == _ch(_L[4], _R[2])
assert rows[2].plain[0] == _ch(_L[4], _R[4])
def test_timeline_never_hides_a_lone_packet() -> None:
"""A tiny non-zero value still lights a dot, in the lit style not the floor's."""
rows = timeline_rows([1, 0, 1000, 1000], rows=3)
bottom = rows[2]
assert bottom.plain[0] == _ch(_L[1], _R[1]) # bar dot + the baseline continuing
assert bottom.spans[0].style == "ok"
def test_timeline_all_silent_is_a_flatline() -> None:
"""With nothing to draw the chart is a faint floor line, not a hole."""
rows = timeline_rows([0, 0, 0, 0, 0, 0], rows=2)
assert rows[1].plain == _BASE * 3
assert rows[0].plain == _BLANK * 3
assert all(span.style == "faint" for span in rows[1].spans)
def test_timeline_pads_an_odd_tail_column() -> None:
"""An odd reading count still renders whole cells, the floor continuing under it."""
rows = timeline_rows([4], rows=1)
assert len(rows[0].plain) == 1
assert rows[0].plain[0] == _ch(_L[4], _R[1]) # full bar + the padded column's floor
# --- timeline_rows: negative and mixed series ------------------------------------------
def test_timeline_negative_series_hangs_from_a_top_baseline() -> None:
"""All-negative data pins zero to the ceiling and bars grow downward."""
rows = timeline_rows([-4, -2], rows=1)
# Left column full hang (0..3); right hangs two dots (2..3); the baseline row
# (the cell's top dots) is inside both bars.
assert rows[0].plain[0] == _ch(0x40, 0x04, 0x02, 0x01, 0x10, 0x08)
def test_timeline_mixed_series_puts_zero_mid_chart() -> None:
"""With readings both sides of zero, positives rise and negatives hang."""
rows = timeline_rows([-10, 5], rows=3, span=(-10, 10))
# Baseline lands on dot row 6: the full -10 hangs 0..6, the +5 rises 6..8.
assert rows[0].plain[0] == _ch(0x80) # the rise's head, above the baseline
assert rows[1].plain[0] == _ch(0x46, 0x18) # hang and rise straddle the zero row
assert rows[2].plain[0] == _ch(0x47) # the hang's tail, below everything
def test_timeline_baseline_shows_mid_chart_in_silent_cells() -> None:
"""A silent cell of a mixed-span chart draws the zero line where zero *is*."""
rows = timeline_rows([-10, 5, None, None], rows=3, span=(-10, 10))
assert rows[1].plain[1] == _ch(0x02, 0x10) # both columns' dot on row 6, faint
assert rows[1].spans[-1].style == "faint"
assert rows[0].plain[1] == _BLANK
assert rows[2].plain[1] == _BLANK
# --- styles -----------------------------------------------------------------------------
def test_timeline_per_cell_style_callable_sees_the_cells_readings() -> None:
"""A callable style is fed each lit cell's present readings."""
seen: list[list[float]] = []
def style(values: list[float]) -> str:
seen.append(values)
return "warn"
rows = timeline_rows([2, 4, None, 6], rows=1, style=style)
assert seen == [[2, 4], [6]]
assert all(span.style == "warn" for span in rows[0].spans)
# --- activity_sparkline -------------------------------------------------------------------
def test_sparkline_puts_now_on_the_right() -> None:
"""A newest-first histogram renders with the current bucket at the right edge."""
# Newest bucket maxed, everything older silent: only the final cell's *right*
# dot column lights (with the floor dot keeping the left column's baseline).
text = activity_sparkline((21, 0, 0, 0), (1, 3, 8, 21), 4)
assert text.plain == _BASE + _ch(_R[4], 0x40)
def test_sparkline_thresholds_step_the_bar_heights() -> None:
"""The level shoulders map counts to 1–4 dots, oldest to the left."""
# Newest-first (1, 3, 8, 21) reads 21,8,3,1 left→right once flipped.
text = activity_sparkline((1, 3, 8, 21), (1, 3, 8, 21), 4)
assert text.plain == _ch(_L[4], _R[3]) + _ch(_L[2], _R[1])
def test_sparkline_pads_and_crops_to_the_window() -> None:
"""A short histogram pads with silence; a long one crops to the newest buckets."""
padded = activity_sparkline((21,), (1, 3, 8, 21), 6)
assert padded.plain == _BASE * 2 + _ch(_R[4], 0x40)
cropped = activity_sparkline((0, 0, 21, 21, 9, 9), (1, 3, 8, 21), 2)
assert cropped.plain == _BASE # only the two newest (silent) buckets survive