forked from HalfVoxel/headway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
record.py
executable file
·53 lines (43 loc) · 1.84 KB
/
record.py
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
#!/bin/env python3
from asyncio import subprocess
from subprocess import run, Popen, PIPE
import shutil
import os
import base64
import re
demos = [("simple", 1), ("message", 1), ("multiple", 5), ("split_weighted", 1), ("abandonment", 3),
("print_during_progress", 11), ("split_each", 1), ("split_summed", 1), ("split_sized", 1), ("indeterminate", 1)]
# Fill with dummy files to allow cargo to compile everything
for (demo, _) in demos:
with open("images/" + demo + ".html", "wb") as f:
f.write(b"temp")
run("cargo build --release --examples", shell=True, check=False)
for file in os.listdir("images"):
if file.endswith(".html"):
os.remove("images/" + file)
svgs = [
Popen(["svg-term", "--command", f"target/release/examples/{demo}", "--no-cursor",
"--width", "60", "--height", str(height)], stdout=PIPE)
for (demo, height) in demos
]
for ((demo, height), svg) in zip(demos, svgs):
assert svg.stdout is not None
out = svg.stdout.read().decode('utf-8')
svg.wait()
# Add a delay at the end of the animation
end_delay = 2
duration = float(re.search("animation-duration:\s*([\d\.]+)s", out).group(1))
new_duration = duration + end_delay
multiplier = duration / new_duration
out = re.sub(r"}([\d\.]+)%", lambda m: f"}}{round(float(m.group(1))*multiplier, 2)}%", out)
out = re.sub(r"animation-duration:\s*([\d\.]+)s",
lambda m: f"animation-duration: {new_duration}s", out)
out = re.sub(r"to(\{.*?\})",
fr"{round(100*multiplier,2)}%\1 to\1", out)
with open("images/" + demo + ".svg", "wb") as f:
f.write(out.encode('utf-8'))
encoded = base64.b64encode(out.encode('utf-8'))
with open("images/" + demo + ".html", "wb") as f:
f.write(b"<img src=\"data:image/svg+xml;base64,")
f.write(encoded)
f.write(b"\" />")