From 3f476821db38b4528b62fcee0a65c9dbc939c33e Mon Sep 17 00:00:00 2001 From: Akemi Izuko Date: Thu, 16 Nov 2023 20:57:10 -0700 Subject: [PATCH] Scripts: gazformat script --- scripts/format.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100755 scripts/format.py diff --git a/scripts/format.py b/scripts/format.py new file mode 100755 index 0000000..27d74f2 --- /dev/null +++ b/scripts/format.py @@ -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()