A message's author chip closes on the point, and a mention is a chip that opens on the notch
A message goes from its author toward the nodes it names, so the two chips are the two ends of one route that is not drawn. name_chip takes a role (ORIGIN or DESTINATION); arrow mode ignores it, so a degraded terminal draws exactly what it did before. A chip in a message body never breaks across lines: render_hanging holds each whole run together, spaces included. _sender_style lost its last caller and is gone; its tests now read the rendered chips. Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
125ddb4003
commit
87baa480a9
7 files changed
+227
-89
No files matched your search
@@ -462,6 +462,14 @@ as a grouped list does. Filling a page in is editing its `.md`; no Python follow
|
||||
where the route began or ended (`PathLine(from_origin=…, to_destination=…)`, defaulting
|
||||
to a whole route). Arrow mode spells the same claim with the separator alone, a leading
|
||||
or trailing `→` with no hop on the far side of it.
|
||||
- **A lone chip can stand at one end of a route it doesn't draw** (`name_chip(role=…)`).
|
||||
A message goes from its author toward the nodes it names, so a chat's sender chip is an
|
||||
`ORIGIN` — square (or round) on the left, the point on the right — and an `@mention` in
|
||||
a body is a `DESTINATION` chip, `@` kept, opening on the notch and ending square. No
|
||||
role is a node alone, square at both ends. Arrow mode ignores the role: a lone name
|
||||
with a stray `→` reads as a cut route, so there the sender is its bare name and the
|
||||
mention its bare `@Name`, as before. A chip in prose never breaks across lines
|
||||
(`render_hanging` holds every `whole` run together, spaces and all).
|
||||
- Concept icons: 📡 advert · 🕒 clock/sync · 🔄 reboot · 💾 backup · 📂 restore ·
|
||||
🔑 channel/credential key · 🔐 identity/auth secret ·
|
||||
🔒 lock (a locked contact, a private channel) · 🔓 unlock · 🗑 clear/delete · ✎ compose/edit ·
|
||||
|
||||
+40
-45
@@ -32,8 +32,9 @@ from ..core.events import EventKind, MeshEvent
|
||||
from ..core.models import ChatMessage, Contact, Conversation, Message, utcnow
|
||||
from ..core.regions import WILDCARD
|
||||
from ..platforms import Platform, on_platform
|
||||
from .pathline import PATH_INK
|
||||
from .qr import QrScreen, qr_strip
|
||||
from .theme import name_style, snr_style
|
||||
from .theme import snr_style
|
||||
from .tui.prompt import (
|
||||
CHANNEL_BYTE_LIMIT,
|
||||
DM_BYTE_LIMIT,
|
||||
@@ -43,7 +44,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
|
||||
from .widgets import DESTINATION, ORIGIN, name_chip
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from ..context import AppContext
|
||||
@@ -671,14 +672,19 @@ class ChatScreen(Screen):
|
||||
sender looks exactly like that node as a hop in a route, also for our messages:
|
||||
the ``★``, never our name.
|
||||
|
||||
Only this name is in a chip. An ``@mention`` in a body (refer to
|
||||
:meth:`_render_mentions`) is a part of what the sender said. A chip for it puts a
|
||||
chip in the middle of a sentence.
|
||||
Our star comes from the direction of the message (``outbound``), not from the
|
||||
``"you"`` label. Thus a remote sender with the name ``you`` still gets the hue of
|
||||
its key, and does not look like our node.
|
||||
|
||||
The sender is the origin of the route of each message under it. Thus the chip
|
||||
closes on the point, the mark of a route that goes on. An ``@mention`` in a body is
|
||||
the other end, and it opens on the notch (refer to :meth:`_render_mentions`).
|
||||
"""
|
||||
if message.outbound:
|
||||
return name_chip("", you=True)
|
||||
return name_chip("", you=True, role=ORIGIN)
|
||||
# ``·`` is a channel line that arrived with no sender prefix: there is no key to use.
|
||||
return name_chip(sender, None if sender == "·" else self._header_key(sender, message))
|
||||
key = None if sender == "·" else self._header_key(sender, message)
|
||||
return name_chip(sender, key, role=ORIGIN)
|
||||
|
||||
def _header_key(self, sender: str, message: ChatMessage) -> str | None:
|
||||
"""The key that gives the colour of the sender chip of a group: the key of the name.
|
||||
@@ -718,6 +724,8 @@ class ChatScreen(Screen):
|
||||
prefix.append(f"{stamp:%H:%M} ", style="cursor" if selected else "muted")
|
||||
body_text = self._body_text(body, message, selected=selected)
|
||||
whole = [m.span() for m in _WORD.finditer(body_text.plain) if links.urls(m.group())]
|
||||
# A mention is a chip, and a chip never breaks across two lines.
|
||||
whole += [(s.start, s.end) for s in body_text.spans if str(s.style) == PATH_INK]
|
||||
lines = render_hanging(
|
||||
prefix, body_text, width, indent=prefix.cell_len, whole=whole, gutter=_URL_GUTTER
|
||||
)
|
||||
@@ -760,13 +768,19 @@ class ChatScreen(Screen):
|
||||
return Text(f" · scope {message.scope}", style="muted")
|
||||
|
||||
def _render_mentions(self, body: str, *, selected: bool) -> Text:
|
||||
"""Render body text, and change each ``@[Name]`` token to a ``@Name`` in its hue.
|
||||
"""Render body text, and change each ``@[Name]`` token to a ``@Name`` chip.
|
||||
|
||||
The reply flow puts an ``@[Name]`` token in the compose line (refer to
|
||||
:meth:`_begin_reply`). Here it shows as a bare ``@Name`` in the stable hue of that
|
||||
sender. Thus the user sees that a mention belongs to the person that it names. The
|
||||
text around the mentions keeps the base style of the line (``cursor`` when the
|
||||
message is the selected reply target, or else no style).
|
||||
:meth:`_begin_reply`). Here it shows as ``@Name`` in a chip of the hue of that
|
||||
node. Thus the user sees that a mention belongs to the node that it names. The text
|
||||
around the mentions keeps the base style of the line (``cursor`` when the message
|
||||
is the selected reply target, or else no style).
|
||||
|
||||
The chip opens on the notch, because the node that a mention names is the
|
||||
destination of a route that came from the sender (refer to :meth:`_group_header`,
|
||||
whose chip closes on the point). The ``@`` stays in the chip. A chip keeps the words
|
||||
of the arrow mode, where a mention is ``@Name`` with no frame, as it was before the
|
||||
chip (JP, 2026-10-10).
|
||||
"""
|
||||
base = "cursor" if selected else None
|
||||
text = Text()
|
||||
@@ -775,7 +789,8 @@ class ChatScreen(Screen):
|
||||
if match.start() > pos:
|
||||
text.append(body[pos : match.start()], style=base)
|
||||
name = match.group(1)
|
||||
text.append(f"@{name}", style=self._sender_style(name, mention=True))
|
||||
key = self._sender_key(name, mention=True)
|
||||
text.append_text(name_chip(f"@{name}", key, role=DESTINATION))
|
||||
pos = match.end()
|
||||
if pos < len(body):
|
||||
text.append(body[pos:], style=base)
|
||||
@@ -811,44 +826,24 @@ class ChatScreen(Screen):
|
||||
)
|
||||
return Text("Enter to see the paths this message took · End to cancel", style="accent")
|
||||
|
||||
def _sender_style(self, sender: str, *, is_self: bool = False, mention: bool = False) -> str:
|
||||
"""Get a stable colour for a sender name, from the node key of the sender.
|
||||
|
||||
Our messages are white. This comes from ``is_self`` (the message is outbound), not
|
||||
from the ``"you"`` label. Thus a remote sender with the name ``you`` still gets a
|
||||
hue from the palette, and does not look like our node. ``·`` (unknown) takes
|
||||
``node.unknown``. Each other sender name is resolved to a key (the contacts, then
|
||||
the names that the recorder stored, and in a direct thread the *sender label*
|
||||
falls back to the key of the peer), and takes the hue of that key. A name that no
|
||||
known node has also stays ``node.unknown``: the rule of the app is that colour
|
||||
marks an identity that has a key.
|
||||
|
||||
Each grey here is the *node* grey, never ``muted``. An unidentified sender is
|
||||
content that you can still do actions on (select the message, open its paths),
|
||||
not chrome. The two greys must not follow each other: on the console, ``muted`` is
|
||||
the dark slot and ``node.unknown`` is the light one (JP, 2026-08-12).
|
||||
|
||||
``mention=True`` gives the style of an ``@mention`` in the body, not of a sender
|
||||
label. A mention names any person, so the fallback to the key of the peer must
|
||||
not apply. Thus a mention that is not resolved stays grey in a direct chat,
|
||||
exactly as it does in a channel.
|
||||
"""
|
||||
if is_self:
|
||||
return "you" # white, outside the range of sender hues, so always easy to see
|
||||
if sender == "·":
|
||||
return "node.unknown"
|
||||
return name_style(sender, self._sender_key(sender, mention=mention))
|
||||
|
||||
def _sender_key(self, sender: str, *, mention: bool = False) -> str | None:
|
||||
"""The key of a sender name, or ``None`` when nothing gives a key for it.
|
||||
|
||||
The contacts first, then the names that the recorder stored. In a *direct* thread,
|
||||
a sender label with no key falls back to the key of the peer, because the only two
|
||||
persons in the conversation are we and the peer. A mention names any person, so
|
||||
that fallback must not apply to a mention.
|
||||
persons in the conversation are we and the peer. A mention names any node, so
|
||||
that fallback must not apply to a mention. Thus a mention that is not resolved
|
||||
stays grey in a direct chat, exactly as it does in a channel.
|
||||
|
||||
The colour (:meth:`_sender_style`) and the chip (:meth:`_group_header`) share this
|
||||
method. Thus the two cannot disagree about the identity of a name.
|
||||
A name with no key is drawn in the *node* grey, never in ``muted`` (the chip does
|
||||
this, refer to :func:`~meshterm.ui.widgets.name_chip`). An unidentified sender is
|
||||
content that you can still do actions on (select the message, open its paths),
|
||||
not chrome. On the console, ``muted`` is the dark slot and ``node.unknown`` is the
|
||||
light one (JP, 2026-08-12).
|
||||
|
||||
The sender chip (:meth:`_group_header`) and the mention chips
|
||||
(:meth:`_render_mentions`) share this method. Thus the two cannot disagree about
|
||||
the identity of a name.
|
||||
"""
|
||||
key = self._key_of(sender)
|
||||
if not key and not self._multiparty and not mention:
|
||||
|
||||
+10
-5
@@ -20,7 +20,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, active_deck
|
||||
from .widgets import _recency_style, channel_glyph, name_chip
|
||||
from .widgets import DESTINATION, ORIGIN, _recency_style, channel_glyph, name_chip
|
||||
|
||||
#: Demonstration identities for the row of name colours: ``(name, key)``. The keys are
|
||||
#: synthetic. We chose them so that their first bytes are in three different sectors of
|
||||
@@ -131,17 +131,22 @@ def specimen_lines() -> list[RenderableType]:
|
||||
names.append("me", style="you")
|
||||
lines.append(names)
|
||||
|
||||
# The same names as chips. These are the sender labels of the chat. The path line's
|
||||
# The same names as chips, as the chat draws them. A sender label closes on the point,
|
||||
# because a message goes on from its author. The mention at the end opens on the
|
||||
# notch, because the node that it names is where the message goes. The path line's
|
||||
# own segment draws them, so this row is also a one-hop sample of the route language:
|
||||
# the hue in the fill, the star for our end, and the keyless grey for a node that
|
||||
# nobody can identify.
|
||||
chips = Text("chips ")
|
||||
for name, key in _DEMO_NAMES[:2]:
|
||||
chips.append_text(name_chip(name, key))
|
||||
chips.append_text(name_chip(name, key, role=ORIGIN))
|
||||
chips.append(" ")
|
||||
chips.append_text(name_chip("", you=True))
|
||||
chips.append_text(name_chip("", you=True, role=ORIGIN))
|
||||
chips.append(" ")
|
||||
chips.append_text(name_chip("·"))
|
||||
chips.append_text(name_chip("·", role=ORIGIN))
|
||||
chips.append(" ")
|
||||
name, key = _DEMO_NAMES[2]
|
||||
chips.append_text(name_chip(f"@{name}", key, role=DESTINATION))
|
||||
lines.append(chips)
|
||||
|
||||
lines.append(Text("heard ").append_text(_ages_row()))
|
||||
|
||||
+45
-10
@@ -395,13 +395,15 @@ def render_hanging(
|
||||
|
||||
``whole`` names runs of the body that must not be cut where the screen can hold them.
|
||||
A URL is such a run: a terminal can open it, and a user can copy it, only while it is
|
||||
on one line. For a run that fits the hanging lane, nothing is necessary, because the
|
||||
wrap already moves a word to the next line and does not split it. A run that is wider than the
|
||||
lane, but not wider than the screen, steps out of the block onto a line of its own. It
|
||||
starts at ``gutter``, or as far to the right of the gutter as still fits. After it,
|
||||
the body continues under its indent, unless the text that follows is short enough to
|
||||
finish the line of the run. Only a run wider than the full screen is folded, as
|
||||
before, because no line can hold it.
|
||||
on one line. A chip is another: a mention that breaks between its notch and its name
|
||||
reads as two things. A run that fits the hanging lane moves to the next line as one
|
||||
word. The wrap breaks only at a space, and a space inside a run does not count for it
|
||||
(refer to :func:`_held`), because a chip has its pads and a name can have spaces. A
|
||||
run that is wider than the lane, but not wider than the screen, steps out of the block
|
||||
onto a line of its own. It starts at ``gutter``, or as far to the right of the gutter
|
||||
as still fits. After it, the body continues under its indent, unless the text that
|
||||
follows is short enough to finish the line of the run. Only a run wider than the full
|
||||
screen is folded, as before, because no line can hold it.
|
||||
|
||||
Args:
|
||||
prefix: The leading run (for example, a pointer and a timestamp), shown one time,
|
||||
@@ -427,15 +429,19 @@ def render_hanging(
|
||||
(start, end) for start, end in sorted(whole) if avail < cell_len(plain[start:end]) <= width
|
||||
]
|
||||
rows: list[tuple[int, Text]] = [] # (left pad, line). The pad of the first row is the prefix.
|
||||
held, stand_in = _held(body, whole)
|
||||
|
||||
def hang(start: int, end: int) -> None:
|
||||
"""Wrap ``body[start:end]`` under the indent, and trim the seams next to a run."""
|
||||
if out:
|
||||
while start < end and plain[start].isspace() and start:
|
||||
while start < end and held.plain[start].isspace() and start:
|
||||
start += 1
|
||||
while end > start and plain[end - 1].isspace():
|
||||
while end > start and held.plain[end - 1].isspace():
|
||||
end -= 1
|
||||
lines = list(body[start:end].wrap(console, avail)) if end > start else []
|
||||
lines = list(held[start:end].wrap(console, avail)) if end > start else []
|
||||
if stand_in:
|
||||
for line in lines:
|
||||
line.plain = line.plain.replace(stand_in, " ")
|
||||
if not rows and not lines:
|
||||
lines = [Text("")] # the line of the prefix, also when the body starts with a run
|
||||
rows.extend((indent, line) for line in lines)
|
||||
@@ -467,3 +473,32 @@ def render_hanging(
|
||||
combined.append_text(prefix if i == 0 else Text(" " * pad))
|
||||
combined.append_text(line)
|
||||
return render_lines(combined, width)
|
||||
|
||||
|
||||
def _held(body: Text, runs: Sequence[tuple[int, int]]) -> tuple[Text, str]:
|
||||
"""A copy of ``body`` in which no run has a space, and the character that stands in for one.
|
||||
|
||||
The wrap of Rich breaks a line only at whitespace. Thus a run that has no space in it
|
||||
moves to the next line whole, and a run that has spaces can break at each of them. In
|
||||
the copy, each space inside a run is a character that the body does not use, from the
|
||||
private use area. It has one cell, the same as the space, so the wrap measures the same
|
||||
widths. The caller wraps the copy, then puts the spaces back on each line.
|
||||
|
||||
Args:
|
||||
body: The text that is about to wrap.
|
||||
runs: ``(start, end)`` offsets into ``body.plain`` of the runs to keep together.
|
||||
|
||||
Returns:
|
||||
``(copy, stand_in)``. When no run has a space, this is the body itself and ``""``.
|
||||
"""
|
||||
plain = body.plain
|
||||
spaced = [(start, end) for start, end in runs if " " in plain[start:end]]
|
||||
if not spaced:
|
||||
return body, ""
|
||||
stand_in = next(c for c in map(chr, range(0xE000, 0xF900)) if c not in plain)
|
||||
chars = list(plain)
|
||||
for start, end in spaced:
|
||||
chars[start:end] = [stand_in if c == " " else c for c in chars[start:end]]
|
||||
held = body.copy()
|
||||
held.plain = "".join(chars) # the same length, so each span stays where it was
|
||||
return held, stand_in
|
||||
+27
-3
@@ -58,6 +58,7 @@ from .marks import (
|
||||
parse_hex,
|
||||
)
|
||||
from .pathline import SELF_GLYPH, PathHop, PathLine, path_line
|
||||
from .termfont import powerline_enabled
|
||||
from .theme import glyph, mark_rgb, name_style, node_style, snr_style
|
||||
|
||||
if TYPE_CHECKING:
|
||||
@@ -580,7 +581,17 @@ def highlighted_hash(
|
||||
return text
|
||||
|
||||
|
||||
def name_chip(label: str, key: str | None = None, *, you: bool = False) -> Text:
|
||||
#: The roles of a lone chip in a route that it does not draw (the ``role`` of
|
||||
#: :func:`name_chip`). The chip of an *origin* closes on the point, because the route goes
|
||||
#: on from it: the author of a message. The chip of a *destination* opens on the notch,
|
||||
#: because the route came to it: the node that a mention names.
|
||||
ORIGIN = "origin"
|
||||
DESTINATION = "destination"
|
||||
|
||||
|
||||
def name_chip(
|
||||
label: str, key: str | None = None, *, you: bool = False, role: str | None = None
|
||||
) -> Text:
|
||||
"""A node name drawn as the single chip of a path line: the only frame for a name.
|
||||
|
||||
This is not an imitation of the chip language. It uses the chip language. The function
|
||||
@@ -592,9 +603,19 @@ def name_chip(label: str, key: str | None = None, *, you: bool = False) -> Text:
|
||||
keyless grey. Our own end is the ``★`` of the map on its neutral dark grey, and not a
|
||||
name.
|
||||
|
||||
The ends of the chip say the same thing as the ends of a route. With no ``role``, the
|
||||
node is the whole route, and the two ends are square (or rounded where the font has
|
||||
the caps). A ``role`` puts the chip at one end of a route that is not drawn, and the
|
||||
other end of the chip shows that the route continues there (refer to
|
||||
:data:`ORIGIN` and :data:`DESTINATION`). A message goes from its author toward the
|
||||
nodes that it names. Thus the author closes on the point and a mention opens on the
|
||||
notch, as the first and the last hop of a route do (JP, 2026-10-10).
|
||||
|
||||
The degradation is also inherited. Where the terminal cannot draw powerline
|
||||
separators, a path line is text in arrow mode, and a single hop is only the name in its
|
||||
own hue. This is what a sender label was before it had a frame.
|
||||
own hue. This is what a sender label was before it had a frame. There the ``role`` does
|
||||
nothing. A lone name with a stray ``→`` before or after it reads as a broken route, and
|
||||
not as a sender or as a mention (JP, 2026-10-10).
|
||||
|
||||
Args:
|
||||
label: The name as it must read (it is already resolved, and this function never
|
||||
@@ -605,12 +626,15 @@ def name_chip(label: str, key: str | None = None, *, you: bool = False) -> Text:
|
||||
only for identities that have a key.
|
||||
you: ``True`` if this is our node. It is the ``★``, the same as a route draws our
|
||||
end of it.
|
||||
role: :data:`ORIGIN`, :data:`DESTINATION`, or ``None`` for a node alone.
|
||||
|
||||
Returns:
|
||||
The chip as a one-line :class:`Text`.
|
||||
"""
|
||||
hop = PathHop(SELF_GLYPH, you=True) if you else PathHop(label, key=key or None)
|
||||
return PathLine([hop]).text()
|
||||
if not powerline_enabled():
|
||||
role = None
|
||||
return PathLine([hop], from_origin=role != DESTINATION, to_destination=role != ORIGIN).text()
|
||||
|
||||
|
||||
def _shorten_hash(value: str, hash_bytes: int | None) -> str:
|
||||
|
||||
+65
-26
@@ -415,6 +415,17 @@ def _keys_of(mapping: dict[str, str]):
|
||||
return lambda name: mapping.get(name.casefold())
|
||||
|
||||
|
||||
def _ink(text, run: str) -> str:
|
||||
"""The style that colours ``run`` in ``text``: its identity span, not the path line's stamp."""
|
||||
from meshterm.ui.pathline import PATH_INK
|
||||
|
||||
return next(
|
||||
str(s.style)
|
||||
for s in text.spans
|
||||
if text.plain[s.start : s.end] == run and str(s.style) != PATH_INK
|
||||
)
|
||||
|
||||
|
||||
def test_preview_prefixes_own_messages_only() -> None:
|
||||
"""Only an outbound message gets a ``you:`` prefix. Inbound text shows exactly as it is.
|
||||
|
||||
@@ -982,16 +993,10 @@ def test_at_mention_renders_as_name_in_sender_hue() -> None:
|
||||
|
||||
assert "@Alice" in text.plain # the token with brackets became a bare mention
|
||||
assert "@[Alice]" not in text.plain and "[Alice]" not in text.plain
|
||||
# The "@Alice" text has the hue of the key (the same hue that the header uses).
|
||||
hue = screen._sender_style("Alice")
|
||||
assert hue == node_style("60") # the spectrum hue from the key of the sender
|
||||
at = text.plain.index("@Alice")
|
||||
hue_spans = [
|
||||
s for s in text.spans if s.style == hue and s.start <= at and at + len("@Alice") <= s.end
|
||||
]
|
||||
assert hue_spans
|
||||
# The "@Alice" text has the spectrum hue of the key (the same hue that the header uses).
|
||||
assert _ink(text, "@Alice") == node_style("60")
|
||||
# The unknown @Zed gets the unknown-node grey, because it has no key and no hue.
|
||||
assert screen._sender_style("Zed") == "node.unknown"
|
||||
assert _ink(text, "@Zed") == "node.unknown"
|
||||
|
||||
|
||||
def test_direct_chat_unknown_mention_stays_muted() -> None:
|
||||
@@ -1019,13 +1024,14 @@ def test_direct_chat_unknown_mention_stays_muted() -> None:
|
||||
key_of=_keys_of({"alice": "60" + "0" * 62}),
|
||||
)
|
||||
|
||||
body = screen._render_mentions("@[Alice] and @[Zed]", selected=False)
|
||||
# A mention that resolves still lights in the hue of its own key…
|
||||
assert screen._sender_style("Alice", mention=True) == node_style("60")
|
||||
assert _ink(body, "@Alice") == node_style("60")
|
||||
# …but an unknown mention is grey. It does not get the hue (d4…) of the peer.
|
||||
assert screen._sender_style("Zed", mention=True) == "node.unknown"
|
||||
assert screen._sender_style("Zed", mention=True) != node_style("d4")
|
||||
assert _ink(body, "@Zed") == "node.unknown"
|
||||
# The sender label keeps the fallback to the peer key (the behaviour is the same).
|
||||
assert screen._sender_style("Zed") == node_style("d4")
|
||||
inbound = ChatMessage(text="hi", peer="d4e5f6a7")
|
||||
assert _ink(screen._group_header("Zed", inbound), "Zed") == node_style("d4")
|
||||
|
||||
|
||||
def test_direct_transcript_groups_under_sender_headers() -> None:
|
||||
@@ -1182,9 +1188,16 @@ def test_channel_transcript_groups_by_sender() -> None:
|
||||
assert stamps[0] != stamps[1] # the grouped Alice messages show different times
|
||||
|
||||
|
||||
def test_transcript_inscribes_the_sender_label_but_never_a_mention(powerline) -> None:
|
||||
"""The name that opens a group is a chip. The same name in a body is prose."""
|
||||
from meshterm.ui.widgets import name_chip
|
||||
def test_transcript_draws_the_sender_as_an_origin_and_a_mention_as_a_destination(
|
||||
powerline,
|
||||
) -> None:
|
||||
"""The sender chip closes on the point, and a mention chip opens on the notch.
|
||||
|
||||
A message goes from its author toward the node that it names. Thus the two chips are
|
||||
the two ends of one route that is not drawn (JP, 2026-10-10).
|
||||
"""
|
||||
from meshterm.ui.pathline import POWERLINE_SEP
|
||||
from meshterm.ui.widgets import DESTINATION, ORIGIN, name_chip
|
||||
|
||||
powerline(True) # this test is about the chip, so ask for a terminal that draws one
|
||||
conv = Conversation(label="#public", is_channel=True, channel_idx=0)
|
||||
@@ -1194,11 +1207,34 @@ def test_transcript_inscribes_the_sender_label_but_never_a_mention(powerline) ->
|
||||
screen = ChatScreen(conv, messages, send=None, names={}, session=_StubSession())
|
||||
rendered = _strip_ansi("\n".join(screen._render_grouped(80)))
|
||||
|
||||
# The test builds the chip through the widget, not as literal text. The end caps are the
|
||||
# caps of the path line, and the test asserts the framing, not the glyph.
|
||||
assert name_chip("Alice").plain in rendered # the header, as a chip
|
||||
assert "@Bob" in rendered # the mention, as the user typed it
|
||||
assert name_chip("Bob").plain not in rendered # …and not framed in the middle of a sentence
|
||||
# The test builds the chips through the widget, not as literal text. The end caps are
|
||||
# the caps of the path line, and the test asserts the framing, not the glyph.
|
||||
sender = name_chip("Alice", role=ORIGIN).plain
|
||||
mention = name_chip("@Bob", role=DESTINATION).plain
|
||||
assert sender.endswith(POWERLINE_SEP) and sender in rendered # the header goes on
|
||||
assert mention.startswith(POWERLINE_SEP) and mention in rendered # the mention is reached
|
||||
assert f"{mention} you around?" in rendered # in its place in the sentence
|
||||
|
||||
|
||||
def test_a_mention_chip_moves_to_the_next_line_whole(powerline) -> None:
|
||||
"""The wrap breaks a body at a space, but never at a space inside a mention chip.
|
||||
|
||||
A chip has a pad at each side of its label, and a name can have spaces. Without the
|
||||
hold, the notch stays at the end of one line and the name starts the next line.
|
||||
"""
|
||||
from meshterm.ui.widgets import DESTINATION, name_chip
|
||||
|
||||
powerline(True)
|
||||
conv = Conversation(label="#public", is_channel=True, channel_idx=0)
|
||||
text = "Alice: the relay on the hill looks bad @[Bob Smith] you around?"
|
||||
messages = [ChatMessage(text=text, is_channel=True, channel_idx=0)]
|
||||
screen = ChatScreen(conv, messages, send=None, names={}, session=_StubSession())
|
||||
mention = name_chip("@Bob Smith", role=DESTINATION).plain
|
||||
|
||||
for width in range(30, 60):
|
||||
lines = [_strip_ansi(line).rstrip() for line in screen._render_grouped(width)]
|
||||
assert sum(mention.strip() in line for line in lines) == 1, width
|
||||
assert all(cell_len(line) <= width for line in lines), width
|
||||
|
||||
|
||||
def test_unidentified_sender_takes_the_node_grey_not_the_chrome_grey() -> None:
|
||||
@@ -1210,9 +1246,10 @@ def test_unidentified_sender_takes_the_node_grey_not_the_chrome_grey() -> None:
|
||||
"""
|
||||
conv = Conversation(label="#public", is_channel=True, channel_idx=0)
|
||||
screen = ChatScreen(conv, [], send=None, names={}, session=_StubSession())
|
||||
inbound = ChatMessage(text="hi", is_channel=True, channel_idx=0)
|
||||
|
||||
assert screen._sender_style("·") == "node.unknown"
|
||||
assert screen._sender_style("nobody-knows-me") == "node.unknown" # no key and no hue
|
||||
for sender in ("·", "nobody-knows-me"): # no prefix, and a name with no key and no hue
|
||||
assert _ink(screen._group_header(sender, inbound), sender) == "node.unknown"
|
||||
|
||||
|
||||
def _two_day_messages():
|
||||
@@ -1442,11 +1479,13 @@ def test_channel_self_style_keyed_on_concept_not_label() -> None:
|
||||
"""
|
||||
from meshterm.ui.theme import node_style
|
||||
|
||||
ours = ChatMessage(text="hi", is_channel=True, channel_idx=0, outbound=True)
|
||||
theirs = ChatMessage(text="you: hi", is_channel=True, channel_idx=0)
|
||||
screen = _channel_screen([])
|
||||
assert screen._sender_style("you", is_self=True) == "you" # us: white
|
||||
assert screen._sender_style("you", is_self=False) == "node.unknown" # no key: grey
|
||||
assert _ink(screen._group_header("you", ours), "★") == "you" # us: the white star
|
||||
assert _ink(screen._group_header("you", theirs), "you") == "node.unknown" # no key: grey
|
||||
keyed = _channel_screen([], key_of=_keys_of({"you": "d4" + "0" * 62}))
|
||||
remote = keyed._sender_style("you", is_self=False)
|
||||
remote = _ink(keyed._group_header("you", theirs), "you")
|
||||
assert remote == node_style("d4") # the spectrum hue of the key
|
||||
|
||||
|
||||
|
||||
@@ -15,12 +15,15 @@ from meshterm.core.models import node_type_label
|
||||
from meshterm.ui.pathline import (
|
||||
POWERLINE_ROUND_CLOSE,
|
||||
POWERLINE_ROUND_OPEN,
|
||||
POWERLINE_SEP,
|
||||
PathHop,
|
||||
PathLine,
|
||||
_chip_fill,
|
||||
)
|
||||
from meshterm.ui.theme import node_style
|
||||
from meshterm.ui.widgets import (
|
||||
DESTINATION,
|
||||
ORIGIN,
|
||||
format_age,
|
||||
format_ago,
|
||||
name_chip,
|
||||
@@ -272,6 +275,35 @@ def test_name_chip_greys_a_node_it_cannot_place(powerline) -> None:
|
||||
assert _span_style(keyless, "·").bgcolor.name == "#64748b" # the keyless grey
|
||||
|
||||
|
||||
def test_name_chip_of_an_origin_closes_on_the_point(powerline) -> None:
|
||||
"""The author of a message is where a route starts, and the route goes on from it."""
|
||||
powerline(True)
|
||||
chip = name_chip("Alice", "a1b2", role=ORIGIN)
|
||||
hop = PathLine([PathHop("Alice", key="a1b2")], to_destination=False).text()
|
||||
|
||||
assert chip.plain == POWERLINE_ROUND_OPEN + " Alice " + POWERLINE_SEP
|
||||
assert [(s.start, s.end, str(s.style)) for s in chip.spans] == [
|
||||
(s.start, s.end, str(s.style)) for s in hop.spans
|
||||
]
|
||||
|
||||
|
||||
def test_name_chip_of_a_destination_opens_on_the_notch(powerline) -> None:
|
||||
"""The node that a mention names is where a route ends, and the route came to it."""
|
||||
powerline(True)
|
||||
chip = name_chip("@Bob", "c3d4", role=DESTINATION)
|
||||
|
||||
assert chip.plain == POWERLINE_SEP + " @Bob " + POWERLINE_ROUND_CLOSE
|
||||
assert _span_style(chip, POWERLINE_SEP).reverse # the notch, cut out of the fill
|
||||
|
||||
|
||||
def test_name_chip_role_changes_nothing_without_powerline(powerline) -> None:
|
||||
"""In arrow mode, a lone name has no ends to shape, and a stray arrow reads as a cut route."""
|
||||
powerline(False)
|
||||
|
||||
for role in (ORIGIN, DESTINATION):
|
||||
assert name_chip("Alice", "a1b2", role=role).plain == "Alice"
|
||||
|
||||
|
||||
def test_name_chip_falls_back_to_the_bare_name_without_powerline(powerline) -> None:
|
||||
"""If there is no separator glyph to draw a chip, the chip is the bare name.
|
||||
|
||||
|
||||
Reference in new issue
Block a user