63 lines
1.6 KiB
Python
63 lines
1.6 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Build static/js/mountain.js with all ASCII frames from a build/<name>/ dir
|
|
embedded as a JS array.
|
|
|
|
Usage: build_mountain_js.py <src_dir> <dst_js_path>
|
|
|
|
Frames are read in sorted order, escaped for JS template literals, and
|
|
concatenated into a window.TP_MOUNTAIN_FRAMES array.
|
|
"""
|
|
import os
|
|
import sys
|
|
|
|
|
|
def js_escape(s: str) -> str:
|
|
return s.replace("\\", "\\\\").replace("`", "\\`").replace("${", "\\${")
|
|
|
|
|
|
def main() -> int:
|
|
if len(sys.argv) != 3:
|
|
print(__doc__, file=sys.stderr)
|
|
return 2
|
|
|
|
src = sys.argv[1]
|
|
dst = sys.argv[2]
|
|
|
|
txts = sorted(p for p in os.listdir(src) if p.startswith("frame-") and p.endswith(".txt"))
|
|
if not txts:
|
|
print(f"no frame-*.txt files in {src}", file=sys.stderr)
|
|
return 1
|
|
|
|
frames = []
|
|
for name in txts:
|
|
with open(os.path.join(src, name)) as f:
|
|
frames.append(f.read().rstrip("\n"))
|
|
|
|
header = """/* Autogenerated by tools/build_mountain_js.py
|
|
ASCII frames from a video, chafa-rendered, concatenated into a JS array.
|
|
Cycled at 12 fps by static/js/mountain-bg.js. */
|
|
|
|
window.TP_MOUNTAIN_FRAMES = [
|
|
"""
|
|
footer = f"""];
|
|
|
|
window.TP_MOUNTAIN_N_FRAMES = {len(frames)};
|
|
window.TP_MOUNTAIN_FPS = 12;
|
|
"""
|
|
body = ",\n".join(f" `{js_escape(fr)}`" for fr in frames)
|
|
|
|
os.makedirs(os.path.dirname(dst), exist_ok=True)
|
|
with open(dst, "w") as f:
|
|
f.write(header)
|
|
f.write(body)
|
|
f.write("\n")
|
|
f.write(footer)
|
|
|
|
print(f" {os.path.getsize(dst):>7d} bytes {dst} ({len(frames)} frames)")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|