Created
August 21, 2017 02:34
-
-
Save TheJJ/d5072e8b3718a1421a4a1f8f31872124 to your computer and use it in GitHub Desktop.
c++ template error message decoder and colorizer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
C++ template error message decoder and colorizer. | |
Copyright (C) 2017 Jonas Jelten <[email protected]> | |
Licensed under GNU GPLv3 or later. | |
""" | |
import argparse | |
import sys | |
def main(): | |
""" | |
Decode and colorize template error messages. | |
It can surely be improved, but it was written while in rage. | |
""" | |
cmd = argparse.ArgumentParser() | |
cmd.add_argument("-c", "--no-color", action="store_true", | |
help="don't colorize output") | |
args = cmd.parse_args() | |
print("pls giev template data on stdin...") | |
data = [] | |
while True: | |
read = sys.stdin.buffer.read() | |
if not read: | |
break | |
data.append(read) | |
indent_per_level = 4 | |
indent = 0 | |
in_quotes = False | |
opentags = 0 | |
current_line = bytearray() | |
output = [] | |
for group in data: | |
for char in group: | |
if char == ord("\n"): | |
continue | |
elif char == ord(","): | |
# newline after , | |
current_line.append(char) | |
output.append((indent, current_line)) | |
current_line = bytearray() | |
elif char == ord("'"): | |
# newline after ' if in quotes | |
if in_quotes: | |
output.append((indent, current_line)) | |
current_line = bytearray() | |
current_line.append(char) | |
in_quotes = False | |
else: | |
current_line.append(char) | |
output.append((indent, current_line)) | |
current_line = bytearray() | |
in_quotes = True | |
elif char == ord("<"): | |
# newline after < then indent | |
current_line.append(char) | |
output.append((indent, current_line)) | |
current_line = bytearray() | |
indent += indent_per_level | |
opentags += 1 | |
elif char == ord(">"): | |
# newline before > then dedent | |
output.append((indent, current_line)) | |
current_line = bytearray() | |
current_line.append(char) | |
indent -= indent_per_level | |
if opentags == 0: | |
print("ERR: have more closing > than opening ones!") | |
opentags -= 1 | |
else: | |
current_line.append(char) | |
if current_line: | |
output.append((indent, current_line)) | |
if opentags != 0: | |
print("ERR: unbalanced < and > count!") | |
for indent, line in output: | |
if not args.no_color: | |
color = str(31 + (indent//indent_per_level) % 8).encode() | |
sys.stdout.buffer.write(b"\x1b[%sm" % color) | |
for elem in (b" " * indent, line.strip(), b"\n"): | |
sys.stdout.buffer.write(elem) | |
if not args.no_color: | |
sys.stdout.buffer.write(b"\x1b[m") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment