Skip to content

Instantly share code, notes, and snippets.

@jrha
Last active December 17, 2015 11:09
Show Gist options
  • Save jrha/5600336 to your computer and use it in GitHub Desktop.
Save jrha/5600336 to your computer and use it in GitHub Desktop.
Wrapper for pan ant tasks to generate more expressive error messages
#!/usr/bin/python2 -u
from sys import stdin, stdout
from re import compile
RE_ERROR = compile(r'\[panc\]([\w\s]+)\[(/.+):(\d+).(\d+)-(\d+).(\d+)\]')
RE_INFO = compile(r'\[panc\](.+)')
def caretline(pos, pre = 0, post = 0):
line = " " * (pos - 1 - pre)
line += "~" * pre
line += "^"
line += "~" * post
stdout.write(line + "\n")
def caretify(filename, s_row, s_col, e_row, e_col):
source = open(filename).readlines()
if s_row <= len(source) and e_row <= len(source):
for rownum in range(s_row, e_row):
row = source[rownum]
if stdout.isatty():
stdout.write("\033[32m" + row + "\033[0m")
else:
stdout.write(row)
if stdout.isatty():
stdout.write("\033[94m")
if rownum == s_row:
c = 0
if e_col > s_col:
c = e_col - s_col
caretline(s_col, 0, c)
if stdout.isatty():
stdout.write("\033[0m")
if __name__ == "__main__":
was_error = False
while True:
try:
line = stdin.readline()
except KeyboardInterrupt:
break
if not line:
break
is_error = RE_ERROR.search(line)
if is_error:
message, filename, s_row, s_col, e_row, e_col = is_error.groups()
s_row, s_col, e_row, e_col = int(s_row), int(s_col), int(e_row), int(e_col)
if stdout.isatty():
stdout.write(line.replace(message, "\033[91m" + message + "\033[0m"))
else:
stdout.write(line)
caretify(filename, s_row - 1, s_col, e_row, e_col)
elif was_error:
was_error = RE_INFO.search(line)
if was_error:
message, = was_error.groups()
stdout.write(line.replace(message, "\033[93m" + message + "\033[0m"))
else:
stdout.write(line)
else:
stdout.write(line)
was_error = is_error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment