From 7306f05d9f0da2edb2a13acf9c5caa47e39ff0f2 Mon Sep 17 00:00:00 2001 From: Jean-Pierre Martineau Date: Thu, 19 Nov 2026 22:44:32 -0500 Subject: [PATCH] The full-screen image keeps a margin, centred like the QR codes A small image grown to fill the bare frame touched its edges: a wide one ran edge to edge, and a tall one took every row. It now keeps two cells at each side and three free rows, which the frame splits one above and two below, so it sits centred a little above the middle as a code does. Co-Authored-By: Claude Opus 5.5 --- meshterm/ui/pixelart.py | 21 ++++++++++++++++++--- tests/test_mcoimg.py | 23 +++++++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/meshterm/ui/pixelart.py b/meshterm/ui/pixelart.py index 2417e00..95b1b30 100644 --- a/meshterm/ui/pixelart.py +++ b/meshterm/ui/pixelart.py @@ -148,6 +148,15 @@ def _hex(rgb: RGB | None) -> str: return f"#{r:02x}{g:02x}{b:02x}" +#: The cells of page at each side of the image on its bare frame. +_MARGIN_COLS = 2 +#: The rows of page that the image and its caption leave free on the bare frame, in total. +#: The frame puts two fifths of the free rows above the block (refer to +#: :func:`~meshterm.ui.tui.frame.compose_bare`). With three, one row is above and two are +#: below. With two, the frame puts both below, and the image touches the top. +_MARGIN_ROWS = 3 + + class ImageScreen(ScrollScreen): """The pixel image of a message on a bare frame, as large as the frame permits (^U). @@ -155,6 +164,10 @@ class ImageScreen(ScrollScreen): 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. + + 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. """ bare = True @@ -175,10 +188,12 @@ class ImageScreen(ScrollScreen): # The frame notes its height before it asks for the body, as on the share screen. rows = self._scroll_viewport picture = self.picture - art_rows = max(1, rows - 2) # a blank row and the caption - cols, _ = fit(picture.width, picture.height, width, art_rows, grow=True) + # 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) + cols, _ = fit(picture.width, picture.height, art_cols, art_rows, grow=True) indent = max(0, (width - cols) // 2) - art = picture_text(picture, width, indent=indent, max_rows=art_rows, grow=True) + 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)) diff --git a/tests/test_mcoimg.py b/tests/test_mcoimg.py index 627524d..b4dd614 100644 --- a/tests/test_mcoimg.py +++ b/tests/test_mcoimg.py @@ -320,3 +320,26 @@ def test_the_image_screen_makes_a_small_image_larger() -> None: assert fit(4, 2, 72, 22, grow=True) == (72, 36) # 18 times: the width is the limit assert fit(16, 16, 53, 24, grow=True) == (48, 48) # 3 times: 24 rows hold 48 pixels assert fit(4, 2, 72, 22) == (4, 2) # under a message, never larger + + +@pytest.mark.parametrize(("cols", "rows"), [(72, 24), (120, 40), (53, 26), (53, 14)]) +@pytest.mark.parametrize("text", ["im3:g/5WjAL`NA", "im3:TAYL>iBNGbA"]) +def test_the_image_screen_centres_the_image_with_a_margin(text: str, cols: int, rows: int) -> None: + """On its bare frame, the image is centred, a little above the middle, with page around it.""" + import re + + from meshterm.ui.pixelart import ImageScreen + from meshterm.ui.tui.frame import compose_bare + from meshterm.ui.widgets import pixel_image_text + + screen = ImageScreen(decode(text), pixel_image_text(recognize(text))) + for _ in range(2): # the first paint tells the screen its rows + frame = compose_bare(screen, cols, rows) + lines = [re.sub(r"\x1b\[[0-9;]*m", "", line) for line in frame.split("\n")] + ink = [index for index, line in enumerate(lines) if line.strip()] + above, below = ink[0], rows - 1 - ink[-1] + assert 1 <= above <= below + art = lines[ink[0]] + left, right = len(art) - len(art.lstrip()), cols - len(art.rstrip()) + assert left >= 2 and right >= 2 + assert abs(left - right) <= 1