- ubuntu:lts instead of ubuntu:24.04 (26.04 today) - cloud-init status --wait --long gives the summary, no need to read the log - long options everywhere in the tutorial and its demos - a discreet link to the asciinema recording under each GIF (pause, fast forward in Gitea's player), in the main README too - castfix.py only restores the prompt after autocast's own !Clear Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018inUJkPw1qcoCubeKqfffm
45 lines
1.5 KiB
Python
45 lines
1.5 KiB
Python
"""Post-process an autocast recording (asciicast v2), in place.
|
|
|
|
- Scenarios with an empty autocast prompt rely on the shell's own prompt (PS1):
|
|
autocast drops the very first one and the one a !Clear erases, so they are
|
|
put back (at the start, and after each screen clear).
|
|
- Progress dots (cloud-init status --wait prints one per second) are squeezed,
|
|
so that a long wait does not take a minute of the animation.
|
|
"""
|
|
|
|
import json
|
|
import re
|
|
import sys
|
|
|
|
DOT_GAP = 0.05
|
|
|
|
path = sys.argv[1]
|
|
with open(path, encoding="utf-8") as f:
|
|
header, *events = (json.loads(line) for line in f if line.strip())
|
|
|
|
ps1 = header.get("env", {}).get("PS1", "")
|
|
prompt = ps1.replace("AUTOCAST_PROMPT", "").replace("\\[", "").replace("\\]", "").replace("\\e", "\x1b")
|
|
first = next((e for e in events if e[1] == "o"), None)
|
|
if prompt and first and not first[2].startswith(prompt):
|
|
events.insert(0, [0.0, "o", prompt])
|
|
if prompt:
|
|
for event in events:
|
|
# Only autocast's own !Clear (a clear run in the shell redraws its prompt)
|
|
if event[1] == "o" and event[2] == "\r\x1b[H\x1b[2J\x1b[3J":
|
|
event[2] += prompt
|
|
|
|
shift, previous = 0.0, 0.0
|
|
for event in events:
|
|
time = event[0]
|
|
gap = time - previous
|
|
previous = time
|
|
if event[1] == "o" and re.fullmatch(r"\.+", event[2]) and gap > DOT_GAP:
|
|
shift += gap - DOT_GAP
|
|
event[0] = round(time - shift, 6)
|
|
if events:
|
|
header["duration"] = events[-1][0]
|
|
|
|
with open(path, "w", encoding="utf-8") as f:
|
|
for record in (header, *events):
|
|
f.write(json.dumps(record, ensure_ascii=False) + "\n")
|