walpurgis/scripts/format.py

45 lines
981 B
Python
Executable file

#!/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()