|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Script to generate terminal status screenshot for README using rich, chrome/cairosvg.""" |
| 3 | + |
| 4 | +import os |
| 5 | +from pathlib import Path |
| 6 | +import re |
| 7 | +import shutil |
| 8 | +import subprocess |
| 9 | +import sys |
| 10 | +from PIL import Image, ImageChops |
| 11 | +from rich.console import Console |
| 12 | +from rich.terminal_theme import TerminalTheme |
| 13 | +from rich.text import Text |
| 14 | + |
| 15 | + |
| 16 | +# Authentic terminal color palette matching standard terminal emulators (with rich purple HEADER) |
| 17 | +VIBRANT_TERMINAL_THEME = TerminalTheme( |
| 18 | + background=(41, 41, 41), |
| 19 | + foreground=(248, 248, 242), |
| 20 | + normal=[ |
| 21 | + (0, 0, 0), |
| 22 | + (255, 85, 85), |
| 23 | + (80, 250, 123), |
| 24 | + (241, 250, 140), |
| 25 | + (139, 233, 253), |
| 26 | + (189, 147, 249), # Purple (#BD93F9) |
| 27 | + (0, 229, 255), |
| 28 | + (191, 191, 191), |
| 29 | + ], |
| 30 | + bright=[ |
| 31 | + (98, 114, 164), |
| 32 | + (255, 110, 110), |
| 33 | + (105, 255, 148), |
| 34 | + (255, 255, 165), |
| 35 | + (120, 190, 255), |
| 36 | + (189, 147, 249), # Bright Purple (#BD93F9) |
| 37 | + (0, 229, 255), |
| 38 | + (255, 255, 255), |
| 39 | + ], |
| 40 | +) |
| 41 | + |
| 42 | + |
| 43 | +def crop_margins(image_path: Path) -> None: |
| 44 | + """Crop white outer margins from generated screenshot.""" |
| 45 | + im = Image.open(image_path) |
| 46 | + im_rgb = im.convert("RGB") |
| 47 | + bg = Image.new("RGB", im_rgb.size, (255, 255, 255)) |
| 48 | + diff = ImageChops.difference(im_rgb, bg) |
| 49 | + bbox = diff.getbbox() |
| 50 | + if bbox: |
| 51 | + cropped = im.crop(bbox) |
| 52 | + cropped.save(image_path) |
| 53 | + |
| 54 | + |
| 55 | +def convert_svg_to_png(svg_path: Path, output_png: Path) -> None: |
| 56 | + """Convert SVG to PNG using Google Chrome/Chromium if available, falling back to cairosvg.""" |
| 57 | + chrome_bin = shutil.which("google-chrome") or shutil.which("chromium") |
| 58 | + |
| 59 | + if chrome_bin: |
| 60 | + temp_png = svg_path.parent / "temp_chrome.png" |
| 61 | + cmd = [ |
| 62 | + chrome_bin, |
| 63 | + "--headless", |
| 64 | + "--disable-gpu", |
| 65 | + "--hide-scrollbars", |
| 66 | + f"--screenshot={temp_png}", |
| 67 | + "--window-size=1000,800", |
| 68 | + str(svg_path), |
| 69 | + ] |
| 70 | + subprocess.run( |
| 71 | + cmd, |
| 72 | + check=True, |
| 73 | + stdout=subprocess.DEVNULL, |
| 74 | + stderr=subprocess.DEVNULL, |
| 75 | + ) |
| 76 | + |
| 77 | + if temp_png.exists(): |
| 78 | + crop_margins(temp_png) |
| 79 | + shutil.move(temp_png, output_png) |
| 80 | + return |
| 81 | + |
| 82 | + # Fallback to cairosvg |
| 83 | + import cairosvg |
| 84 | + |
| 85 | + svg_data = svg_path.read_text(encoding="utf-8") |
| 86 | + svg_data = re.sub( |
| 87 | + r"font-family:\s*([^;]+);", |
| 88 | + r"font-family: \1, 'Noto Color Emoji', 'Noto Emoji', 'Segoe UI Emoji', 'Apple Color Emoji', 'Symbola', monospace;", |
| 89 | + svg_data, |
| 90 | + ) |
| 91 | + cairosvg.svg2png( |
| 92 | + bytestring=svg_data.encode("utf-8"), |
| 93 | + write_to=str(output_png), |
| 94 | + ) |
| 95 | + |
| 96 | + |
| 97 | +def generate_screenshot() -> None: |
| 98 | + """Run status command as root (or fallback), capture terminal output with ASCII header banner, and export to PNG image.""" |
| 99 | + script_path = Path(__file__).resolve() |
| 100 | + repo_root = script_path.parent.parent.parent |
| 101 | + assets_dir = repo_root / "assets" |
| 102 | + assets_dir.mkdir(parents=True, exist_ok=True) |
| 103 | + output_png = assets_dir / "status-screenshot.png" |
| 104 | + temp_svg = assets_dir / "temp_status.svg" |
| 105 | + |
| 106 | + src_dir = str(repo_root / "src") |
| 107 | + if src_dir not in sys.path: |
| 108 | + sys.path.insert(0, src_dir) |
| 109 | + |
| 110 | + from flatpak_automatic.cli import banner |
| 111 | + |
| 112 | + cli_script = repo_root / "src" / "flatpak-automatic.py" |
| 113 | + |
| 114 | + output_text = None |
| 115 | + |
| 116 | + # Try non-interactive sudo first, then interactive sudo, then non-root execution |
| 117 | + attempts = [ |
| 118 | + ["sudo", "-n", sys.executable, str(cli_script), "-s"], |
| 119 | + ["sudo", sys.executable, str(cli_script), "-s"], |
| 120 | + [sys.executable, str(cli_script), "-s"], |
| 121 | + ] |
| 122 | + |
| 123 | + if os.geteuid() == 0: |
| 124 | + attempts.insert(0, [sys.executable, str(cli_script), "-s"]) |
| 125 | + |
| 126 | + for cmd in attempts: |
| 127 | + try: |
| 128 | + result = subprocess.run( |
| 129 | + cmd, |
| 130 | + cwd=repo_root, |
| 131 | + capture_output=True, |
| 132 | + text=True, |
| 133 | + check=True, |
| 134 | + timeout=5, |
| 135 | + ) |
| 136 | + output_text = result.stdout.strip() |
| 137 | + if output_text: |
| 138 | + break |
| 139 | + except Exception: |
| 140 | + continue |
| 141 | + |
| 142 | + if not output_text: |
| 143 | + # Ultimate fallback |
| 144 | + result = subprocess.run( |
| 145 | + [sys.executable, str(cli_script), "-s"], |
| 146 | + cwd=repo_root, |
| 147 | + capture_output=True, |
| 148 | + text=True, |
| 149 | + check=True, |
| 150 | + ) |
| 151 | + output_text = result.stdout.strip() |
| 152 | + |
| 153 | + banner_text = banner() |
| 154 | + full_output = f"{banner_text}\n{output_text}" |
| 155 | + |
| 156 | + console = Console(record=True, width=100) |
| 157 | + rich_text = Text.from_ansi(full_output) |
| 158 | + console.print(rich_text) |
| 159 | + |
| 160 | + svg_data = console.export_svg( |
| 161 | + title="flatpak-automatic -s", theme=VIBRANT_TERMINAL_THEME |
| 162 | + ) |
| 163 | + temp_svg.write_text(svg_data, encoding="utf-8") |
| 164 | + |
| 165 | + convert_svg_to_png(temp_svg, output_png) |
| 166 | + |
| 167 | + if temp_svg.exists(): |
| 168 | + temp_svg.unlink() |
| 169 | + |
| 170 | + print(f"Successfully generated screenshot at: {output_png}") |
| 171 | + |
| 172 | + |
| 173 | +if __name__ == "__main__": |
| 174 | + generate_screenshot() |
0 commit comments