|
1 | 1 | #!/bin/bash |
| 2 | +set -euo pipefail |
2 | 3 |
|
3 | | -# Script to automatically spawn Ghostty with the Flume theme, |
4 | | -# open Neovim with the sample file, position the cursor, |
5 | | -# and take a screenshot of the window. |
6 | | - |
7 | | -# Ensure we are in the repository root |
8 | 4 | cd "$(dirname "$0")/.." |
9 | 5 |
|
10 | | -# Check if Ghostty is installed |
11 | | -if ! osascript -e 'id of application "Ghostty"' &>/dev/null; then |
12 | | - echo "Error: Ghostty is not installed or not in Applications." |
| 6 | +GHOSTTY_COLUMNS=102 |
| 7 | +GHOSTTY_ROWS=26 |
| 8 | +GHOSTTY_FONT_SIZE=19 |
| 9 | +GHOSTTY_PID="" |
| 10 | +INPUT_FILE="" |
| 11 | + |
| 12 | +fail() { |
| 13 | + echo "Error: $*" >&2 |
13 | 14 | exit 1 |
14 | | -fi |
15 | | - |
16 | | -# Resolve absolute paths |
17 | | -NVIM_PATH=$(command -v nvim) |
18 | | -if [ -z "$NVIM_PATH" ]; then |
19 | | - NVIM_PATH="nvim" |
20 | | -fi |
21 | | - |
22 | | -EXAMPLE_DIR="$(pwd)/examples" |
23 | | -FILE_NAME="flume.zig" |
24 | | -WORKING_DIR="$EXAMPLE_DIR" |
25 | | - |
26 | | -# Ghostty is launched via macOS `open`, so the terminal process may not inherit |
27 | | -# the PATH from the shell that ran this script. Capture the user's login-shell |
28 | | -# PATH and pass it explicitly so Neovim can find tools such as zig and zls. |
29 | | -TERMINAL_PATH=$(/bin/zsh -lc 'print -r -- "$PATH"') |
30 | | -if [ -z "$TERMINAL_PATH" ]; then |
31 | | - TERMINAL_PATH="$PATH" |
32 | | -fi |
33 | | - |
34 | | -# Avoid Ghostty's macOS "Allow Ghostty to execute ...?" prompt by not using |
35 | | -# -e/--command. Instead, start the normal login shell and send the nvim command |
36 | | -# as startup input. Leave the temp file in /tmp long enough for Ghostty to read it. |
37 | | -INPUT_FILE=$(mktemp "${TMPDIR:-/tmp}/flume-screenshot-input.XXXXXX") |
38 | | -printf '%q %q %q %q %q\n' \ |
39 | | - "$NVIM_PATH" \ |
40 | | - "+18" \ |
41 | | - "+normal! w" \ |
42 | | - "+lua vim.defer_fn(function() vim.o.autochdir = false; pcall(vim.cmd, 'lcd %:p:h'); pcall(vim.cmd, 'Gitsigns detach') end, 500)" \ |
43 | | - "$FILE_NAME" >"$INPUT_FILE" |
44 | | - |
45 | | -echo "Opening Ghostty with the Flume theme and screenshot settings..." |
46 | | -open -na Ghostty.app --args \ |
47 | | - --window-save-state=never \ |
48 | | - --quit-after-last-window-closed=true \ |
49 | | - --theme=flume \ |
50 | | - --font-size=19 \ |
51 | | - --window-width=102 \ |
52 | | - --window-height=28 \ |
53 | | - --window-padding-x=16 \ |
54 | | - --window-padding-y=16 \ |
55 | | - --working-directory="$WORKING_DIR" \ |
56 | | - --env="PATH=$TERMINAL_PATH" \ |
57 | | - --input="path:$INPUT_FILE" |
58 | | - |
59 | | -# Give Ghostty and Neovim time to load LSP/Tree-sitter and draw |
| 15 | +} |
| 16 | + |
| 17 | +cleanup() { |
| 18 | + if [ -n "$GHOSTTY_PID" ] && kill -0 "$GHOSTTY_PID" &>/dev/null; then |
| 19 | + echo "Closing Ghostty..." |
| 20 | + kill "$GHOSTTY_PID" &>/dev/null || true |
| 21 | + fi |
| 22 | + |
| 23 | + if [ -n "$INPUT_FILE" ]; then |
| 24 | + rm -f "$INPUT_FILE" |
| 25 | + fi |
| 26 | +} |
| 27 | + |
| 28 | +screen_recording_allowed() { |
| 29 | + local probe |
| 30 | + probe=$(mktemp "${TMPDIR:-/tmp}/flume-screenshot-probe.XXXXXX.png") |
| 31 | + |
| 32 | + if screencapture -x -R 0,0,1,1 "$probe" &>/dev/null && [ -s "$probe" ]; then |
| 33 | + rm -f "$probe" |
| 34 | + return 0 |
| 35 | + fi |
| 36 | + |
| 37 | + rm -f "$probe" |
| 38 | + return 1 |
| 39 | +} |
| 40 | + |
| 41 | +require_screen_recording() { |
| 42 | + if screen_recording_allowed; then |
| 43 | + return |
| 44 | + fi |
| 45 | + |
| 46 | + echo "macOS denied Screen Recording permission for this terminal." |
| 47 | + echo "Grant permission, quit/reopen the terminal, then retry." |
| 48 | + echo "" |
| 49 | + echo "System Settings -> Privacy & Security -> Screen Recording" |
| 50 | + echo "" |
| 51 | + echo "sudo/password cannot grant this macOS privacy permission." |
| 52 | + open "x-apple.systempreferences:com.apple.preference.security?Privacy_ScreenCapture" &>/dev/null || true |
| 53 | + exit 1 |
| 54 | +} |
| 55 | + |
| 56 | +capture_ghostty_window() { |
| 57 | + local err_file |
| 58 | + err_file=$(mktemp "${TMPDIR:-/tmp}/flume-screenshot-error.XXXXXX") |
| 59 | + |
| 60 | + echo "" |
| 61 | + echo "==========================================================" |
| 62 | + echo " ACTION REQUIRED: click the Ghostty window with the camera." |
| 63 | + echo "==========================================================" |
| 64 | + echo "" |
| 65 | + |
| 66 | + if screencapture -o -w screenshot_raw.png 2>"$err_file" && [ -s screenshot_raw.png ]; then |
| 67 | + rm -f "$err_file" |
| 68 | + return 0 |
| 69 | + fi |
| 70 | + |
| 71 | + cat "$err_file" >&2 |
| 72 | + rm -f "$err_file" screenshot_raw.png |
| 73 | + |
| 74 | + echo "Retrying with screen-backed window capture. Click Ghostty again." |
| 75 | + screencapture -i -W -S -o screenshot_raw.png && [ -s screenshot_raw.png ] |
| 76 | +} |
| 77 | + |
| 78 | +launch_ghostty() { |
| 79 | + local nvim_bin terminal_path example_dir input_cmd ghostty_app ghostty_bin |
| 80 | + |
| 81 | + nvim_bin=$(command -v nvim || true) |
| 82 | + if [ -z "$nvim_bin" ]; then |
| 83 | + nvim_bin="nvim" |
| 84 | + fi |
| 85 | + |
| 86 | + terminal_path=$(/bin/zsh -lc 'print -r -- "$PATH"') |
| 87 | + if [ -z "$terminal_path" ]; then |
| 88 | + terminal_path="$PATH" |
| 89 | + fi |
| 90 | + |
| 91 | + example_dir="$(pwd)/examples" |
| 92 | + INPUT_FILE=$(mktemp "${TMPDIR:-/tmp}/flume-screenshot-input.XXXXXX") |
| 93 | + input_cmd="+lua vim.defer_fn(function() vim.o.autochdir = false; pcall(vim.cmd, 'lcd %:p:h'); pcall(vim.cmd, 'Gitsigns detach') end, 500)" |
| 94 | + |
| 95 | + printf '%q %q %q %q %q\n' \ |
| 96 | + "$nvim_bin" \ |
| 97 | + "+18" \ |
| 98 | + "+normal! w" \ |
| 99 | + "$input_cmd" \ |
| 100 | + "flume.zig" >"$INPUT_FILE" |
| 101 | + |
| 102 | + ghostty_app=$(osascript -e 'POSIX path of (path to application "Ghostty")') |
| 103 | + ghostty_bin="${ghostty_app}Contents/MacOS/ghostty" |
| 104 | + |
| 105 | + echo "Opening Ghostty with the Flume theme and screenshot settings..." |
| 106 | + "$ghostty_bin" \ |
| 107 | + --window-save-state=never \ |
| 108 | + --quit-after-last-window-closed=true \ |
| 109 | + --theme=flume \ |
| 110 | + --font-size="$GHOSTTY_FONT_SIZE" \ |
| 111 | + --window-width="$GHOSTTY_COLUMNS" \ |
| 112 | + --window-height="$GHOSTTY_ROWS" \ |
| 113 | + --window-padding-x=16 \ |
| 114 | + --window-padding-y=16 \ |
| 115 | + --working-directory="$example_dir" \ |
| 116 | + --env="PATH=$terminal_path" \ |
| 117 | + --input="path:$INPUT_FILE" & |
| 118 | + GHOSTTY_PID=$! |
| 119 | + disown "$GHOSTTY_PID" 2>/dev/null || true |
| 120 | +} |
| 121 | + |
| 122 | +trap cleanup EXIT |
| 123 | +trap 'exit 130' INT |
| 124 | +trap 'exit 143' TERM |
| 125 | + |
| 126 | +osascript -e 'id of application "Ghostty"' &>/dev/null || fail "Ghostty is not installed or not in Applications." |
| 127 | + |
| 128 | +require_screen_recording |
| 129 | +launch_ghostty |
| 130 | + |
| 131 | +rm -f screenshot_raw.png |
| 132 | + |
60 | 133 | echo "Waiting for window to render..." |
61 | | -sleep 2.5 |
62 | | - |
63 | | -# Bring Ghostty to front and capture its window ID |
64 | | -WINDOW_ID=$(osascript -e ' |
65 | | -tell application "Ghostty" |
66 | | - activate |
67 | | - try |
68 | | - return id of window 1 |
69 | | - on error |
70 | | - return "" |
71 | | - end try |
72 | | -end tell') |
73 | | - |
74 | | -if [ -n "$WINDOW_ID" ]; then |
75 | | - echo "Capturing Ghostty window ID $WINDOW_ID..." |
76 | | - # Capture only the window, omitting the drop shadow (-o) |
77 | | - screencapture -o -l "$WINDOW_ID" screenshot.png |
78 | | - echo "Screenshot successfully saved to screenshot.png" |
79 | | - |
80 | | - # Gracefully close the Ghostty window |
81 | | - osascript -e ' |
82 | | - tell application "Ghostty" |
83 | | - close window 1 |
84 | | - end tell' |
85 | | -else |
86 | | - echo "Error: Could not retrieve Ghostty window ID. Please take screenshot manually." |
87 | | -fi |
| 134 | +sleep 1.0 |
| 135 | +osascript -e 'tell application "Ghostty" to activate' |
| 136 | + |
| 137 | +capture_ghostty_window || fail "Capture cancelled or failed." |
| 138 | + |
| 139 | +cleanup |
| 140 | +trap - EXIT |
88 | 141 |
|
| 142 | +python3 "$(dirname "$0")/compose_header.py" |
| 143 | +echo "Updated screenshot.png" |
0 commit comments