Scripts: gazformat script
This commit is contained in:
parent
2b54fe0263
commit
3f476821db
1 changed files with 44 additions and 0 deletions
44
scripts/format.py
Executable file
44
scripts/format.py
Executable 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()
|
Loading…
Reference in a new issue