|
| 1 | +"""Render docs/images/*.tape with VHS, optionally in parallel. |
| 2 | +
|
| 3 | +Usage: |
| 4 | + uv run poe doc:screenshots # all tapes |
| 5 | + python scripts/gen_cli_interactive_gifs.py # all tapes |
| 6 | + python scripts/gen_cli_interactive_gifs.py commit init # subset |
| 7 | + python scripts/gen_cli_interactive_gifs.py -j 1 # serial |
| 8 | +""" |
| 9 | + |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +import argparse |
| 13 | +import shutil |
1 | 14 | import subprocess |
| 15 | +import sys |
| 16 | +from concurrent.futures import ThreadPoolExecutor, as_completed |
2 | 17 | from pathlib import Path |
3 | 18 |
|
| 19 | +VHS_DIR = Path(__file__).parent.parent / "docs" / "images" |
| 20 | +OUTPUT_DIR = VHS_DIR / "cli_interactive" |
4 | 21 |
|
5 | | -def gen_cli_interactive_gifs() -> None: |
6 | | - """Generate GIF screenshots for interactive commands using VHS.""" |
7 | | - vhs_dir = Path(__file__).parent.parent / "docs" / "images" |
8 | | - output_dir = Path(__file__).parent.parent / "docs" / "images" / "cli_interactive" |
9 | | - output_dir.mkdir(parents=True, exist_ok=True) |
10 | 22 |
|
11 | | - vhs_files = list(vhs_dir.glob("*.tape")) |
| 23 | +def gen_cli_interactive_gifs( |
| 24 | + tape_names: list[str] | None = None, |
| 25 | + max_workers: int | None = None, |
| 26 | +) -> None: |
| 27 | + """Render VHS tapes in parallel. |
12 | 28 |
|
13 | | - if not vhs_files: |
| 29 | + ``tape_names`` filters by stem or filename (``None`` renders all). |
| 30 | + ``max_workers`` defaults to ``min(len(tapes), 4)``; pass ``1`` for serial. |
| 31 | + """ |
| 32 | + if shutil.which("vhs") is None: |
| 33 | + raise SystemExit( |
| 34 | + "VHS is not installed. Please install it from: " |
| 35 | + "https://github.com/charmbracelet/vhs" |
| 36 | + ) |
| 37 | + OUTPUT_DIR.mkdir(parents=True, exist_ok=True) |
| 38 | + all_tapes = sorted(VHS_DIR.glob("*.tape")) |
| 39 | + if not all_tapes: |
14 | 40 | print("No VHS tape files found in docs/images/, skipping") |
15 | 41 | return |
16 | 42 |
|
17 | | - for vhs_file in vhs_files: |
18 | | - print(f"Processing: {vhs_file.name}") |
19 | | - try: |
20 | | - subprocess.run( |
21 | | - ["vhs", vhs_file.name], |
22 | | - check=True, |
23 | | - cwd=vhs_dir, |
24 | | - ) |
25 | | - gif_name = vhs_file.stem + ".gif" |
26 | | - print(f"✓ Generated {gif_name}") |
27 | | - except FileNotFoundError: |
28 | | - print( |
29 | | - "✗ VHS is not installed. Please install it from: " |
30 | | - "https://github.com/charmbracelet/vhs" |
31 | | - ) |
32 | | - raise |
33 | | - except subprocess.CalledProcessError as e: |
34 | | - print(f"✗ Error processing {vhs_file.name}: {e}") |
35 | | - raise |
| 43 | + if tape_names: |
| 44 | + by_stem = {t.stem: t for t in all_tapes} |
| 45 | + tapes: list[Path] = [] |
| 46 | + seen: set[str] = set() |
| 47 | + for name in tape_names: |
| 48 | + stem = Path(name).stem |
| 49 | + if stem in seen: |
| 50 | + continue |
| 51 | + if stem not in by_stem: |
| 52 | + raise SystemExit( |
| 53 | + f"Unknown tape: {name}. Available: {', '.join(sorted(by_stem))}" |
| 54 | + ) |
| 55 | + seen.add(stem) |
| 56 | + tapes.append(by_stem[stem]) |
| 57 | + else: |
| 58 | + tapes = all_tapes |
| 59 | + |
| 60 | + workers = max(1, max_workers if max_workers is not None else min(len(tapes), 4)) |
| 61 | + print(f"Rendering {len(tapes)} tape(s) with up to {workers} worker(s)") |
| 62 | + |
| 63 | + def _render(tape: Path) -> None: |
| 64 | + subprocess.run( |
| 65 | + ["vhs", tape.name], |
| 66 | + check=True, |
| 67 | + cwd=VHS_DIR, |
| 68 | + capture_output=True, |
| 69 | + text=True, |
| 70 | + ) |
| 71 | + |
| 72 | + errors: list[Path] = [] |
| 73 | + with ThreadPoolExecutor(max_workers=workers) as pool: |
| 74 | + futures = {pool.submit(_render, t): t for t in tapes} |
| 75 | + for fut in as_completed(futures): |
| 76 | + tape = futures[fut] |
| 77 | + try: |
| 78 | + fut.result() |
| 79 | + except subprocess.CalledProcessError as exc: |
| 80 | + print(f"✗ {tape.name}", file=sys.stderr) |
| 81 | + if exc.stdout: |
| 82 | + print(exc.stdout, file=sys.stderr) |
| 83 | + if exc.stderr: |
| 84 | + print(exc.stderr, file=sys.stderr) |
| 85 | + errors.append(tape) |
| 86 | + else: |
| 87 | + print(f"✓ {tape.stem}.gif") |
| 88 | + |
| 89 | + if errors: |
| 90 | + raise SystemExit("vhs failed for: " + ", ".join(t.name for t in errors)) |
36 | 91 |
|
37 | 92 |
|
38 | 93 | if __name__ == "__main__": |
39 | | - gen_cli_interactive_gifs() |
| 94 | + parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) |
| 95 | + parser.add_argument( |
| 96 | + "tapes", |
| 97 | + nargs="*", |
| 98 | + help="Tape stems or filenames (e.g. 'commit' or 'commit.tape'). Default: all.", |
| 99 | + ) |
| 100 | + parser.add_argument( |
| 101 | + "-j", |
| 102 | + "--max-workers", |
| 103 | + type=int, |
| 104 | + default=None, |
| 105 | + help="Max parallel vhs invocations. Default: min(len(tapes), 4). Use 1 for serial.", |
| 106 | + ) |
| 107 | + args = parser.parse_args() |
| 108 | + gen_cli_interactive_gifs(args.tapes or None, args.max_workers) |
0 commit comments