from PIL import Image,ImageDraw,ImageFont
from pathlib import Path
import textwrap
root=Path(__file__).parent
run=next(root.glob('run-*'))
font=ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf',18)
for side in ['before','after']:
    lines=[]
    for line in (run/f'{side}.txt').read_text().splitlines():
        lines.extend(textwrap.wrap(line,126,replace_whitespace=False,drop_whitespace=False) or [''])
    im=Image.new('RGB',(1440,1332), '#111827'); d=ImageDraw.Draw(im)
    for i,line in enumerate(lines):
        color='#fca5a5' if any(s in line for s in ['EADDRNOTAVAIL','NetListenError','ServeError','Waiting for file']) else '#e5e7eb'
        d.text((24,16+26*i),line,font=font,fill=color)
    im.save(run/f'{side}.png')
