from PIL import Image, ImageDraw, ImageFont
import pathlib, textwrap, json
root=pathlib.Path(__file__).parent
font=ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf',18)
for side,folder in [('before','before-20261120T190323'),('after','after-20261120T190333')]:
 raw=(root/folder/'transcript.txt').read_text()
 # All stdout remains verbatim; long lines only wrap for readability.
 lines=[]
 for line in raw.splitlines(): lines.extend(textwrap.wrap(line,130,replace_whitespace=False,drop_whitespace=False) or [''])
 im=Image.new('RGB',(1480,40+len(lines)*24),'#10151c'); d=ImageDraw.Draw(im)
 for i,line in enumerate(lines): d.text((24,20+i*24),line,font=font,fill=('#79c3ff' if line.startswith('$') else '#e4eaf0'))
 im.save(root/(side+'.png'))
 # A transcript recording: show each command and its actual output in sequence.
 blocks=raw.split('\n$ ')
 frames=root/(side+'-frames'); frames.mkdir()
 for n,block in enumerate(blocks):
  txt=block if n==0 else '$ '+block
  wrapped=[]
  for line in txt.splitlines(): wrapped.extend(textwrap.wrap(line,130,replace_whitespace=False,drop_whitespace=False) or [''])
  image=Image.new('RGB',(1480,1100),'#10151c'); draw=ImageDraw.Draw(image)
  draw.text((24,20),f'{side.upper()} | CLI transcript replay | '+raw.splitlines()[1],font=font,fill='#79c3ff')
  for i,line in enumerate(wrapped): draw.text((24,70+i*24),line,font=font,fill='#e4eaf0')
  image.save(frames/f'{n:03}.png')
