|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Record demo.sh as an asciinema cast and render it to an animated GIF. |
| 3 | +# |
| 4 | +# Installs both tools into ~/.local/bin without root: asciinema is pure |
| 5 | +# Python, agg is a single static binary. Run this from a real terminal -- |
| 6 | +# asciinema needs a TTY, which is the one thing a non-interactive session |
| 7 | +# cannot give it. |
| 8 | +set -euo pipefail |
| 9 | + |
| 10 | +AGG_VERSION="${AGG_VERSION:-v1.9.0}" |
| 11 | + |
| 12 | +# agg ships one binary per architecture and there is no universal build, so |
| 13 | +# pick by uname rather than assuming x86_64 -- guessing wrong fails late, at |
| 14 | +# render time, with a bare "Exec format error" after the cast is recorded. |
| 15 | +if [ -z "${AGG_TARGET:-}" ]; then |
| 16 | + case "$(uname -m)" in |
| 17 | + x86_64 | amd64) AGG_TARGET="x86_64-unknown-linux-musl" ;; |
| 18 | + aarch64 | arm64) AGG_TARGET="aarch64-unknown-linux-gnu" ;; |
| 19 | + armv7l | armv6l) AGG_TARGET="arm-unknown-linux-gnueabihf" ;; |
| 20 | + *) |
| 21 | + echo "no agg build for $(uname -m); set AGG_TARGET manually" >&2 |
| 22 | + exit 1 |
| 23 | + ;; |
| 24 | + esac |
| 25 | +fi |
| 26 | + |
| 27 | +HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 28 | +OUT_DIR="${OUT_DIR:-$HERE}" |
| 29 | +CAST="$OUT_DIR/demo.cast" |
| 30 | +GIF="$OUT_DIR/demo.gif" |
| 31 | + |
| 32 | +BIN_DIR="$HOME/.local/bin" |
| 33 | +mkdir -p "$BIN_DIR" "$OUT_DIR" |
| 34 | +export PATH="$BIN_DIR:$PATH" |
| 35 | + |
| 36 | +if ! command -v asciinema >/dev/null 2>&1; then |
| 37 | + echo "installing asciinema..." |
| 38 | + if command -v uv >/dev/null 2>&1; then |
| 39 | + uv tool install asciinema |
| 40 | + else |
| 41 | + python3 -m pip install --user asciinema |
| 42 | + fi |
| 43 | +fi |
| 44 | + |
| 45 | +if ! command -v agg >/dev/null 2>&1 || ! agg --version >/dev/null 2>&1; then |
| 46 | + echo "installing agg $AGG_VERSION for $AGG_TARGET..." |
| 47 | + curl -fsSL -o "$BIN_DIR/agg" \ |
| 48 | + "https://github.com/asciinema/agg/releases/download/$AGG_VERSION/agg-$AGG_TARGET" |
| 49 | + chmod +x "$BIN_DIR/agg" |
| 50 | + # Fail here, not after a recording, if the binary still does not run. |
| 51 | + agg --version >/dev/null |
| 52 | +fi |
| 53 | + |
| 54 | +rm -f "$CAST" "$GIF" |
| 55 | + |
| 56 | +# --cols/--rows exist in asciinema 3.x but not 2.x, where the pty simply |
| 57 | +# inherits the terminal. Probe instead of pinning a version: on 2.x, size |
| 58 | +# your terminal to about 100x30 before recording. |
| 59 | +REC_ARGS=(--idle-time-limit 1 --command "bash $HERE/demo.sh") |
| 60 | +if asciinema rec --help 2>&1 | grep -q -- '--cols'; then |
| 61 | + REC_ARGS+=(--cols 100 --rows 30) |
| 62 | +fi |
| 63 | + |
| 64 | +echo "recording -> $CAST" |
| 65 | +asciinema rec "${REC_ARGS[@]}" "$CAST" |
| 66 | + |
| 67 | +echo "rendering -> $GIF" |
| 68 | +agg --font-size 16 --speed 1.3 --theme asciinema "$CAST" "$GIF" |
| 69 | + |
| 70 | +ls -lh "$GIF" |
| 71 | +echo |
| 72 | +echo "Preview it, then copy into the repo:" |
| 73 | +echo " cp $GIF ~/CubesatTestbed/docs/images/demo.gif" |
0 commit comments