Scripts: gazformat script

This commit is contained in:
Akemi Izuko 2023-11-16 20:57:10 -07:00
parent 2b54fe0263
commit 3f476821db
Signed by: akemi
GPG key ID: 8DE0764E1809E9FC

44
scripts/format.py Executable file
View file

@ -0,0 +1,44 @@
#!/usr/bin/env python3
import argparse, sys
# Parse args
parser = argparse.ArgumentParser(
prog="GazFormat v0.1.0",
description="Really basic formatting for compressed gazprea code",
)
parser.add_argument(
"file",
nargs="?",
type=argparse.FileType("r"),
help="File to read from, instead of stdin",
)
args = parser.parse_args()
if args.file is not None:
file = args.file
elif not sys.stdin.isatty():
file = sys.stdin
else:
print("Please provide a file or pipe in text to fit")
exit(1)
indent_level = 0
indent_width = 4
while True:
c = file.read(1)
if not c:
break
print(c, end="")
if c == ";":
print("\n" + " " * indent_level * indent_width, end="")
elif c == "{":
indent_level += 1
print("\n" + " " * indent_level * indent_width, end="")
elif c == "}":
indent_level = max(0, indent_level - 1)
print("\n" + " " * indent_level * indent_width, end="")
file.close()