From 12188afaa066fda3c41922ba44c088e367d3b81a Mon Sep 17 00:00:00 2001 From: Jean-Pierre Martineau Date: Fri, 10 Jul 2026 01:51:59 -0400 Subject: [PATCH] Light every frame from its top-left corner A new glow pass post-processes composed ANSI lines: it finds each box's top-left corner glyph, lifts it toward white, and fades the highlight quadratically along the top and left edges into the border's own colour. It reads colours from the SGR stream itself, so accent, muted, and warn frames each glow in their own hue and nested tool panels are lit individually with no per-callsite wiring. String surgery instead of a Rich parse/re-render round-trip keeps the pass ~2ms per repaint. Replaces render_to_ansi_dialog, whose only caller now composes through the glow. Co-Authored-By: Claude Fable 5 --- meshterm/ui/tui/frame.py | 16 +- meshterm/ui/tui/glow.py | 308 +++++++++++++++++++++++++++++++++++++++ tests/test_tui.py | 77 +++++++++- 3 files changed, 390 insertions(+), 11 deletions(-) create mode 100644 meshterm/ui/tui/glow.py diff --git a/meshterm/ui/tui/frame.py b/meshterm/ui/tui/frame.py index eb62877..e268eff 100644 --- a/meshterm/ui/tui/frame.py +++ b/meshterm/ui/tui/frame.py @@ -15,6 +15,7 @@ from rich.panel import Panel from rich.text import Text from ..theme import hint_style, title_style +from .glow import apply_corner_glow from .render import render_lines from .screen import Screen @@ -151,7 +152,8 @@ def compose_base( viewport = max(1, rows - header_h - 1 - 2) # minus footer(1) and panel border(2) panel = _panel(base, cols - 4, viewport, active=True) footer = Text.from_markup(f"[muted]{footer_hint}[/muted]") - lines = header_lines + render_lines(Group(panel, footer), cols) + # The glow pass lights the outer frame *and* any tool panels nested in the body. + lines = header_lines + apply_corner_glow(render_lines(Group(panel, footer), cols)) # Guarantee we never exceed the terminal height (pt would otherwise clip unpredictably). if len(lines) > rows: lines = lines[:rows] @@ -245,7 +247,8 @@ def compose_startup(screen: Screen, cols: int, rows: int) -> str: padding=(vpad, 1), width=inner_w + 4, ) - panel_lines = _center(render_lines(panel, inner_w + 4), cols) + # Glow before centering, while the box still starts at column 0 of its own lines. + panel_lines = _center(apply_corner_glow(render_lines(panel, inner_w + 4)), cols) # A small muted line (e.g. a copyright notice) sits a blank row below the box. footnote_lines: list[str] = [] @@ -299,11 +302,4 @@ def compose_dialog(screen: Screen, cols: int, rows: int) -> str: padding=(vpad, 1), width=max_w, ) - return render_to_ansi_dialog(panel, max_w) - - -def render_to_ansi_dialog(panel: Panel, width: int) -> str: - """Render a dialog panel to ANSI at a fixed width (thin wrapper for clarity).""" - from .render import render_to_ansi - - return render_to_ansi(panel, width) + return "\n".join(apply_corner_glow(render_lines(panel, max_w))) diff --git a/meshterm/ui/tui/glow.py b/meshterm/ui/tui/glow.py new file mode 100644 index 0000000..ef550d4 --- /dev/null +++ b/meshterm/ui/tui/glow.py @@ -0,0 +1,308 @@ +"""A corner-light pass for framed boxes: brighten each frame's top-left, fading to normal. + +Every panel the TUI draws — the full-screen base frame, floating dialogs, the startup +splash box, and the content-sized tool panels nested *inside* screen bodies (Status, Link +budget, Mesh, ...) — is bordered with Rich's box glyphs. This module post-processes +composed ANSI lines and gives each such frame a subtle lighting treatment: the top-left +corner is lifted toward white, and the highlight decays along the top edge (rightward) and +the left edge (downward) until it meets the border's own, untouched colour. The right and +bottom edges stay as drawn, so the box reads as lit from the top-left. + +The pass is generic on purpose. It does not know which panels exist or what colour each +border uses: it *finds* frames by their corner glyphs and reads every border cell's current +colour from the ANSI stream itself, blending that toward white. A muted box therefore gets +a muted glow, a warn box a warm one — and nested panels are lit individually, each from its +own corner — with no per-callsite wiring. Composers apply it exactly once, after layout +(see :func:`~meshterm.ui.tui.frame.compose_base` and friends), so the effect never +compounds. + +Because it runs on every repaint, the pass works on the ANSI strings directly rather than +round-tripping through Rich (parse → restyle → re-render costs ~10ms a frame; string +surgery is well under 1ms). The render console emits truecolor SGR sequences for every +themed colour, so a tiny state machine tracking ``38;2;r;g;b`` foregrounds is enough to +know each border cell's colour; the recolour splices in an override before the glyph and +restores the tracked colour after it, leaving bold/dim attributes and everything else in +the stream untouched. Title and subtitle text embedded in a border row is skipped (only +box-drawing glyphs are recoloured). +""" + +from __future__ import annotations + +import re +from itertools import accumulate + +from rich.cells import cell_len + +#: Glyphs that open a frame's top edge. Rich's panels use the ROUNDED box (``╭``) — or its +#: SQUARE substitute (``┌``) on legacy Windows consoles; the heavy/double corners are +#: accepted too so a restyled box would still glow. +_TOP_LEFT = frozenset("╭┌┏╔") + +#: Glyphs that close a top edge. The nearest one right of a top-left corner on the same row +#: marks the frame's width, which normalises the horizontal fade. +_TOP_RIGHT = frozenset("╮┐┓╗") + +#: Glyphs that close a left edge. Finding one directly below a top-left corner marks the +#: frame's height, which normalises the vertical fade. +_BOTTOM_LEFT = frozenset("╰└┗╚") + +#: Border glyphs a top edge is drawn with. Anything else on the row (a title, its +#: surrounding spaces) is left untouched, so only the frame itself is recoloured. +_HORIZONTAL = frozenset("─━═") + +#: Border glyphs a left edge is drawn with, walked downward from each top-left corner. +_VERTICAL = frozenset("│┃║") + +#: How far the corner cell itself is lifted toward pure white (0 = untouched, 1 = white). +#: The fade multiplies this by a per-cell weight, so every other border cell gets less. +_CORNER_LIFT = 0.8 + +#: Weights below this leave the cell alone — the blend would be invisible, and skipping it +#: keeps far border cells byte-identical to their un-glowed rendering. +_MIN_WEIGHT = 0.02 + +#: One SGR escape sequence (the only kind the render console emits); group 1 is its +#: semicolon-separated parameter list. +_SGR = re.compile(r"\x1b\[([0-9;]*)m") + +#: Finds top-left corner glyphs in a plain (escape-free) row at C speed. +_CORNER = re.compile(f"[{''.join(_TOP_LEFT)}]") + + +def _strip(line: str) -> str: + """Return the line's plain text with all SGR escape sequences removed.""" + return _SGR.sub("", line) + + +def _weight(distance: int, extent: int) -> float: + """Return the highlight weight for a border cell ``distance`` cells from the corner. + + The fade is quadratic — ``(1 - t)²`` over the edge's ``extent`` — so the glow + concentrates near the corner and melts into the border's normal colour well before the + far end, rather than tinting the whole edge half-bright. + + Args: + distance: Cells travelled along the edge from the top-left corner (0 = the corner). + extent: The edge's full length in cells; the weight reaches 0 here. + + Returns: + A weight in [0, 1] to scale :data:`_CORNER_LIFT` by. + """ + if extent <= 0: + return 0.0 + t = min(1.0, distance / extent) + return (1.0 - t) ** 2 + + +def _lift(rgb: tuple[int, int, int], amount: float) -> tuple[int, int, int]: + """Blend an RGB colour toward white by ``amount`` (0 = unchanged, 1 = pure white).""" + r, g, b = rgb + return ( + round(r + (255 - r) * amount), + round(g + (255 - g) * amount), + round(b + (255 - b) * amount), + ) + + +def _columns(plain: str) -> list[int]: + """Return each character's display column (a cumulative cell-width prefix sum). + + Border glyphs are single-cell, but body text earlier in a row may hold double-width + characters; walking columns rather than indices keeps the vertical edge walk aligned + under its corner regardless. + """ + return [0, *accumulate(cell_len(ch) for ch in plain)] + + +def _index_at_column(columns: list[int], column: int) -> int | None: + """Return the index of the character at display ``column``, or ``None`` if none lands there. + + A wide character straddling the column (so nothing *starts* there) returns ``None``, + which simply ends the edge walk — a frame's border cells always start on their column. + """ + for idx in range(len(columns) - 1): + if columns[idx] == column: + return idx + if columns[idx] > column: + return None + return None + + +def _advance_fg(fg: tuple[int, int, int] | None, params: str) -> tuple[int, int, int] | None: + """Track the foreground colour across one SGR sequence's parameters. + + Only truecolor foregrounds (``38;2;r;g;b``) are resolved — the render console emits + every themed colour that way. A reset (``0`` or empty), a default (``39``), or any + other foreground form yields ``None``, and cells whose colour is unknown are simply not + glowed rather than guessed at. + + Args: + fg: The foreground in effect before this sequence, or ``None`` if unknown/default. + params: The sequence's raw parameter list (the text between ``ESC[`` and ``m``). + + Returns: + The foreground in effect after the sequence. + """ + parts = params.split(";") if params else [""] + i = 0 + while i < len(parts): + code = parts[i] or "0" + if code == "0" or code == "39": + fg = None + elif code == "38": + if parts[i + 1 : i + 2] == ["2"] and i + 4 < len(parts): + try: + fg = (int(parts[i + 2]), int(parts[i + 3]), int(parts[i + 4])) + except ValueError: # pragma: no cover - malformed sequence + fg = None + i += 5 + continue + fg = None # an 8-bit (38;5;n) or malformed foreground: unknown, skip its cells + i += 2 if parts[i + 1 : i + 2] == ["5"] else 1 + elif code == "48": + # A background colour: skip its components so they aren't misread as codes. + if parts[i + 1 : i + 2] == ["2"]: + i += 5 + elif parts[i + 1 : i + 2] == ["5"]: + i += 2 + elif code.isdigit() and (30 <= int(code) <= 37 or 90 <= int(code) <= 97): + fg = None # a named 16-colour foreground; the themed borders never use these + i += 1 + return fg + + +def _recolor_line(line: str, targets: list[tuple[int, float]]) -> str: + """Splice colour overrides into one ANSI line at the given plain-text indices. + + Walks the line's SGR sequences to track the active foreground, and wraps each target + character in a lifted-colour override followed by a restore of the tracked colour. + Targets falling where the foreground is unknown are left untouched. + + Args: + line: The original ANSI line. + targets: ``(plain_index, weight)`` pairs, the border cells to brighten. + + Returns: + The line with the highlight spliced in. + """ + wanted = dict(sorted(targets)) + out: list[str] = [] + fg: tuple[int, int, int] | None = None + plain_pos = 0 + cursor = 0 + + def emit(text: str) -> None: + """Append a raw text run, recolouring any target characters inside it.""" + nonlocal plain_pos + start = plain_pos + plain_pos += len(text) + if fg is None: + out.append(text) + return + base = 0 + for idx, weight in wanted.items(): + rel = idx - start + if rel < base or rel >= len(text): + continue + r, g, b = _lift(fg, _CORNER_LIFT * weight) + out.append(text[base:rel]) + out.append(f"\x1b[38;2;{r};{g};{b}m{text[rel]}\x1b[38;2;{fg[0]};{fg[1]};{fg[2]}m") + base = rel + 1 + out.append(text[base:]) + + for match in _SGR.finditer(line): + emit(line[cursor : match.start()]) + out.append(match.group(0)) + fg = _advance_fg(fg, match.group(1)) + cursor = match.end() + emit(line[cursor:]) + return "".join(out) + + +class _Glower: + """One glow pass over a block of composed lines (find frames → splice highlights).""" + + def __init__(self, lines: list[str]) -> None: + self.lines = lines + self.plains = [_strip(line) for line in lines] + #: Per-row display-column prefix sums, computed lazily — only walked rows need one. + self._column_cache: dict[int, list[int]] = {} + #: The border cells to brighten: row → ``(plain_index, weight)`` pairs. + self.targets: dict[int, list[tuple[int, float]]] = {} + + def _cols(self, row: int) -> list[int]: + """Return (and cache) the column prefix sums for ``row``.""" + cols = self._column_cache.get(row) + if cols is None: + cols = self._column_cache[row] = _columns(self.plains[row]) + return cols + + def apply(self) -> list[str]: + """Find every top-left corner and light its frame; return the updated lines.""" + for row, plain in enumerate(self.plains): + for match in _CORNER.finditer(plain): + self._glow_frame(row, match.start()) + if not self.targets: + return self.lines + out = list(self.lines) + for row, targets in self.targets.items(): + out[row] = _recolor_line(self.lines[row], targets) + return out + + def _glow_frame(self, row: int, corner: int) -> None: + """Light one frame from its top-left corner at ``(row, corner)``.""" + plain = self.plains[row] + columns = self._cols(row) + + # The matching ╮ bounds the top edge; a clipped frame falls back to the row's end. + closing = next((j for j in range(corner + 1, len(plain)) if plain[j] in _TOP_RIGHT), None) + end = closing if closing is not None else len(plain) + extent = max(1, columns[end] - columns[corner]) + for idx in range(corner, end): + ch = plain[idx] + if idx != corner and ch not in _HORIZONTAL: + continue # title text (and its padding) keeps its own style + self._mark(row, idx, _weight(columns[idx] - columns[corner], extent)) + + # Walk the left edge downward until the ╰ that closes it (or the block's clipped + # end), then fade over that height. The bottom corner lands at weight 0 by design. + column = columns[corner] + edge: list[tuple[int, int]] = [] + closed = False + for below in range(row + 1, len(self.plains)): + idx = _index_at_column(self._cols(below), column) + if idx is None: + break + ch = self.plains[below][idx] + if ch in _BOTTOM_LEFT: + closed = True + break + if ch not in _VERTICAL: + break + edge.append((below, idx)) + height = len(edge) + 1 if closed else len(edge) + 2 + for depth, (below, idx) in enumerate(edge, start=1): + self._mark(below, idx, _weight(depth, height)) + + def _mark(self, row: int, idx: int, weight: float) -> None: + """Record one border cell to brighten (skipping invisibly small weights).""" + if weight < _MIN_WEIGHT: + return + self.targets.setdefault(row, []).append((idx, weight)) + + +def apply_corner_glow(lines: list[str]) -> list[str]: + """Light every frame in ``lines`` from its top-left corner. + + Scans the composed ANSI lines for box top-left corners, then blends each frame's border + colour toward white — strongest at the corner, fading quadratically along the top and + left edges to the border's normal colour. Body text, titles, and the right/bottom edges + are untouched. Rows without recoloured cells are returned byte-identical. + + Args: + lines: Composed ANSI lines (one per terminal row, no newlines). + + Returns: + The lines with the corner highlight applied. + """ + return _Glower(lines).apply() diff --git a/tests/test_tui.py b/tests/test_tui.py index 08619e9..b9296a2 100644 --- a/tests/test_tui.py +++ b/tests/test_tui.py @@ -12,10 +12,13 @@ import asyncio from prompt_toolkit.input.defaults import create_pipe_input from prompt_toolkit.output import DummyOutput +from rich.console import Group +from rich.panel import Panel from rich.table import Table from rich.text import Text -from meshterm.ui.tui import frame +from meshterm.ui.tui import frame, glow +from meshterm.ui.tui.glow import apply_corner_glow from meshterm.ui.tui.progress import ProgressScreen from meshterm.ui.tui.prompt import ( AutocompleteScreen, @@ -374,6 +377,78 @@ def test_compose_startup_box_is_horizontally_centered() -> None: assert box_lines and all(ln.startswith(" ") for ln in box_lines) # centered inset +# --- glow -------------------------------------------------------------------- + + +def _cell_color(line: str, idx: int) -> tuple[int, int, int]: + """Resolve the foreground RGB of the character at ``idx`` in an ANSI line.""" + from meshterm.ui.tui.render import _console + + style = Text.from_ansi(line).get_style_at_offset(_console(80), idx) + assert style.color is not None + triplet = style.color.get_truecolor() + return (triplet.red, triplet.green, triplet.blue) + + +_ACCENT = (129, 140, 248) # the theme's accent border, #818cf8 + + +def _find(plain: str, glyphs: frozenset[str]) -> int: + """Index of the first box glyph from ``glyphs`` (the render console may substitute + rounded corners with square ones on legacy Windows, so tests match the whole family).""" + return next(i for i, ch in enumerate(plain) if ch in glyphs) + + +def test_corner_glow_brightens_top_left_and_fades_along_top_edge() -> None: + """The ╭ corner is lifted toward white and the glow decays rightward to the base color.""" + lines = apply_corner_glow(render_lines(Panel(Text("x"), border_style="accent", width=60), 60)) + plain = Text.from_ansi(lines[0]).plain + corner = sum(_cell_color(lines[0], _find(plain, glow._TOP_LEFT))) + middle = sum(_cell_color(lines[0], len(plain) // 2)) + far = sum(_cell_color(lines[0], _find(plain, glow._TOP_RIGHT) - 1)) + assert far == sum(_ACCENT) # the glow has fully melted into the border color + assert corner > middle > far # brightest at the corner, fading rightward + + +def test_corner_glow_fades_down_left_edge_only() -> None: + """The left border fades downward; the right edge and bottom corner stay untouched.""" + body = Text("\n".join("row" for _ in range(8))) + lines = apply_corner_glow(render_lines(Panel(body, border_style="accent", width=30), 30)) + upper = sum(_cell_color(lines[1], 0)) + lower = sum(_cell_color(lines[7], 0)) + assert upper > lower >= sum(_ACCENT) # vertical fade toward the base color + right = Text.from_ansi(lines[1]).plain.rindex("│") + assert _cell_color(lines[1], right) == _ACCENT # right edge keeps the plain border + bottom = Text.from_ansi(lines[-1]).plain + assert _cell_color(lines[-1], _find(bottom, glow._BOTTOM_LEFT)) == _ACCENT # weight is 0 here + + +def test_corner_glow_leaves_titles_and_text_alone() -> None: + """Only border glyphs are recoloured: the title keeps its style, the text its content.""" + panel = Panel(Text("body"), title="Hello", border_style="accent", width=40) + raw = render_lines(panel, 40) + lines = apply_corner_glow(raw) + assert [Text.from_ansi(ln).plain for ln in lines] == [Text.from_ansi(ln).plain for ln in raw] + idx = Text.from_ansi(raw[0]).plain.index("H") + assert _cell_color(lines[0], idx) == _cell_color(raw[0], idx) # title untouched + + +def test_corner_glow_lights_nested_panels_from_their_own_corners() -> None: + """A tool panel nested inside a screen body glows too, blended from its own border color.""" + inner = Panel(Text("body"), border_style="muted", width=20) + screen = ScrollScreen(Group(Text("above"), inner), title="outer") + lines = frame.compose_base(Text("h"), screen, "hint", 60, 20).split("\n") + row, plain = next( + (i, p) + for i, p in ((i, Text.from_ansi(ln).plain) for i, ln in enumerate(lines)) + # The inner box's top edge: a corner glyph on a row already inside the outer border. + if any(ch in glow._TOP_LEFT for ch in p) and p.lstrip()[0] in glow._VERTICAL + ) + muted = (148, 163, 184) # #94a3b8 + corner = _cell_color(lines[row], _find(plain, glow._TOP_LEFT)) + assert corner != muted and all(c > b for c, b in zip(corner, muted)) # lifted toward white + + # --- prompts -----------------------------------------------------------------