|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Generate PNG snapshots for visual regression testing of zsh prompt.""" |
| 3 | + |
| 4 | +from PIL import Image, ImageDraw, ImageFont |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +# Colors (terminal-like dark theme) |
| 8 | +BG_COLOR = (30, 30, 30) |
| 9 | +DIR_COLOR = (95, 135, 255) # Blue - directory |
| 10 | +BRANCH_COLOR = (0, 215, 215) # Cyan - git branch |
| 11 | +STAGED_COLOR = (95, 215, 0) # Green - staged changes |
| 12 | +UNSTAGED_COLOR = (215, 175, 0) # Yellow - unstaged changes |
| 13 | +UNTRACKED_COLOR = (95, 135, 255) # Blue - untracked files |
| 14 | +PROMPT_OK_COLOR = (95, 215, 0) # Green - success prompt |
| 15 | +PROMPT_ERR_COLOR = (215, 0, 0) # Red - error prompt |
| 16 | +TIME_COLOR = (108, 108, 108) # Gray - time/labels |
| 17 | +EXEC_TIME_COLOR = (215, 175, 0) # Yellow - execution time |
| 18 | + |
| 19 | +# Image dimensions |
| 20 | +WIDTH = 600 |
| 21 | +HEIGHT = 100 |
| 22 | +PADDING = 20 |
| 23 | +LINE_HEIGHT = 25 |
| 24 | + |
| 25 | +def get_font(size=14): |
| 26 | + """Get a monospace font, falling back to default if needed.""" |
| 27 | + font_paths = [ |
| 28 | + "/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf", |
| 29 | + "/usr/share/fonts/TTF/DejaVuSansMono.ttf", |
| 30 | + "/System/Library/Fonts/Monaco.ttf", |
| 31 | + "/usr/share/fonts/truetype/liberation/LiberationMono-Regular.ttf", |
| 32 | + ] |
| 33 | + for path in font_paths: |
| 34 | + if Path(path).exists(): |
| 35 | + return ImageFont.truetype(path, size) |
| 36 | + return ImageFont.load_default() |
| 37 | + |
| 38 | +def create_snapshot(filename, elements, label): |
| 39 | + """ |
| 40 | + Create a snapshot image. |
| 41 | +
|
| 42 | + elements is a list of tuples: [(text, color), ...] |
| 43 | + for each line of the prompt. |
| 44 | + """ |
| 45 | + img = Image.new('RGB', (WIDTH, HEIGHT), BG_COLOR) |
| 46 | + draw = ImageDraw.Draw(img) |
| 47 | + font = get_font(14) |
| 48 | + small_font = get_font(12) |
| 49 | + |
| 50 | + # Draw label |
| 51 | + draw.text((PADDING, 8), label, fill=TIME_COLOR, font=small_font) |
| 52 | + |
| 53 | + y = 35 |
| 54 | + for line in elements: |
| 55 | + x = PADDING |
| 56 | + for item in line: |
| 57 | + if len(item) == 2: |
| 58 | + text, color = item |
| 59 | + draw.text((x, y), text, fill=color, font=font) |
| 60 | + bbox = draw.textbbox((x, y), text, font=font) |
| 61 | + x = bbox[2] |
| 62 | + elif len(item) == 3: |
| 63 | + text, color, position = item |
| 64 | + if position == 'right': |
| 65 | + bbox = draw.textbbox((0, 0), text, font=font) |
| 66 | + text_width = bbox[2] - bbox[0] |
| 67 | + draw.text((WIDTH - PADDING - text_width, y), text, fill=color, font=font) |
| 68 | + y += LINE_HEIGHT |
| 69 | + |
| 70 | + output_dir = Path(__file__).parent / "snapshots" |
| 71 | + output_dir.mkdir(exist_ok=True) |
| 72 | + img.save(output_dir / filename) |
| 73 | + print(f" Generated: {filename}") |
| 74 | + |
| 75 | +def main(): |
| 76 | + print("Generating PNG snapshots...") |
| 77 | + |
| 78 | + # no_git: Just directory, no git info |
| 79 | + create_snapshot("no_git.png", [ |
| 80 | + [("~/some/directory", DIR_COLOR)], |
| 81 | + [("❯", PROMPT_OK_COLOR), ("", TIME_COLOR), ("12:34:56", TIME_COLOR, 'right')], |
| 82 | + ], "no-git") |
| 83 | + |
| 84 | + # git_clean: Directory + clean branch |
| 85 | + create_snapshot("git_clean.png", [ |
| 86 | + [("~/project", DIR_COLOR), (" main", BRANCH_COLOR)], |
| 87 | + [("❯", PROMPT_OK_COLOR), ("", TIME_COLOR), ("12:34:56", TIME_COLOR, 'right')], |
| 88 | + ], "git-clean") |
| 89 | + |
| 90 | + # git_staged: Directory + branch + staged indicator |
| 91 | + create_snapshot("git_staged.png", [ |
| 92 | + [("~/project", DIR_COLOR), (" main", BRANCH_COLOR), ("+", STAGED_COLOR)], |
| 93 | + [("❯", PROMPT_OK_COLOR), ("", TIME_COLOR), ("12:34:56", TIME_COLOR, 'right')], |
| 94 | + ], "git-staged") |
| 95 | + |
| 96 | + # git_unstaged: Directory + branch + unstaged indicator |
| 97 | + create_snapshot("git_unstaged.png", [ |
| 98 | + [("~/project", DIR_COLOR), (" main", BRANCH_COLOR), ("!", UNSTAGED_COLOR)], |
| 99 | + [("❯", PROMPT_OK_COLOR), ("", TIME_COLOR), ("12:34:56", TIME_COLOR, 'right')], |
| 100 | + ], "git-unstaged") |
| 101 | + |
| 102 | + # git_untracked: Directory + branch + untracked indicator |
| 103 | + create_snapshot("git_untracked.png", [ |
| 104 | + [("~/project", DIR_COLOR), (" main", BRANCH_COLOR), ("?", UNTRACKED_COLOR)], |
| 105 | + [("❯", PROMPT_OK_COLOR), ("", TIME_COLOR), ("12:34:56", TIME_COLOR, 'right')], |
| 106 | + ], "git-untracked") |
| 107 | + |
| 108 | + # git_mixed: Directory + branch + all indicators |
| 109 | + create_snapshot("git_mixed.png", [ |
| 110 | + [("~/project", DIR_COLOR), (" main", BRANCH_COLOR), ("+", STAGED_COLOR), ("!", UNSTAGED_COLOR), ("?", UNTRACKED_COLOR)], |
| 111 | + [("❯", PROMPT_OK_COLOR), ("", TIME_COLOR), ("12:34:56", TIME_COLOR, 'right')], |
| 112 | + ], "git-mixed") |
| 113 | + |
| 114 | + # error: Red prompt character |
| 115 | + create_snapshot("error.png", [ |
| 116 | + [("~/project", DIR_COLOR), (" main", BRANCH_COLOR)], |
| 117 | + [("❯", PROMPT_ERR_COLOR), ("", TIME_COLOR), ("12:34:56", TIME_COLOR, 'right')], |
| 118 | + ], "error-exit") |
| 119 | + |
| 120 | + # long_command: Shows execution time |
| 121 | + create_snapshot("long_command.png", [ |
| 122 | + [("~/project", DIR_COLOR), (" main", BRANCH_COLOR)], |
| 123 | + [("❯", PROMPT_OK_COLOR), ("", TIME_COLOR), ("took 5s 12:34:56", EXEC_TIME_COLOR, 'right')], |
| 124 | + ], "long-command-5s") |
| 125 | + |
| 126 | + # very_long_command: Shows longer execution time |
| 127 | + create_snapshot("very_long_command.png", [ |
| 128 | + [("~/project", DIR_COLOR), (" main", BRANCH_COLOR)], |
| 129 | + [("❯", PROMPT_OK_COLOR), ("", TIME_COLOR), ("took 1h 0m 0s 12:34:56", EXEC_TIME_COLOR, 'right')], |
| 130 | + ], "long-command-1h") |
| 131 | + |
| 132 | + # deep_path: Long directory path |
| 133 | + create_snapshot("deep_path.png", [ |
| 134 | + [("~/project/very/deep/nested/directory/structure", DIR_COLOR), (" main", BRANCH_COLOR)], |
| 135 | + [("❯", PROMPT_OK_COLOR), ("", TIME_COLOR), ("12:34:56", TIME_COLOR, 'right')], |
| 136 | + ], "deep-path") |
| 137 | + |
| 138 | + print("Done!") |
| 139 | + |
| 140 | +if __name__ == "__main__": |
| 141 | + main() |
0 commit comments