diff --git a/meshterm/ui/timemachine_screen.py b/meshterm/ui/timemachine_screen.py index 0387f57..6f83d00 100644 --- a/meshterm/ui/timemachine_screen.py +++ b/meshterm/ui/timemachine_screen.py @@ -319,16 +319,53 @@ def _day_axis(shown: list) -> Callable[[float], str]: return label_at -def _day_columns(values: list[int], chars: int) -> list[int]: - """Stretch per-day counts into day-wide bars that fill the chart's width. +def _day_columns(values: list[int], chars: int) -> list[Optional[int]]: + """Stretch per-day counts into day-wide bars that fill the chart's width exactly. One dot column per day leaves a short history as a sliver in a wide terminal — - beneath how every other MeshTerm chart spends its width. Each day repeats over - ``2 × chars // len(values)`` columns instead (at least one), so few days read - as wide bars and a deep history falls back to the one-column-per-day density. + beneath how every other MeshTerm chart spends its width — so each day repeats + over a share of the chart's columns instead. The share is an even split with + the one-off remainder spread across the oldest days, so the total always lands + on exactly ``2 × chars`` dot columns: a plain floor division drops the + remainder instead, leaving the bars short of the axis border and caption + sized for the full width (misreading as the whole chart sitting shifted left). + + A day wide enough to span more than one character has its very first dot + column left blank — the left half of its leading braille cell — so same-height + neighbours still read as separate bars instead of fusing into one solid block. + A history deeper than the chart is wide falls back to one dot column per day + (no room for a full character each, so no notch), the dot-column total still + landing exactly on ``2 × chars``. + + Args: + values: Per-day counts, oldest first. + chars: The chart's width in character cells. + + Returns: + Exactly ``2 × chars`` dot-column readings, oldest first (``None`` marks a + day-boundary notch). """ - per_day = max(1, (chars * 2) // max(1, len(values))) - return [value for value in values for _ in range(per_day)] + n = max(1, len(values)) + out: list[Optional[int]] = [] + if n <= chars: + # Each day spans at least one whole character: split in character units so + # every day's block starts on an even dot-column index (a fresh cell), + # which is what makes "blank the leading cell's left half" well-defined. + base, extra = divmod(chars, n) + for i, value in enumerate(values): + width_chars = base + (1 if i < extra else 0) + if width_chars > 1: + out.append(None) + out.extend([value] * (width_chars * 2 - 1)) + else: + out.extend([value] * (width_chars * 2)) + else: + # More days than characters: no day gets a whole one, so no notch is drawn + # (nothing to space apart) — just split the dot columns themselves. + base, extra = divmod(chars * 2, n) + for i, value in enumerate(values): + out.extend([value] * (base + (1 if i < extra else 0))) + return out def _mesh_sections( diff --git a/tests/test_timemachine.py b/tests/test_timemachine.py index ea1c9d4..daead1b 100644 --- a/tests/test_timemachine.py +++ b/tests/test_timemachine.py @@ -15,6 +15,7 @@ from meshterm.core.models import Observation, utcnow from meshterm.persistence.repository import Repository from meshterm.ui.timemachine_screen import ( TimeMachineScreen, + _day_columns, _mesh_sections, _node_sections, _snr_cell_style, @@ -154,6 +155,41 @@ def test_snr_band_hangs_negative_readings_below_zero() -> None: assert rows[0].spans[1].style == _snr_cell_style([-4.0, -8.0]) +def test_day_columns_always_hits_the_exact_width() -> None: + """The stretched output always lands on exactly 2 × chars, remainder included. + + A plain floor division (the old implementation) drops the remainder, leaving + the bars short of the axis border and caption sized for the full width — the + chart reading as shifted left of where its own axis says it ends. + """ + for n, chars in [(7, 92), (3, 10), (1, 20), (14, 40), (30, 45), (60, 30)]: + cols = _day_columns(list(range(n)), chars) + assert len(cols) == chars * 2, (n, chars) + + +def test_day_columns_notches_the_first_dot_of_a_multi_character_day() -> None: + """A day wide enough to span several characters opens with a blank dot column.""" + cols = _day_columns([2, 8, 4], 12) # 3 days, 12 chars -> 4 chars (8 dots) each + assert cols[0] is None and cols[8] is None and cols[16] is None + assert cols[1:8] == [2] * 7 + assert cols[9:16] == [8] * 7 + assert cols[17:24] == [4] * 7 + + +def test_day_columns_skips_the_notch_for_a_single_character_day() -> None: + """A day exactly one character wide stays fully lit — nothing to space apart.""" + cols = _day_columns([5, 9], 2) # 2 days, 2 chars -> exactly 1 char (2 dots) each + assert None not in cols + assert cols == [5, 5, 9, 9] + + +def test_day_columns_skips_notches_when_days_outnumber_characters() -> None: + """More days than character columns: every day is sub-character, no notches.""" + cols = _day_columns(list(range(50)), 30) # 50 days into 30 chars (<=60 dots) + assert len(cols) == 60 + assert None not in cols + + # --- the pages -------------------------------------------------------------------------- @@ -195,6 +231,24 @@ def test_mesh_page_renders_days_rhythm_arrivals_and_ledger(tmp_path: Path) -> No repo.close() +def test_mesh_page_day_chart_axis_matches_the_bar_width(tmp_path: Path) -> None: + """A day chart's bottom border spans exactly as many columns as its bars. + + A rounding mismatch here (the old ``_day_columns``) leaves the border and + caption sized for a wider chart than the bars actually drawn — the bars + reading as shifted left of an axis drawn for more columns than exist. + """ + repo = _seeded_repo(tmp_path) + ctx = SimpleNamespace(repo=repo) + lines = _plain(_mesh_sections(ctx, None, 90), width=90).split("\n") + border_idx = next(i for i, line in enumerate(lines) if re.fullmatch(r"\s*└─+┘", line)) + border_dashes = lines[border_idx].count("─") + row_line = lines[border_idx - 1] + content = re.search(r"[┤│](.*?)[├│]", row_line).group(1) + assert len(content) == border_dashes + repo.close() + + def test_picker_row_lanes_align_under_the_header() -> None: """Picker rows lane up under the header; unknown nodes read as heat-coloured names.""" from meshterm.core.models import HeardNode