From 366719771f0baecbc65264903ed0844c0089ae42 Mon Sep 17 00:00:00 2001 From: Jean-Pierre Martineau Date: Wed, 12 Aug 2026 17:46:10 -0400 Subject: [PATCH] Chat: a sender label is a name inscribed in a chip MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A name standing alone above its messages was a coloured word with nothing to say it was a label — at a glance it read like something someone typed. It now sits on a field: the node's own hue as ink on the neutral dark grey pathline already fills our ★ chip with, closed at both ends by the rounded caps a path line closes on, or by half blocks where the font has none. Only the group header. An @mention inside a body is part of what was said, and framing it would put a chip in the middle of a sentence. The widget is ui/widgets.name_chip — the ink arrives as whatever style the caller would have drawn the name in and is *added* to the fill as a Style, because Rich silently drops "node.unknown on #3f3f46" and renders the name bare. The console has no field to offer (its one dark grey is slot 8 and backgrounds stop at 7), so the name draws unframed there, bound on truecolor like the heat gradient. The specimen card grows a chips row so both dresses are visible on the acceptance screen. And the unknown sender takes node.unknown rather than muted: one hex apart here, a whole slot apart on the console, and an unidentified sender is content you can still act on, not chrome. Co-Authored-By: Claude Opus 5 (1M context) --- meshterm/ui/chat.py | 31 +++++++++----- meshterm/ui/specimen.py | 13 +++++- meshterm/ui/theme.py | 12 ++++++ meshterm/ui/widgets.py | 89 ++++++++++++++++++++++++++++++++++++++++- tests/test_chat.py | 35 ++++++++++++++++ tests/test_widgets.py | 48 +++++++++++++++++++++- 6 files changed, 215 insertions(+), 13 deletions(-) diff --git a/meshterm/ui/chat.py b/meshterm/ui/chat.py index 9c0bcf4..99012a2 100644 --- a/meshterm/ui/chat.py +++ b/meshterm/ui/chat.py @@ -35,6 +35,7 @@ from .tui.prompt import ( from .tui.render import render_hanging, render_lines, right_aligned_tail from .tui.screen import CANCEL, Screen from .tui.spinner import Spinner, spinner_interval +from .widgets import name_chip if TYPE_CHECKING: from ..context import AppContext @@ -435,8 +436,15 @@ class ChatScreen(Screen): return (self._name(message.peer) or message.peer or "?"), message.text def _group_header(self, sender: str, *, is_self: bool = False) -> Text: - """Build the sender header that starts a group, colored per sender.""" - return Text(sender, style=self._sender_style(sender, is_self=is_self)) + """Build the sender header that starts a group: the name, chipped, in its hue. + + The chip is what separates a *label* from the prose under it — a name standing + alone above its messages reads as a tag rather than as a coloured word someone + typed. Only this one is framed: an ``@mention`` inside a body (see + :meth:`_render_mentions`) is part of what was said, and inscribing it would put a + chip in the middle of a sentence. + """ + return name_chip(sender, self._sender_style(sender, is_self=is_self)) def _body_lines( self, body: str, message: ChatMessage, width: int, *, selected: bool = False @@ -533,20 +541,25 @@ class ChatScreen(Screen): Our own messages are white — keyed on ``is_self`` (the message being outbound), not on the ``"you"`` label, so a remote sender who happens to be named ``you`` still gets - a hue from the palette rather than masquerading as us. ``·`` (unknown) is muted. - Every other sender resolves its name back to a key (contacts, then the recorder's - stored names; a direct thread's *sender label* falls back to the peer's own key) - and takes that key's hue; a name no known node carries stays muted — the app-wide - rule that colour marks a keyed identity. + a hue from the palette rather than masquerading as us. ``·`` (unknown) takes + ``node.unknown``. Every other sender resolves its name back to a key (contacts, then + the recorder's stored names; a direct thread's *sender label* falls back to the + peer's own key) and takes that key's hue; a name no known node carries stays + ``node.unknown`` too — the app-wide rule that colour marks a keyed identity. + + Every grey here is the *node* grey, never ``muted``: an unidentified sender is + content you can still act on (pick the message, open its paths), not chrome, and + the two must not track each other — on the console ``muted`` is the dark slot and + ``node.unknown`` the light one (JP, 2026-08-12). ``mention=True`` styles an ``@mention`` in the body rather than a sender label: a mention names an arbitrary person, so the peer-key fallback must not apply — an - unresolved one stays muted (gray) in a direct chat exactly as it does in a channel. + unresolved one stays grey in a direct chat exactly as it does in a channel. """ if is_self: return "you" # white, out of the per-sender hue range — always easy to spot if sender == "·": - return "muted" + return "node.unknown" key = self._key_of(sender) if not key and not self._is_channel and not mention: key = self._peer_key or None diff --git a/meshterm/ui/specimen.py b/meshterm/ui/specimen.py index 5ab5ce7..5966f04 100644 --- a/meshterm/ui/specimen.py +++ b/meshterm/ui/specimen.py @@ -18,7 +18,7 @@ from ..platforms import get_platform from .packet_viewer import KIND_ICONS, PAYLOAD_ICONS from .theme import _VT_SLOTS, fold_text, glyph, name_style, snr_style from .tui.fkeys import FPair, lane_text -from .widgets import _recency_style, channel_glyph +from .widgets import _recency_style, channel_glyph, name_chip #: Demo identities for the name-colour row: ``(name, key)``. The keys are synthetic, and #: chosen so their first bytes land in three different sectors of the hue wheel — on the @@ -116,6 +116,17 @@ def specimen_lines() -> list[RenderableType]: names.append("me", style="you") lines.append(names) + # The same names framed — the chat's sender labels. On the console this row shows + # them bare, which is the point: there is no dark grey to field a chip with there. + chips = Text("chips ") + for name, key in _DEMO_NAMES[:2]: + chips.append_text(name_chip(name, name_style(name, key))) + chips.append(" ") + chips.append_text(name_chip("me", "you")) + chips.append(" ") + chips.append_text(name_chip("·", "node.unknown")) + lines.append(chips) + lines.append(Text("heard ").append_text(_ages_row())) snr = Text("snr ") diff --git a/meshterm/ui/theme.py b/meshterm/ui/theme.py index 643ebce..1481e98 100644 --- a/meshterm/ui/theme.py +++ b/meshterm/ui/theme.py @@ -136,6 +136,11 @@ MESH_THEME = Theme( # only PicoCalc's footer_fkeys ever asks for them. "fkey.chip": "bold #ffffff on #475569", "fkey.chip.shift": "bold #ffffff on #16a34a", + # The field a node name is inscribed on when a surface draws it as a chip (see + # ui.widgets.name_chip): the same neutral dark grey pathline fills our own ``★`` + # chip with, so the two chip languages agree. Background only — the ink is always + # the node's own hue, laid over this. + "name.chip": "on #3f3f46", # The prose voices — what a markdown page's inline marks are drawn in (see # ui.markdown). Headings borrow the styles the rest of the app already heads # sections with (``brand``, ``accent``), so only the *body* marks need names of @@ -312,6 +317,13 @@ MESH_THEME_16 = Theme( # Shift bank, white text on both. "fkey.chip": "bold color(15) on color(7)", "fkey.chip.shift": "bold color(15) on color(2)", + # The name chip's field, which this console cannot actually draw: the palette's + # dark grey is slot 8 and backgrounds stop at 7, so there is no neutral field for + # a coloured name to sit on (the F-key lane's light grey is far too bright to take + # a node hue). The name is drawn bare here instead — ui.widgets.name_chip fields a + # chip only where the platform has the colour for one — and this entry exists so + # the style name resolves on both platforms, as every style name must. + "name.chip": "on color(0)", # Prose on the console. The page's body text is the default light grey (slot 7), # so emphasis is the one place the VT's bold-is-brightness rule *is* the design: # ``md.strong`` says bold on purpose and lands on white, exactly the step up the diff --git a/meshterm/ui/widgets.py b/meshterm/ui/widgets.py index 7d806ea..041dab8 100644 --- a/meshterm/ui/widgets.py +++ b/meshterm/ui/widgets.py @@ -18,6 +18,7 @@ from rich.progress import ( TextColumn, TimeElapsedColumn, ) +from rich.style import Style from rich.table import Table from rich.text import Text @@ -51,8 +52,14 @@ from .marks import ( LabelRgbOf, parse_hex, ) -from .pathline import PathLine, path_line -from .theme import glyph, mark_rgb, name_style, node_style, snr_style +from .pathline import ( + POWERLINE_ROUND_CLOSE, + POWERLINE_ROUND_OPEN, + PathLine, + path_line, +) +from .termfont import powerline_full +from .theme import active_theme, glyph, mark_rgb, name_style, node_style, snr_style if TYPE_CHECKING: from ..core.discovery import DiscoveredDevice @@ -495,6 +502,84 @@ def highlighted_hash( return text +#: The theme name of the field a chipped name sits on (defined by both themes). +_CHIP_FILL = "name.chip" + +#: The chip's two end caps, in the two shapes a terminal can give them. The rounded caps +#: (:data:`~meshterm.ui.pathline.POWERLINE_ROUND_OPEN` / ``_CLOSE``) are the lozenge a +#: path line already closes on, so a chipped name and a chipped hop round the same way — +#: but they live in a full Nerd Font patch's extended block. Without one the caps fall +#: back to the half blocks: ``▐`` fills a cell's right half and ``▌`` its left, so drawn in +#: the fill over the page they widen the field by half a cell at each end and taper it, +#: exactly the way the device picker's Bluetooth badge is rounded off (see +#: :func:`~meshterm.ui.device_picker._type_cell`). Two cells of overhead either way. +_ROUND_CAPS = (POWERLINE_ROUND_OPEN, POWERLINE_ROUND_CLOSE) +_HALF_CAPS = ("▐", "▌") + + +def name_chip(label: str, style: str) -> Text: + """A node name inscribed in a chip — THE way a surface *frames* a name. + + The name keeps its own hue (the caller passes whatever style it would have drawn the + name in — a key-derived palette colour, the ``you`` white, the ``node.unknown`` grey) + and that hue becomes the chip's ink, laid on the neutral dark grey field + :data:`_CHIP_FILL` names. Nothing about which colour a name gets changes here; the + chip only gives the colour something to sit on, so a sender label reads as a tag + rather than as a stray coloured word above a message. + + The ink is resolved to a concrete :class:`~rich.style.Style` and *added* to the fill + rather than concatenated as text: Rich silently drops a style string that mixes a + theme name with anything else (``"node.unknown on #3f3f46"`` renders as plain text), + which is exactly the shape a naive f-string would build here. + + A platform whose console cannot field the chip draws the bare name instead — see + :func:`_bind_chip`, and the ``name.chip`` note in the 16-slot theme. + + Args: + label: The name as it should read (already resolved; this never renames it). + style: The style the name would wear uncipped — a theme name or a literal + ``"bold #rrggbb"``, either way the node's own colour. + + Returns: + The chip as a :class:`Text`, or the plain styled name where chips aren't drawn. + """ + if not _chip_fielded: + return Text(label, style=style) + fill = active_theme().styles[_CHIP_FILL] + themed = active_theme().styles.get(style) + ink = themed if themed is not None else Style.parse(style) + # The caps are the fill drawn *over the page* — foreground only, so the half of the + # cell that isn't chip stays whatever the row behind it is. + edge = Style(color=fill.bgcolor) + # Asked per call rather than bound: the verdict is cached after the first ask, and + # binding it would move termfont's installed-font scan onto every import of this + # module — including a bare ``meshterm --help``, which draws nothing at all. + open_cap, close_cap = _ROUND_CAPS if powerline_full() else _HALF_CAPS + text = Text() + text.append(open_cap, style=edge) + text.append(label, style=ink + fill) + text.append(close_cap, style=edge) + return text + + +#: Whether this platform draws the chip at all, rather than the bare name (bound below). +_chip_fielded: bool = True + + +@on_platform +def _bind_chip(platform: Platform) -> None: + """Decide whether the platform can field a chip behind a name. + + The chip is a *field* behind a name, and the 16-slot console has none to offer: its + one dark grey is slot 8 and backgrounds stop at 7, so the choice there is between a + name on light grey (which drowns the hue the chip exists to carry) and no chip at + all. Truecolor is the honest proxy for "this platform has a neutral dark grey", the + same test the heat gradient binds on. + """ + global _chip_fielded + _chip_fielded = platform.truecolor + + def _shorten_hash(value: str, hash_bytes: Optional[int]) -> str: """Return ``value`` as bare hex truncated to ``hash_bytes`` bytes. diff --git a/tests/test_chat.py b/tests/test_chat.py index ca5d4c6..1536dba 100644 --- a/tests/test_chat.py +++ b/tests/test_chat.py @@ -986,6 +986,41 @@ def test_channel_transcript_groups_by_sender() -> None: assert stamps[0] != stamps[1] # grouped Alice messages show distinct times +def test_transcript_inscribes_the_sender_label_but_never_a_mention() -> None: + """The name that *opens* a group is a tag; the same name inside a body is prose.""" + from meshterm.ui.widgets import name_chip + + conv = Conversation(label="#public", is_channel=True, channel_idx=0) + messages = [ + ChatMessage(text="Alice: @[Bob] you around?", is_channel=True, channel_idx=0), + ] + screen = ChatScreen(conv, messages, send=None, names={}, session=_StubSession()) + rendered = _strip_ansi("\n".join(screen._render_grouped(80))) + + # Built through the widget rather than spelled out: which caps it uses depends on the + # font the session found, and the point here is the framing, not the glyph. + def chipped(name: str) -> str: + return name_chip(name, "bold #ffffff").plain + + assert chipped("Alice") in rendered # the header, inscribed + assert "@Bob" in rendered # the mention, as it was typed + assert chipped("Bob") not in rendered # …and not framed mid-sentence + + +def test_unidentified_sender_takes_the_node_grey_not_the_chrome_grey() -> None: + """``·`` is a sender we can't place — content, so ``node.unknown``, never ``muted``. + + The two are one hex apart on the desktop and a whole slot apart on the console (light + grey against dark), which is where a sender drawn in chrome grey stops reading as a + node at all. + """ + conv = Conversation(label="#public", is_channel=True, channel_idx=0) + screen = ChatScreen(conv, [], send=None, names={}, session=_StubSession()) + + assert screen._sender_style("·") == "node.unknown" + assert screen._sender_style("nobody-knows-me") == "node.unknown" # no key, no hue + + def _two_day_messages(): """Two days of direct messages, long enough to overflow a small viewport.""" from datetime import datetime, timedelta, timezone diff --git a/tests/test_widgets.py b/tests/test_widgets.py index f21953e..32b5dd4 100644 --- a/tests/test_widgets.py +++ b/tests/test_widgets.py @@ -7,7 +7,14 @@ and every caller inherits it. from __future__ import annotations -from meshterm.ui.widgets import _format_age, format_ago, path_text, revisit_note +from meshterm.platforms import PICOCALC, REGULAR, set_platform +from meshterm.ui.widgets import ( + _format_age, + format_ago, + name_chip, + path_text, + revisit_note, +) def test_format_age_is_the_bare_column_form() -> None: @@ -183,3 +190,42 @@ def test_revisit_note_is_absent_when_nothing_repeats() -> None: """No repeats, no line — the warning is evidence, never chrome.""" assert revisit_note([], _resolve) is None assert revisit_note(()) is None + + +def test_name_chip_frames_the_name_without_recolouring_it() -> None: + """The chip adds a field; the hue it fields is exactly the one it was handed.""" + chip = name_chip("Alice", "bold #ff00aa") + + assert chip.plain[1:-1] == "Alice" # a cap either side, the name untouched between + styles = {chip.plain[s.start : s.end]: s.style for s in chip.spans} + assert styles["Alice"].color.name == "#ff00aa" # its own hue, still + assert styles["Alice"].bgcolor is not None # now with something under it + # The caps are the fill drawn over the page — foreground only, so the half-cell that + # isn't chip keeps whatever the row behind it is. + for cap in (chip.plain[0], chip.plain[-1]): + assert styles[cap].bgcolor is None + assert styles[cap].color == styles["Alice"].bgcolor + + +def test_name_chip_takes_a_theme_name_as_readily_as_a_hex() -> None: + """``node.unknown``/``you`` arrive as theme names — the fill must not flatten them. + + Rich drops a style string that mixes a theme name with anything else, so a chip built + by concatenation renders the name unstyled. This is that regression, pinned. + """ + chip = name_chip("·", "node.unknown") + + style = next(s.style for s in chip.spans if chip.plain[s.start : s.end] == "·") + assert style.color is not None and style.bgcolor is not None + assert style.color.name != style.bgcolor.name # not the invisible-on-itself case + + +def test_name_chip_draws_bare_where_the_console_has_no_field_for_it() -> None: + """PicoCalc's greys stop at slot 7 as backgrounds — so the name goes unframed there.""" + set_platform(PICOCALC) + try: + chip = name_chip("Alice", "bold #ff00aa") + finally: + set_platform(REGULAR) + + assert chip.plain == "Alice" # no caps, no field — and no tofu on the console font