fix(v5): load plugin — align corecount with load-average values

The header used '{:3}core' (7 chars) while load-average values used
'{:>6.2f}' (6 chars), so '16core' overhung the right edge by 1
character compared to '0.96'/'0.81'/'0.83'. Drop the int padding and
rjust the whole corecount cell to the same width as the value cells
(6 chars), keeping right edges aligned across all four rows of the
block.

Two new tests pin the invariant: every value cell shares the same
width, and every rendered line has the same total width.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
nicolargo
2026-05-13 15:33:08 +02:00
parent 2090888370
commit a49b95acd8
2 changed files with 22 additions and 2 deletions

View File

@@ -73,11 +73,15 @@ def render(payload: dict[str, Any], fields_desc: dict[str, dict[str, Any]]) -> l
return [Row(cells=[Cell(text="LOAD", color=ColorRole.HEADER)])]
# Line 1: title + cpucore suffix.
# The value cell width must match the body load-average value width
# (`_LOAD_VALUE_WIDTH = 6`) so the right edges of every line in the
# block align. v4 padded the int with `{:3}core` (7 chars), which
# made the corecount cell 1 char wider than the load-average values
# and produced a visible 1-char overhang.
header_cells: list[Cell] = [Cell(text="LOAD".ljust(_LOAD_LABEL_WIDTH), color=ColorRole.HEADER)]
cores = payload.get("cpucore")
if isinstance(cores, (int, float)) and cores > 0:
# v4 format: `{:3}core` — `' 4core'` for 4 cores.
header_cells.append(Cell(text=f"{int(cores):>3}core".rjust(_LOAD_VALUE_WIDTH)))
header_cells.append(Cell(text=f"{int(cores)}core".rjust(_LOAD_VALUE_WIDTH)))
else:
header_cells.append(Cell(text="".rjust(_LOAD_VALUE_WIDTH)))
rows: list[Row] = [Row(cells=header_cells)]

View File

@@ -130,3 +130,19 @@ def test_render_columns_align(load_payload, load_fields):
rows = render(load_payload, load_fields)
label_widths = {len(r.cells[0].text) for r in rows if r.cells}
assert len(label_widths) == 1
def test_render_value_cells_share_width_across_header_and_body(load_payload, load_fields):
"""The corecount cell (header) and the load-average cells (body) must
have the same width so right edges align. Earlier bug: `{:3}core`
produced 7-char header value vs 6-char body values → 1-char overhang."""
rows = render(load_payload, load_fields)
widths = {len(r.cells[1].text) for r in rows if len(r.cells) >= 2}
assert len(widths) == 1, f"value cells not uniform: {widths}"
def test_render_total_line_width_matches_across_rows(load_payload, load_fields):
"""Each line's total rendered width (cells joined with 1 space) is identical."""
rows = render(load_payload, load_fields)
totals = {sum(len(c.text) for c in r.cells) + max(0, len(r.cells) - 1) for r in rows}
assert len(totals) == 1, f"line widths differ: {totals}"