#!/usr/bin/env python3
"""Render captured asciicast output; hold each event for readability, not latency evidence."""
import json,pathlib,subprocess,textwrap,sys
run=pathlib.Path(sys.argv[1])
lines=(run/'interaction.cast').read_text().splitlines()
events=[json.loads(x) for x in lines[1:]]
text=''
for i,event in enumerate(events):
 text+=event[2].replace('\r','')
 display='RPC OUTPUT RECORDING REPLAY | event holds extended for readability\n1440 x 900 | dark terminal | synthetic data\n\n'
 display+='\n'.join(textwrap.fill(line,110,replace_whitespace=False,drop_whitespace=False) if line else '' for line in text.splitlines())
 (run/f'frame-{i}.txt').write_text(display)
 subprocess.run(['convert','-background','#10151d','-fill','#e8edf4','-font','DejaVu-Sans-Mono','-pointsize','20','-size','1380x840','-gravity','NorthWest','caption:'+display,'-bordercolor','#10151d','-border','30',str(run/f'frame-{i}.png')],check=True)
subprocess.run(['ffmpeg','-y','-framerate','1/4','-i',str(run/'frame-%d.png'),'-c:v','libx264','-pix_fmt','yuv420p','-r','20','-movflags','+faststart',str(run/'interaction.mp4')],check=True,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
