"""Render the tmux pane captures in out/ as screenshots in shots/.

A test aid, not part of the tool: each out/pane-<columns>-<scene>.txt, written
by test/pane.sh from a real tmux pane, is set as plain text on the terminal's
light ground (ink on cream) and photographed with Chromium. The 100 column
panes are shot at 1280x900, the 44 column panes at 390x844, and a pane taller
than its viewport is also shot as a full page. Run from the project folder.
"""

import html
from pathlib import Path

from playwright.sync_api import sync_playwright

SIZES = {"100": (1280, 900, 15), "44": (390, 844, 13)}
PAGE = """<!doctype html><meta charset="utf-8"><style>
:root {{ --bg: #FCF0DD; --ink: #1A1A1A; }}
html, body {{ margin: 0; background: var(--bg); color: var(--ink); }}
pre {{ margin: 0; padding: 24px 16px; font: {size}px/1.4 "DejaVu Sans Mono", ui-monospace, monospace; white-space: pre; }}
</style><pre>{text}</pre>"""


def main() -> None:
    Path("shots").mkdir(exist_ok=True)
    panes = sorted(Path("out").glob("pane-*-*.txt"))
    assert panes, "no pane captures in out/, run test/pane.sh first"
    with sync_playwright() as p:
        browser = p.chromium.launch()
        for pane in panes:
            _, cols, scene = pane.stem.split("-", 2)
            w, h, size = SIZES[cols]
            page = browser.new_page(viewport={"width": w, "height": h}, color_scheme="light")
            page.set_content(PAGE.format(size=size, text=html.escape(pane.read_text())))
            name = f"shots/{scene}-{w}"
            page.screenshot(path=f"{name}.png")
            tall, wide = page.evaluate("[document.documentElement.scrollHeight, document.documentElement.scrollWidth]")
            if tall > h:
                page.screenshot(path=f"{name}-full.png", full_page=True)
            print(f"{name}.png  {cols} columns, {w}x{h}, page {wide}x{tall}{', also full' if tall > h else ''}")
            assert wide <= w, f"{pane} is wider than the {w}px viewport"
            page.close()
        browser.close()


if __name__ == "__main__":
    main()
