summaryrefslogtreecommitdiff
path: root/test/record_demos.py
blob: 71bf1055e90d75fb9d0df6371c6d34426e6ea6b1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
#!/usr/bin/env python3
"""Record the documentation site's clips and theme shots from a hidden window.

zig build -Dplatform=gui -Doptimize=ReleaseSafe --prefix <scratch dir>
python3 -B test/record_demos.py <scratch dir>/bin/pardes-gui [clip ... | themes]

Each clip drives a throwaway session (PARDES_TEST: an offscreen capture,
the virtual clock) through its stdin's window-pixel mouse events, its keys
and its 9P files, keeps the settled frame after every step, and lays the
frames out on a timeline with a drawn pointer: a click glides there,
stops, and presses and releases still, a ring and the button's name drawn.
ffmpeg encodes the timeline to docs/site/media/<clip>.webm and .mp4, with
<clip>.png as its poster and captions.txt beside them (clips.toml, which
the site reads, is kept by hand). `themes` instead writes
docs/site/media/themes/<theme>.png for each theme of src/themes, in the
Themes builtin's order, and themes.toml.

Needs a Wayland display for the hidden window, gcc and git for the demo
project, 9ns (cloud9) for the scripting clip, Pillow and numpy, and the
Intel SDM vol. 2 for the PDF clips (--pdf; read where it is, never copied).

Frames and the intermediate video go under --work (default
../.scratch/rec/work, beside the checkout). The demo project lives at
/tmp/hello, a short path for the tags, and the session's home at
/tmp/pardes-rec-home; both are removed after. Nothing of the caller's
environment that names a session, a mount or a bus reaches the recording.
"""
import argparse
import contextlib
import fcntl
import json
import os
from pathlib import Path
import shutil
import signal
import struct
import subprocess
import sys
import termios
import threading
import time

from PIL import Image, ImageDraw, ImageFont

sys.path.insert(0, str(Path(__file__).resolve().parent))
from ninep import Client  # noqa: E402

REPO = Path(__file__).resolve().parent.parent
MEDIA = REPO / 'docs/site/media'
BADGE_FONT = REPO / 'assets/AdwaitaMono-Regular.ttf'
FPS = 30
COLS, ROWS = 60, 20
# A test window's text is 27 px (16 px cells); tags draw at 82% of it
# (config.gui_tagline_font_percent), 13 px a character.
TAG_W = 13
# acme's pale yellow and blue read at the size a landing page shows a clip,
# and are the site's own colours.
THEME = ['acme']
WIDTH = 960  # the clips' width: the window's own, never scaled
PROJECT = Path('/tmp/hello')
HOME = Path('/tmp/pardes-rec-home')

# Nothing of the caller's session reaches the recording: no live pardes,
# no mount, no bus, no editor.
SCRUB = ('NINE_MOUNT', 'NAMESPACE', 'DBUS_SESSION_BUS_ADDRESS', 'GIT_EDITOR', 'EDITOR', 'VISUAL', 'PAGER',
         'GIT_PAGER', 'MANPAGER', 'LESS', 'PROMPT_COMMAND', 'ENV', 'BASH_ENV', 'TMUX', 'ZMX_SESSION')


def clean_env():
    env = {k: v for k, v in os.environ.items() if not k.startswith('PARDES_') and k not in SCRUB}
    display = Path(os.environ.get('WAYLAND_DISPLAY', 'wayland-0'))
    if not display.is_absolute():
        display = Path(os.environ.get('XDG_RUNTIME_DIR', '/run/user/%d' % os.getuid())) / display
    env.update(WAYLAND_DISPLAY=str(display), SDL_VIDEODRIVER='wayland', TERM='xterm-256color',
               LANG='C.UTF-8', LC_ALL='C.UTF-8', GIT_AUTHOR_DATE='2026-09-30T12:00:00', GIT_COMMITTER_DATE='2026-09-30T12:00:00')
    return env


MAIN_C = '''#include <stdio.h>

static int square(int n)
{
    return n * n;
}

int main(void)
{
    int sum = 0;
    for (int i = 1; i <= 4; i++) sum += square(i);
    pritnf("sum: %d\\n", sum);
    return 0;
}
'''

README = '''hello: prints the sum of the first four squares.

Build it with make, run it with ./hello.
'''

MAKEFILE = '''CFLAGS = -Wall -fno-diagnostics-show-caret -fno-diagnostics-show-option

hello: src/main.c
\t@cc $(CFLAGS) -o hello src/main.c
'''


def make_project():
    shutil.rmtree(PROJECT, ignore_errors=True)
    (PROJECT / 'src').mkdir(parents=True)
    (PROJECT / 'src/main.c').write_text(MAIN_C)
    (PROJECT / 'Makefile').write_text(MAKEFILE)
    (PROJECT / 'README').write_text(README)


class Session:
    """A hidden pardes-gui window, its capture directory, its 9P client."""

    def __init__(self, binary, work, name, args, init='', cols=COLS, rows=ROWS):
        self.binary, self.work, self.name = Path(binary).resolve(), work, name
        self.cols, self.rows = cols, rows
        self.caps = work / 'cap'
        self.frames = work / 'frames'
        for d in (self.caps, self.frames):
            shutil.rmtree(d, ignore_errors=True)
            d.mkdir(parents=True)
        shutil.rmtree(HOME, ignore_errors=True)
        HOME.mkdir(mode=0o700)
        (HOME / 'config/pardes').mkdir(parents=True)
        theme = '' if THEME[0] == 'default' else f'Theme {THEME[0]}\n'
        (HOME / 'config/pardes/init').write_text('Shell /bin/sh\n' + theme + 'MessageLinger 0\n' + init)
        env = clean_env()
        env.update(HOME=str(HOME), XDG_RUNTIME_DIR=str(HOME), XDG_CONFIG_HOME=str(HOME / 'config'),
                   PARDES_NOTIME='1', PARDES_DUMP=str(HOME / 'demo.dump.zon'),
                   PARDES_TEST='1', PARDES_TEST_CLOCK='1', PARDES_TEST_COLS=str(cols), PARDES_TEST_ROWS=str(rows),
                   PARDES_TEST_CAPTURE_DIR=str(self.caps))
        for language in ['RS', 'C', 'GO', 'TS', 'PY', 'ZIG']:
            env['PARDES_LSP_' + language] = ''
        master, slave = os.openpty()
        fcntl.ioctl(slave, termios.TIOCSWINSZ, struct.pack('HHHH', rows, cols, 0, 0))
        self.master = master
        self.child = subprocess.Popen(
            [str(self.binary), '--9p=' + name, *map(str, args)], cwd=PROJECT, env=env,
            stdin=slave, stdout=slave, stderr=open(work / 'stderr.log', 'wb'), start_new_session=True,
            preexec_fn=lambda: fcntl.ioctl(0, termios.TIOCSCTTY, 0))
        os.close(slave)
        self.reader = threading.Thread(target=self._drain, daemon=True)
        self.reader.start()
        address = HOME / f'pardes-9p-{name}.sock'
        deadline = time.monotonic() + 30
        while not address.exists():
            if self.child.poll() is not None or time.monotonic() > deadline:
                raise RuntimeError('session did not start; see ' + str(work / 'stderr.log'))
            time.sleep(.01)
        self.client = Client(str(address))
        self.client.__enter__()
        self.shots = []  # the timeline: see shot()
        self.pointer = None  # the drawn pointer's (x, y), None for none
        self.button = None  # the SDL button held, drawn as a ring and its name
        self.badge = None  # a key or two, drawn in the corner
        self.settle()

    def _drain(self):
        try:
            while os.read(self.master, 65536):
                pass
        except OSError:
            pass

    def close(self):
        with contextlib.suppress(Exception):
            self.client.__exit__(None, None, None)
        if self.child.poll() is None:
            self.child.terminate()
            try:
                self.child.wait(timeout=10)
            except subprocess.TimeoutExpired:
                self.child.kill()
                self.child.wait()
        # The session's own process group: its shells and their children.
        with contextlib.suppress(ProcessLookupError, PermissionError):
            os.killpg(self.child.pid, signal.SIGKILL)
        with contextlib.suppress(OSError):
            os.close(self.master)
        self.reader.join(timeout=2)

    # 9P

    def read(self, path):
        return self.client.read(path).decode(errors='replace')

    def write(self, path, data):
        self.client.write(path, data.encode() if isinstance(data, str) else data)

    def panes(self):
        return {int(r.split()[0]): r.split()[1] for r in self.read('/index').splitlines() if r.strip()}

    def tag(self, serial):
        return self.read(f'/pane/{serial}/tag')

    def screen(self):
        s = json.loads(self.client.read('/screen'))
        cols = s['cols']
        return [''.join((cell[0] or ' ') for cell in s['cells'][r * cols:(r + 1) * cols]) for r in range(s['rows'])]

    def find(self, text, after=0, nth=0):
        """The (col, row) of `text` on screen, searching rows from `after`."""
        for r, line in enumerate(self.screen()):
            if r < after:
                continue
            at = line.find(text)
            while at >= 0:
                if nth == 0:
                    return at, r
                nth -= 1
                at = line.find(text, at + 1)
        raise LookupError(f'{text!r} not on screen:\n' + '\n'.join(self.screen()))

    def terminal_rows(self):
        """The screen's rows below the (one) terminal's tag."""
        rows = self.screen()
        return rows[next(i for i, line in enumerate(rows) if 'Tty+sh' in line) + 1:]

    def wait(self, predicate, what, seconds=20):
        deadline = time.monotonic() + seconds
        while time.monotonic() < deadline:
            with contextlib.suppress(Exception):
                if predicate():
                    return
            time.sleep(.03)
        raise RuntimeError('timed out waiting for ' + what + '\n' + '\n'.join(self.screen()))

    def wait_text(self, text, seconds=20):
        self.wait(lambda: any(text in line for line in self.screen()), repr(text), seconds)

    # the window

    def cell_px(self, col, row):
        """The pixel at the middle of a cell (the window is an exact grid)."""
        w, h = self.size()
        cw, ch = w / self.cols, h / self.rows
        return int((col + .5) * cw), int((row + .55) * ch)

    def tag_px(self, col, row, start=3, offset=.5):
        """The pixel over a tag's character: a tag is drawn in the tagline
        font, narrower than the grid, from its first column `start` (3 in a
        pane's or column's tag, 0 in the workspace's)."""
        w, h = self.size()
        cw, ch = w / self.cols, h / self.rows
        return int(start * cw + (col - start + offset) * TAG_W), int((row + .55) * ch)

    def tag_word(self, serial, word):
        """The pixel over the middle of `word` in pane `serial`'s tag, which
        may run past the grid's columns (/screen cuts a tag at its width,
        the window wraps it at its own): its rows hold (width - 3 cells) /
        TAG_W characters each."""
        tag = self.tag(serial)
        w, h = self.size()
        cw, ch = w / self.cols, h / self.rows
        per_row = int((w - 3 * cw) // TAG_W)
        top = self.find(tag[:min(len(tag), self.cols - 4)], 0)[1]
        at = tag.index(word) + len(word) // 2
        return int(3 * cw + (at % per_row + .5) * TAG_W), int((top + at // per_row + .55) * ch)

    def size(self):
        with open(self.caps / 'latest.ppm', 'rb') as f:
            f.readline()
            w, h = map(int, f.readline().split())
        return w, h

    def mouse(self, kind, button, x, y):
        os.write(self.master, f'\x1b]777;mouse;{kind};{button};{x};{y}\x07'.encode())
        time.sleep(.05)

    def keys(self, data):
        os.write(self.master, data.encode() if isinstance(data, str) else data)
        time.sleep(.03)

    def status(self):
        try:
            step, frames, idle = (self.caps / 'status').read_text().split()[:3]
            return int(step), int(frames), idle == '1'
        except (FileNotFoundError, ValueError):
            return None

    def settle(self):
        """The frame once the window says it is idle (test/gui_golden.py)."""
        deadline = time.monotonic() + 60
        start = None
        while time.monotonic() < deadline:
            now = self.status()
            if now is not None:
                if start is None:
                    start = now[0]
                elif now[0] >= start + 2 and now[2] and now[1] > 0:
                    data = (self.caps / 'latest.ppm').read_bytes()
                    if data.startswith(b'P6'):
                        return data
            time.sleep(.02)
        raise RuntimeError('the window never went idle')

    # the timeline

    def shot(self, seconds, poster=False, ring=None, ring_button=None):
        """Hold the settled frame for `seconds`, the pointer where it is.
        `poster` marks the frame drawn as the clip's still; `ring` draws a
        click there ('press' while held, 'pulse' just after)."""
        data = self.settle()
        path = self.frames / f'{len(self.shots):04d}.ppm'
        path.write_bytes(data)
        self.shots.append(dict(frame=path, secs=seconds, start=self.pointer, end=self.pointer,
                               button=self.button, badge=self.badge, poster=poster, ring=ring,
                               ring_button=ring_button or self.button))

    def glide(self, x, y, seconds=.6):
        """Glide the drawn pointer to (x, y) over the frame on screen, then
        move the window's pointer there (dragging any button held)."""
        if self.shots and self.pointer is not None:
            last = self.shots[-1]
            self.shots.append(dict(last, secs=seconds, start=self.pointer, end=(x, y), poster=False, ring=None))
        self.pointer = (x, y)
        self.mouse('motion', self.button or 0, x, y)

    def click(self, button, x, y, done=None, what='the click', rest=.4, hold=.35, after=1.0):
        """Glide to (x, y) and stop; a beat later press `button` (SDL: 1
        left, 2 middle, 3 right) and release it with the pointer still, a
        ring and the button's name drawn on the press; once `done()` says
        the click took, hold the result `after` seconds, the ring pulsing
        out, before anything moves again."""
        if self.pointer != (x, y):
            self.glide(x, y)
        self.shot(rest)
        self.mouse('down', button, x, y)
        self.button = button
        self.shot(hold, ring='press')
        self.mouse('up', button, x, y)
        self.button = None
        if done is not None:
            self.wait(done, what)
        self.shot(after, ring='pulse', ring_button=button)

    def press(self, keys, per_key=.4):
        """Send each (key bytes, badge) of `keys`, a frame a key with the
        badge in the corner."""
        for key, badge in keys:
            self.badge = badge
            self.keys(key)
            self.shot(per_key)
        self.badge = None

    def type(self, text, expect, per_key=.07):
        """Type `text` a key at a time, a frame per key, once `expect(prefix)`
        says the window shows what is typed so far."""
        for i, ch in enumerate(text):
            self.keys(ch)
            prefix = text[:i + 1]
            self.wait(lambda: expect(prefix), 'echo of ' + repr(prefix))
            self.shot(per_key)


# composing

def pointer_sprite(scale):
    """An arrow pointer, white with a dark outline, its hotspot at (1, 1)."""
    pts = [(0, 0), (0, 17), (4, 13), (7, 20), (10, 19), (7, 12), (12, 12)]
    pts = [(x * scale + 1, y * scale + 1) for x, y in pts]
    im = Image.new('RGBA', (int(13 * scale) + 4, int(21 * scale) + 4), (0, 0, 0, 0))
    ImageDraw.Draw(im).polygon(pts, fill=(255, 255, 255, 255), outline=(20, 20, 20, 255), width=2)
    return im


# acme's colours for the buttons: B2 red, B3 green.
BUTTONS = {1: ('click', (70, 70, 70)), 2: ('middle-click', (170, 0, 0)), 3: ('right-click', (0, 110, 40))}


def draw_badge(img, text, xy, font, fill=(40, 40, 40), anchor='lt'):
    d = ImageDraw.Draw(img, 'RGBA')
    box = d.textbbox(xy, text, font=font, anchor=anchor)
    pad = 7
    d.rounded_rectangle((box[0] - pad, box[1] - pad, box[2] + pad, box[3] + pad), radius=8, fill=fill + (235,))
    d.text(xy, text, font=font, fill=(255, 255, 255), anchor=anchor)


class Painter:
    def __init__(self, size):
        self.sprite = pointer_sprite(1.7)
        self.font = ImageFont.truetype(str(BADGE_FONT), 24)
        self.size = size

    def paint(self, img, pointer, badge, ring=None, button=None, since=0.0):
        """`img` with the badge, the pointer, and a click: `ring` 'press'
        (a ring and the button's name) or 'pulse' (`since` seconds after the
        release: the ring widening and fading, the name kept a moment)."""
        img = img.copy()
        w, h = self.size
        if badge:
            draw_badge(img, badge, (w - 16, h - 16), self.font, anchor='rb')
        if pointer is not None:
            x, y = pointer
            if ring and button:
                label, colour = BUTTONS[button]
                d = ImageDraw.Draw(img, 'RGBA')
                if ring == 'press':
                    r, alpha, named = 16, 255, True
                else:
                    t = min(since / .45, 1)
                    r, alpha, named = 16 + 22 * t, int(255 * (1 - t)), since < .6
                if alpha > 0:
                    d.ellipse((x - r, y - r, x + r, y + r), outline=colour + (alpha,), width=4)
                if named:
                    # The name sits below the pointer, kept inside the frame.
                    lx = min(x + 22, w - 16 - self.font.getlength(label))
                    ly = y + 40 if y + 70 < h else y - 60
                    draw_badge(img, label, (lx, ly), self.font, fill=colour)
            img.paste(self.sprite, (int(x) - 1, int(y) - 1), self.sprite)
        return img


def ease(t):
    return t * t * (3 - 2 * t)


def lerp(a, b, t):
    if a is None or b is None:
        return b if t >= .5 else a
    return (a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t)


def compose(shots, out):
    """The shots at FPS with the pointer drawn, ending in a dissolve back to
    the first frame so the clip loops; written lossless (FFV1) to `out`.
    Answers the clip's length in seconds and its poster image."""
    first = Image.open(shots[0]['frame']).convert('RGB')
    painter = Painter(first.size)
    w, h = first.size
    ff = subprocess.Popen(['ffmpeg', '-v', 'error', '-y', '-f', 'rawvideo', '-pix_fmt', 'rgb24', '-s', f'{w}x{h}',
                           '-r', str(FPS), '-i', '-', '-c:v', 'ffv1', str(out)], stdin=subprocess.PIPE)
    poster = None
    clock, emitted = 0.0, 0
    loaded = (None, None)
    for shot in shots:
        if loaded[0] != shot['frame']:
            loaded = (shot['frame'], Image.open(shot['frame']).convert('RGB'))
        img = loaded[1]
        clock += shot['secs']
        n = round(clock * FPS) - emitted
        for i in range(n):
            t = ease((i + 1) / n)
            frame = painter.paint(img, lerp(shot['start'], shot['end'], t), shot['badge'], shot['ring'],
                                  shot['ring_button'], i / FPS)
            ff.stdin.write(frame.tobytes())
        if shot['poster'] and poster is None:
            poster = painter.paint(img, shot['end'], None)
        emitted += n
    last = shots[-1]
    n = round(.7 * FPS)
    for i in range(n):
        t = ease((i + 1) / n)
        frame = Image.blend(loaded[1], first, t)
        ff.stdin.write(painter.paint(frame, lerp(last['end'], shots[0]['start'], t), None).tobytes())
    emitted += n
    ff.stdin.close()
    if ff.wait() != 0:
        raise RuntimeError('ffmpeg (lossless) failed')
    return emitted / FPS, poster or painter.paint(first, None, None)


def encode(lossless, name, poster, crf_webm=30, crf_mp4=20):
    """<name>.webm (VP9) and .mp4 (H.264, yuv420p, faststart) and the poster
    PNG, in MEDIA."""
    MEDIA.mkdir(parents=True, exist_ok=True)
    webm, mp4 = MEDIA / (name + '.webm'), MEDIA / (name + '.mp4')
    scale = [] if poster.width == WIDTH else ['-vf', f'scale={WIDTH}:-2:flags=lanczos']
    subprocess.run(['ffmpeg', '-v', 'error', '-y', '-i', str(lossless), *scale, '-c:v', 'libvpx-vp9',
                    '-b:v', '0', '-crf', str(crf_webm), '-row-mt', '1', '-deadline', 'good', '-cpu-used', '1',
                    '-g', '240', '-pix_fmt', 'yuv420p', '-an', str(webm)], check=True)
    subprocess.run(['ffmpeg', '-v', 'error', '-y', '-i', str(lossless), *scale, '-c:v', 'libx264',
                    '-preset', 'veryslow', '-crf', str(crf_mp4), '-tune', 'animation', '-pix_fmt', 'yuv420p',
                    '-movflags', '+faststart', '-an', str(mp4)], check=True)
    if poster.width != WIDTH:
        poster = poster.resize((WIDTH, round(poster.height * WIDTH / poster.width)), Image.LANCZOS)
    poster.save(MEDIA / (name + '.png'), optimize=True)
    return [webm, mp4, MEDIA / (name + '.png')]


# the clips

def hero_setup(s):
    """README, main.c under it and an output pane under that, all on
    screen from the first frame: the output pane is a finished `clear`,
    which `make` will reuse."""
    s.write('/pane/1/look', f'{PROJECT}/src/main.c\n')
    s.wait(lambda: len(s.panes()) == 2, 'main.c')
    s.write('/pane/1/exec', 'clear\n')
    s.wait(lambda: any(kind == 'cmd' and 'exit' in s.tag(n) for n, kind in s.panes().items()), 'the output pane')
    s.write('/pane/1/tag', ' make')
    s.mouse('motion', 0, *s.cell_px(36, 9))


def text_is_the_interface(s):
    """B2 on `make` in a tag runs it; B3 on the error opens main.c there."""
    output = next(n for n, kind in s.panes().items() if kind == 'cmd')
    s.pointer = s.cell_px(36, 9)
    s.shot(1.2)
    col, row = s.find('make', 0)
    s.click(2, *s.tag_px(col + 1, row), done=lambda: '(make) exit' in s.tag(output), what='make')
    col, row = s.find('src/main.c:12:5', 0)
    s.click(3, *s.cell_px(col + 4, row), done=lambda: any(line.startswith('    12 ') for line in s.screen()[6:12]),
            what='main.c at line 12', after=2.8)
    s.shots[-1]['poster'] = True


def shell_setup(s):
    """A terminal under the file, at the project, with a short prompt."""
    s.write('/pane/1/exec', 'Tty\n')
    s.wait(lambda: 2 in s.panes(), 'the terminal')
    s.write('/pane/2/pty/data', f"cd {PROJECT}; PS1='$ '; clear\r")
    s.wait(lambda: any(line.rstrip() == '  $' for line in s.screen()), 'the prompt')
    # The pointer rests off the tags, so no word's hint is up.
    s.mouse('motion', 0, *s.cell_px(COLS - 2, ROWS - 1))


def mounted_shell_setup(s):
    """A terminal under the file whose shell sees the session as files:
    cloud9's 9ns mounts the pane's $PARDES_9P in a private namespace."""
    shell_setup(s)
    mount = HOME / 'n'
    mount.mkdir()
    s.write('/pane/2/pty/data', f'exec 9ns --unix "$PARDES_9P" --mount {mount} -- /bin/sh\r')
    time.sleep(1.5)  # 9ns mounts and starts the shell that reads the next line
    s.write('/pane/2/pty/data', "cd \"$NINE_MOUNT\" && PS1='$ ' && clear\r")
    s.wait(lambda: [line.rstrip() for line in s.terminal_rows() if line.strip()] == ['  $'], 'the mounted shell')


def scripting_over_9p(s):
    """A shell reads the session's files and writes a pane's body."""
    s.shot(1.0)
    for command, done in [('ls', lambda: any('index' in line for line in s.terminal_rows()[1:])),
                          ('cat index', lambda: any(' term ' in line for line in s.terminal_rows())),
                          ("echo 'hello from a script' > pane/1/body", lambda: 'hello from' in s.screen()[3])]:
        s.type(command, lambda typed: at_prompt(s, typed)(), per_key=.06)
        s.keys('\r')
        s.wait(done, command)
        s.wait(lambda: [line.rstrip() for line in s.terminal_rows() if line.strip()][-1] == '  $', 'the prompt')
        s.shot(1.3)
    s.shots[-1]['secs'] = 2.4
    s.shots[-1]['poster'] = True


def at_prompt(s, text):
    return lambda: any(line.startswith('  $ ' + text) for line in s.screen())


def terminals_are_panes(s):
    """A shell under the file; Ctrl-b and its output is text to move over."""
    s.write('/pane/1/dot', '12\n')
    s.shot(1.0)
    s.type('make', lambda typed: at_prompt(s, typed)())
    s.keys('\r')
    s.wait(lambda: 'Error 1' in '\n'.join(s.screen()), 'make')
    s.wait(lambda: any(line.rstrip() == '  $' for line in s.screen()), 'the next prompt')
    s.shot(1.4)
    s.press([('\x02', 'Ctrl-b')], per_key=.8)
    s.press([('k', 'k'), ('k', 'k k'), ('k', 'k k k')], per_key=.4)
    s.press([('x', 'x')], per_key=2.4)
    s.shots[-1]['poster'] = True


def diff_setup(s):
    """The project committed, then its typo fixed; a lone terminal."""
    env = dict(clean_env(), HOME=str(HOME), GIT_AUTHOR_NAME='demo', GIT_AUTHOR_EMAIL='[email protected]',
               GIT_COMMITTER_NAME='demo', GIT_COMMITTER_EMAIL='[email protected]')
    for args in (['init', '-q'], ['add', '.'], ['commit', '-qm', 'hello'], ['config', 'diff.context', '1']):
        subprocess.run(['git', '-C', str(PROJECT), *args], check=True, env=env, capture_output=True)
    main = PROJECT / 'src/main.c'
    main.write_text(main.read_text().replace('pritnf', 'printf'))
    shell_setup(s)
    s.write('/pane/1/exec', 'Del\n')
    s.wait(lambda: list(s.panes()) == [2], 'the README to close')


def diff_review(s):
    """git diff in a shell pages into +Pager as a diff; B3 on its + line
    opens the file at that line."""
    s.pointer = s.cell_px(40, 6)
    s.shot(.9)
    s.type('git diff', lambda typed: at_prompt(s, typed)())
    s.keys('\r')
    s.wait(lambda: 'pager' in s.panes().values() or len(s.panes()) == 2, 'the pager')
    s.wait_text('+    printf')
    s.shot(1.4)
    col, row = s.find('+    printf', 0)
    s.click(3, *s.cell_px(col, row), done=lambda: len(s.panes()) == 3, what='main.c to open', after=2.6)
    s.shots[-1]['poster'] = True


def mandelbrot(path, width=1200, height=800):
    """The classic view of the Mandelbrot set, smoothly coloured."""
    import numpy as np
    x = np.linspace(-2.35, 0.85, width)
    y = np.linspace(-1.0667, 1.0667, height)
    c = x[None, :] + 1j * y[:, None]
    z = np.zeros_like(c)
    count = np.zeros(c.shape)
    alive = np.ones(c.shape, bool)
    limit = 200
    for n in range(limit):
        z[alive] = z[alive] ** 2 + c[alive]
        escaped = alive & (np.abs(z) > 4)
        # The smooth (normalised) iteration count, so the bands blend.
        count[escaped] = n + 1 - np.log2(np.log2(np.abs(z[escaped])))
        alive &= ~escaped
    t = np.sqrt(np.clip(count / 40, 0, 1))
    # A deep blue through gold to white, the inside black.
    stops = np.array([[0, 7, 100], [32, 107, 203], [237, 255, 255], [255, 170, 0], [80, 2, 0]], float)
    pos = np.linspace(0, 1, len(stops))
    rgb = np.stack([np.interp(t, pos, stops[:, k]) for k in range(3)], axis=-1)
    rgb[alive] = 0
    Image.fromarray(rgb.astype(np.uint8)).save(path)


def image_setup(s):
    """A lone terminal at the project, which holds a picture."""
    mandelbrot(PROJECT / 'mandelbrot.png')
    shell_setup(s)
    s.write('/pane/1/exec', 'Del\n')
    s.wait(lambda: list(s.panes()) == [2], 'the README to close')


def image_pane(s):
    """ls in a shell; B3 on the PNG's name opens it in a pane."""
    s.pointer = s.cell_px(40, 8)
    s.shot(1.0)
    s.type('ls', lambda typed: at_prompt(s, typed)())
    s.keys('\r')
    s.wait(lambda: any('mandelbrot.png' in line for line in s.terminal_rows()), 'ls')
    s.shot(.9)
    col, row = s.find('mandelbrot.png', 0)
    s.click(3, *s.cell_px(col + 5, row), done=lambda: len(s.panes()) == 2, what='the picture', after=2.2)
    s.shots[-1]['poster'] = True
    image = next(n for n, kind in s.panes().items() if kind == 'image')
    # Petscii, a word typed into the picture's tag, redraws it as a C64's
    # character art would.
    s.write(f'/pane/{image}/tag', ' Petscii')
    s.click(2, *s.tag_word(image, 'Petscii'), done=lambda: 'petscii:on' in s.tag(image), what='petscii',
            after=2.6)


# The Intel 64 and IA-32 SDM, vol. 2 (2,515 pages), opened where it is.
PDF = [Path('/home/goblin/01-resources/intel-devref-ii.pdf')]


def pdf_args():
    return [PDF[0]]


def pdf_setup(s):
    s.mouse('motion', 0, *s.cell_px(COLS - 2, ROWS - 1))


def pdf_tag(s, word, pane=None):
    """The pixel over `word` in the PDF pane's tag."""
    pane = pane or next(n for n, kind in s.panes().items() if kind == 'pdf')
    return s.tag_word(pane, word)


def page_of(s, pane):
    tag = s.tag(pane)
    return int(tag[tag.index('[') + 1:tag.index('/', tag.index('['))])


def pdf_navigation(s):
    """Page through the Intel SDM, jump to CPUID from its sections, search
    a word and step through the hits."""
    pdf = next(n for n, kind in s.panes().items() if kind == 'pdf')
    s.pointer = s.cell_px(44, 12)
    s.shot(1.2)
    s.press([('\x06', 'Ctrl-f'), ('\x06', 'Ctrl-f Ctrl-f')], per_key=.7)
    s.click(2, *pdf_tag(s, 'PdfSections'), done=lambda: len(s.panes()) == 2, what='the sections', after=.8)
    listing = next(n for n in s.panes() if n != pdf)
    top = s.find('+PdfSections', 0)[1]
    s.write('/focus', f'{listing}\n')
    s.badge = '200G'
    for key in '200G':
        s.keys(key)
    s.wait(lambda: any('CPUID' in line for line in s.screen()[top:]), 'the CPUID row')
    s.shot(1.0)
    s.badge = None
    # A row's file.pdf:PAGE:SECTION is the place to look at.
    col, row = s.find('pdf:317:200', after=top + 1)
    s.click(3, *s.cell_px(col + 4, row), done=lambda: page_of(s, pdf) == 317, what='page 317', after=1.2)
    s.click(2, *s.tag_word(listing, 'Del'), done=lambda: list(s.panes()) == [pdf], what='the listing to close',
            after=.8)
    s.write('/focus', f'{pdf}\n')
    s.badge = '/'
    s.keys('/')
    s.shot(.25)
    # Its first hits are on the pages after CPUID's first.
    s.type('apic id', lambda typed: True, per_key=.08)
    s.badge = '/apic id  Enter'
    s.keys('\r')
    s.wait(lambda: any(kind == 'text' for kind in s.panes().values()), 'the hits')
    s.shot(1.0)
    s.badge = None
    hits = next(n for n, kind in s.panes().items() if kind == 'text')
    # The hit list folds to its tag, and the page has the column again.
    s.click(2, *s.tag_word(hits, 'Collapse'), done=lambda: 'collapsed' in s.read(f'/pane/{hits}/ctl'),
            what='the hits to fold', after=.8)
    s.write('/focus', f'{pdf}\n')
    for k in range(1, 4):
        s.badge = ' '.join(['n'] * k)
        s.keys('n')
        s.shot(.8)
    s.badge = None
    s.shots[-1]['secs'] = 1.6
    s.shots[-1]['poster'] = True


def pdf_tint_setup(s):
    """CPUID's page, and two words of the user's in the tag: PdfFit and a
    page address (MOV's)."""
    s.write('/pane/1/look', f'{PDF[0]}:317\n')
    s.wait(lambda: page_of(s, 1) == 317, 'page 317')
    s.write('/pane/1/tag', f' PdfFit {PDF[0].name}:762')
    pdf_setup(s)


def pdf_tint_fit(s):
    """PdfTint through its three looks, PdfFit both ways, then a look at
    a page address."""
    s.pointer = s.cell_px(44, 12)
    s.shot(1.2)
    for _ in range(3):
        before = s.read('/pane/1/ctl').split()[-1]
        s.click(2, *pdf_tag(s, 'PdfTint'), done=lambda: s.read('/pane/1/ctl').split()[-1] != before,
                what='the tint', after=1.0)
    for fit in ('height', 'width'):
        s.click(2, *pdf_tag(s, 'PdfFit'), done=lambda: f'fit:{fit}' in s.read('/pane/1/ctl'), what='the fit',
                after=1.3)
    s.click(3, *pdf_tag(s, f'{PDF[0].name}:762'), done=lambda: page_of(s, 1) == 762, what='page 762', after=2.0)
    s.shots[-1]['poster'] = True


THEMES = MEDIA / 'themes'
THEME_WIDTH = 640
THEME_MAX_BYTES = 120_000


def theme_scene(s):
    """One column: a C file with its syntax colours and a selection, a
    shell that ran make (gcc's colours), and git diff paged into +Pager."""
    env = dict(clean_env(), HOME=str(HOME), GIT_AUTHOR_NAME='demo', GIT_AUTHOR_EMAIL='[email protected]',
               GIT_COMMITTER_NAME='demo', GIT_COMMITTER_EMAIL='[email protected]')
    for args in (['init', '-q'], ['add', '.'], ['commit', '-qm', 'hello'], ['config', 'diff.context', '1']):
        subprocess.run(['git', '-C', str(PROJECT), *args], check=True, env=env, capture_output=True)
    main = PROJECT / 'src/main.c'
    main.write_text(main.read_text().replace('i <= 4', 'i <= 5'))
    s.write('/pane/1/exec', 'Tty\n')
    s.wait(lambda: 2 in s.panes(), 'the terminal')
    s.write('/pane/2/pty/data', f"cd {PROJECT}; PS1='$ '; clear; make; git diff\r")
    s.wait(lambda: len(s.panes()) == 3, 'the pager')
    s.wait(lambda: 'Error 1' in s.read('/pane/2/body'), 'make')
    s.write('/pane/1/dot', '12\n')
    # The pager shows its hunk, the end of the diff.
    pager = next(n for n, kind in s.panes().items() if n not in (1, 2))
    s.write(f'/pane/{pager}/dot', '$\n')


def theme_order(s):
    """The themes as the Themes builtin lists them, up to the imported
    ones: pardes's own, the ports and the legacy pair (src/themes)."""
    s.write('/ctl', 'Themes\n')
    s.wait(lambda: any(s.read(f'/pane/{n}/name').strip().endswith('+Themes') for n in s.panes()), '+Themes')
    pane = next(n for n in s.panes() if s.read(f'/pane/{n}/name').strip().endswith('+Themes'))
    names = []
    for line in s.read(f'/pane/{pane}/body').splitlines():
        if line.startswith('# Imported'):
            break
        if line.startswith('Theme '):
            names.append(line.split()[1])
    s.write(f'/pane/{pane}/ctl', 'Del\n')
    s.wait(lambda: pane not in s.panes(), '+Themes to close')
    return names


def save_theme_png(data, path):
    """The capture at THEME_WIDTH, as small a PNG as reads: full colour if it
    fits THEME_MAX_BYTES, else 256 colours. Answers light or dark, from the
    colour most of the window is."""
    import io
    img = Image.open(io.BytesIO(data)).convert('RGB')
    ground = max(img.getcolors(1 << 24), key=lambda c: c[0])[1]
    lum = sum(w * (v / 255) ** 2.2 for w, v in zip((.2126, .7152, .0722), ground))
    small = img.resize((THEME_WIDTH, round(img.height * THEME_WIDTH / img.width)), Image.LANCZOS)
    small.save(path, optimize=True)
    if path.stat().st_size > THEME_MAX_BYTES:
        small.quantize(256, method=Image.Quantize.MEDIANCUT, dither=Image.Dither.NONE).save(path, optimize=True)
    return 'light' if lum > .18 else 'dark'


def record_themes(binary, work):
    """docs/site/media/themes/<theme>.png for every theme of src/themes, in
    the Themes builtin's order, and themes.toml listing them."""
    make_project()
    s = Session(binary, work / 'themes', 'rec-themes', [PROJECT / 'src/main.c'], rows=22)
    try:
        theme_scene(s)
        names = theme_order(s)
        THEMES.mkdir(parents=True, exist_ok=True)
        rows = []
        for name in names:
            s.write('/ctl', f'Theme {name}\n')
            # A pointer move puts away the word's flash that a command
            # leaves up until the pointer moves.
            s.mouse('motion', 0, *s.cell_px(COLS - 2, 1))
            s.mouse('motion', 0, *s.cell_px(COLS - 3, 1))
            kind = save_theme_png(s.settle(), THEMES / f'{name}.png')
            rows.append((name, kind))
            print(f'{name}.png {kind} {(THEMES / f"{name}.png").stat().st_size / 1e3:.0f} KB', flush=True)
    finally:
        s.close()
    toml = ['# The Themes page gallery, in the order the Themes builtin walks them:',
            '# media/themes/<file> for each, made by test/record_demos.py themes.',
            'ready = true', '']
    for name, kind in rows:
        toml += ['[[theme]]', f'name = "{name}"', f'file = "{name}.png"', f'kind = "{kind}"', '']
    (THEMES / 'themes.toml').write_text('\n'.join(toml))


CLIPS = {
    'text-is-the-interface': dict(
        setup=hero_setup, run=text_is_the_interface, args=[PROJECT / 'README'],
        caption='Middle-click runs a word; right-click an error opens its line.'),
    'terminals-are-panes': dict(
        setup=shell_setup, run=terminals_are_panes, args=[PROJECT / 'src/main.c'],
        caption='A terminal is a pane: Ctrl-b, then move over its output.'),
    'scripting-over-9p': dict(
        setup=mounted_shell_setup, run=scripting_over_9p, args=[PROJECT / 'README'],
        caption='A script drives the session through its virtual filesystem (9P).'),
    'diff-review': dict(
        setup=diff_setup, run=diff_review, args=[PROJECT / 'README'],
        caption='Right-click a diff line to open that line of the file.'),
    'pdf-navigation': dict(
        setup=pdf_setup, run=pdf_navigation, args=pdf_args,
        caption='A 2,515-page PDF: page, jump to CPUID, search, step hits.'),
    'pdf-tint-fit': dict(
        setup=pdf_tint_setup, run=pdf_tint_fit, args=pdf_args,
        caption='PdfTint tints the page, PdfFit fits it, a page address jumps.'),
    'image-pane': dict(
        setup=image_setup, run=image_pane, args=[PROJECT / 'README'],
        caption="Right-click a picture's name to open it; Petscii redraws it."),
}


def record(binary, work, name):
    clip = CLIPS[name]
    make_project()
    args = clip['args']() if callable(clip['args']) else clip['args']
    s = Session(binary, work / name, 'rec-' + name, args, init=clip.get('init', ''))
    try:
        if 'setup' in clip:
            clip['setup'](s)
        clip['run'](s)
    finally:
        s.close()
    seconds, poster = compose(s.shots, work / name / 'lossless.mkv')
    files = encode(work / name / 'lossless.mkv', name, poster)
    for f in files:
        print(f'{f.name} {f.stat().st_size / 1e6:.2f} MB')
    print(f'{name}: {seconds:.1f} s')
    return seconds


def main():
    parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument('binary', type=Path, help='a pardes-gui built with -Dplatform=gui')
    parser.add_argument('clips', nargs='*',
                        help='which clips (default: all), or themes for the theme gallery: ' + ', '.join(CLIPS))
    parser.add_argument('--work', type=Path, default=REPO.parent / '.scratch/rec/work',
                        help='frames and intermediates (large; default %(default)s)')
    parser.add_argument('--theme', default=THEME[0], help='a theme name, or default (default %(default)s)')
    parser.add_argument('--pdf', type=Path, default=PDF[0],
                        help='the PDF the pdf clips page through, opened read-only (default %(default)s)')
    args = parser.parse_args()
    THEME[0] = args.theme
    PDF[0] = args.pdf.resolve()
    names = args.clips or list(CLIPS)
    for name in names:
        if name not in CLIPS and name != 'themes':
            parser.error(f'no clip {name!r}: ' + ', '.join(CLIPS))
    try:
        for name in names:
            if name == 'themes':
                record_themes(args.binary, args.work.resolve())
            else:
                record(args.binary, args.work.resolve(), name)
    finally:
        shutil.rmtree(PROJECT, ignore_errors=True)
        shutil.rmtree(HOME, ignore_errors=True)
    captions = MEDIA / 'captions.txt'
    lines = dict(line.split(': ', 1) for line in captions.read_text().splitlines() if ': ' in line) \
        if captions.exists() else {}
    for name in names:
        if name in CLIPS:
            lines[name] = CLIPS[name]['caption']
    captions.write_text(''.join(f'{name}: {lines[name]}\n' for name in CLIPS if name in lines))


if __name__ == '__main__':
    main()