The full-screen image shows the message's im3: string under its caption

The ^U view now shows the text the sender transmitted, as a centred block
under the caption, so it can be examined or copied. The string never
shrinks the image below its own size: when both cannot fit, the image
keeps the rows and the string goes a page down, as a large QR code's URL
does. A test now pins that ^U opens the view with images shown too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Jean-Pierre MartineauandClaude Opus 5.5 committed 2026-11-19 23:37:23 -05:00
1 parent 24b7690870
commit 2f2225b8bb
4 files changed
+95 -13

No files matched your search

+7 -5
View File
@@ -1366,9 +1366,10 @@ class ChatScreen(Screen):
def _open_image(self) -> bool:
"""Show the pixel image of the selected message on a bare frame (^U).
The image is as large as the frame permits (refer to
:class:`~meshterm.ui.pixelart.ImageScreen`). When the ``show_mcoimg`` preference is
off, this is the way to see an image. When it is on, this is the large view.
The image is as large as the frame permits, with its caption and the text of the
message under it (refer to :class:`~meshterm.ui.pixelart.ImageScreen`). When the
``show_mcoimg`` preference is off, this is the way to see an image. When it is on,
this is the large view.
Returns:
``True`` when the selected message has an image, so that ^U does nothing more.
@@ -1379,13 +1380,14 @@ class ChatScreen(Screen):
if self._codes_open:
return True
message = self._messages[max(0, min(self._selected or 0, len(self._messages) - 1))]
image = mcoimg.recognize(self._sender_and_body(message)[1])
body = self._sender_and_body(message)[1]
image = mcoimg.recognize(body)
caption = pixel_image_text(image) if image is not None else Text("")
self._codes_open = True
async def run() -> None:
try:
await self._session.run_screen(pixelart.ImageScreen(picture, caption))
await self._session.run_screen(pixelart.ImageScreen(picture, caption, body))
finally:
self._codes_open = False
self._session.invalidate()
+42 -8
View File
@@ -162,39 +162,73 @@ class ImageScreen(ScrollScreen):
It is the share screen of a link (:class:`~meshterm.ui.qr.QrScreen`) for an image: no
header, no footer, no box, and no hint. Esc leaves, as it does everywhere. The image
fits itself to the frame at each paint, so a larger terminal shows a larger image. One
line under it gives its size and its colours, centred.
fits itself to the frame at each paint, so a larger terminal shows a larger image.
Under it are the caption (its size and its colours) and the text of the message itself,
the ``im3:`` string that the sender transmitted. Thus the user can examine or copy the
text that made the image.
The image never touches the edges of the frame. It keeps :data:`_MARGIN_COLS` cells at
each side and at least one row above and below, as a QR code keeps its quiet zone. The
frame puts the block a little above the true centre, as it does for a code.
The text of the message has no spaces, so it breaks at the width of the image area,
into a block of lines that is centred as one. The text never makes the image smaller
than its own size. When the frame cannot hold both at that size, the image gets the
rows, and the text is under it, a page down, as the URL of a large QR code is.
"""
bare = True
def __init__(self, picture: Picture, caption: Text) -> None:
"""Set up the screen for ``picture``, with ``caption`` under it.
def __init__(self, picture: Picture, caption: Text, raw: str = "") -> None:
"""Set up the screen for ``picture``, with ``caption`` and ``raw`` under it.
Args:
picture: The decoded image.
caption: The line about the image, as the chat shows it.
raw: The text of the message: the ``im3:`` string.
"""
super().__init__(Text(""), title="Image", floating=False)
self.picture = picture
self._caption = caption
self.raw = raw.strip()
def render_body(self, width: int) -> list[str]:
"""The image as large as ``width`` and the frame rows permit, then its caption."""
"""The image as large as the frame permits, then its caption and its text."""
# The frame notes its height before it asks for the body, as on the share screen.
rows = self._scroll_viewport
picture = self.picture
# The margins, then a blank row and the caption.
art_rows = max(1, rows - _MARGIN_ROWS - 2)
art_cols = max(1, width - 2 * _MARGIN_COLS)
raw_lines = _raw_lines(self.raw, art_cols, width)
# The margins, a blank row and the caption, then a blank row and the text.
alone = max(1, rows - _MARGIN_ROWS - 2)
art_rows = max(1, alone - (1 + len(raw_lines) if raw_lines else 0))
size = (picture.width, picture.height)
if fit(*size, art_cols, art_rows, grow=True) < size <= fit(*size, art_cols, alone):
art_rows = alone # the text goes a page down
cols, _ = fit(picture.width, picture.height, art_cols, art_rows, grow=True)
indent = max(0, (width - cols) // 2)
art = picture_text(picture, art_cols, indent=indent, max_rows=art_rows, grow=True)
caption = self._caption.copy()
caption.justify = "center"
self.replace_content(Group(art, Text(""), caption))
parts = [art, Text(""), caption]
if raw_lines:
parts += [Text(""), *raw_lines]
self.replace_content(Group(*parts))
return super().render_body(width)
def _raw_lines(raw: str, max_cols: int, width: int) -> list[Text]:
"""The text of a message as a block of lines no wider than ``max_cols``, centred in ``width``.
The text is Base91 characters only, each one cell wide, so a line is a slice of it.
Each line has the same indent, so that the block is centred and its last line starts
under the others.
"""
if not raw:
return []
size = max(1, min(len(raw), max_cols))
indent = " " * max(0, (width - size) // 2)
return [
Text(indent + raw[start : start + size], style="muted", no_wrap=True)
for start in range(0, len(raw), size)
]
+29
View File
@@ -991,6 +991,35 @@ def test_a_pixel_image_shows_a_short_line_not_its_text() -> None:
assert lines[1].strip() and set(lines[1].strip()) <= set("▀▄█")
def test_with_show_mcoimg_on_ctrl_u_still_opens_the_full_screen(monkeypatch) -> None:
"""With the image in the transcript, ^U on the picked image opens the full screen too."""
from meshterm.core import preferences
from meshterm.ui.pixelart import ImageScreen
prefs = preferences.Preferences()
monkeypatch.setattr(preferences, "current", lambda: prefs)
text = "im3:g/5WjAL`NA"
session = _StubSession()
opened = []
async def run_screen(screen): # noqa: ANN001, ANN202
opened.append(screen)
session.run_screen = run_screen
screen = _screen(session, send=None, messages=[ChatMessage(text=text, peer="d4e5f6a7")])
screen._render_grouped(60)
screen._selected = 0
assert "^U image" in screen.footer_hint
async def press() -> None:
screen.handle("url_code")
await asyncio.sleep(0)
asyncio.run(press())
assert len(opened) == 1 and isinstance(opened[0], ImageScreen)
assert opened[0].raw == text
def test_with_show_mcoimg_off_the_image_is_behind_ctrl_u(monkeypatch) -> None:
"""With ``show_mcoimg`` off, only the line shows, and ^U on the picked image opens it."""
from meshterm.core import preferences
+17
View File
@@ -343,3 +343,20 @@ def test_the_image_screen_centres_the_image_with_a_margin(text: str, cols: int,
left, right = len(art) - len(art.lstrip()), cols - len(art.rstrip())
assert left >= 2 and right >= 2
assert abs(left - right) <= 1
def test_the_image_screen_shows_the_text_of_the_message_under_the_caption() -> None:
"""Under the image and its caption, the full screen shows the ``im3:`` string itself."""
import re
from meshterm.ui.pixelart import ImageScreen
from meshterm.ui.tui.frame import compose_bare
from meshterm.ui.widgets import pixel_image_text
text = "im3:TAYL>iBNGbA"
screen = ImageScreen(decode(text), pixel_image_text(recognize(text)), text)
for _ in range(2):
frame = compose_bare(screen, 72, 24)
lines = [re.sub(r"\x1b\[[0-9;]*m", "", line).strip() for line in frame.split("\n")]
caption = next(i for i, line in enumerate(lines) if "image · 9×7 · 2 colours" in line)
assert text in lines[caption + 1 :]