Skip to content

Commit

Permalink
Add colored (Clang-style) diagnostics.
Browse files Browse the repository at this point in the history
  • Loading branch information
whitequark committed Aug 7, 2015
1 parent bb6b143 commit db8947c
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions pythonparser/diagnostic.py
Expand Up @@ -57,7 +57,7 @@ def message(self):
"""
return self.reason.format(**self.arguments)

def render(self, only_line=False):
def render(self, only_line=False, colored=False):
"""
Returns the human-readable location of the diagnostic in the source,
the formatted message, the source line corresponding
Expand Down Expand Up @@ -91,11 +91,27 @@ def render(self, only_line=False):
else:
location = str(self.location)

return [
"%s: %s: %s" % (location, self.level, self.message()),
source_line,
highlight_line.decode("utf-8")
] + reduce(list.__add__, [note.render(only_line) for note in self.notes], [])
rendered_notes = reduce(list.__add__, [note.render(only_line, colored)
for note in self.notes], [])
if colored:
if self.level in ("error", "fatal"):
level_color = 31 # red
elif self.level == "warning":
level_color = 35 # magenta
else: # level == "note"
level_color = 30 # gray
return [
"\x1b[1;37m{}: \x1b[{}m{}:\x1b[37m {}\x1b[0m".
format(location, level_color, self.level, self.message()),
source_line,
"\x1b[1;32m{}\x1b[0m".format(highlight_line.decode("utf-8"))
]
else:
return [
"{}: {}: {}".format(location, self.level, self.message()),
source_line,
highlight_line.decode("utf-8")
]


class Error(Exception):
Expand Down

0 comments on commit db8947c

Please sign in to comment.