#!/usr/bin/env python3 """Generate crates/ghostty-macos/src/ghostty/capi/consts.rs from crates/ghostty-lib/include/ghostty.h: every enumerator becomes a `pub const NAME: c_int` (the header's enums are C `int`s).""" import re, sys, pathlib root = pathlib.Path(__file__).resolve().parent.parent hdr = (root / "crates/ghostty-lib/include/ghostty.h").read_text() hdr = re.sub(r"/\*.*?\*/", "", hdr, flags=re.S) hdr = re.sub(r"//[^\n]*", "", hdr) out = [ "//! GENERATED by scripts/gen-macos-capi-consts.py from include/ghostty.h; do not edit.", "//!", "//! The enumerators of every C enum of the header (all `int`).", "#![allow(missing_docs)]", "", "use std::ffi::c_int;", "", ] for m in re.finditer(r"typedef\s+enum\s*(?:\w+\s*)?\{(.*?)\}\s*(\w+)\s*;", hdr, flags=re.S): body, name = m.group(1), m.group(2) out.append(f"// {name}") nxt = 0 for item in [x.strip() for x in body.split(",") if x.strip()]: if "=" in item: n, v = [x.strip() for x in item.split("=", 1)] v = v.replace("UL", "").replace("U", "") nxt = eval(v) else: n = item out.append(f"pub const {n}: c_int = {nxt};") nxt += 1 out.append("") sys.stdout.write("\n".join(out))