From 6bf9f47c146ef654d7e2d8ce7fce3f50213d36f3 Mon Sep 17 00:00:00 2001 From: Jules Villard Date: Mon, 9 Nov 2015 08:54:17 -0800 Subject: [PATCH] add colored output Summary: public Add colored output if pygments is installed on the system. Reviewed By: cristianoc Differential Revision: D2601509 fb-gh-sync-id: 0fa8e9f --- infer/lib/python/inferTraceBugs | 7 ++++++- infer/lib/python/inferlib/infer.py | 6 ++++-- infer/lib/python/inferlib/utils.py | 27 +++++++++++++++++++++++++-- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/infer/lib/python/inferTraceBugs b/infer/lib/python/inferTraceBugs index 326649511..ec5a1faeb 100755 --- a/infer/lib/python/inferTraceBugs +++ b/infer/lib/python/inferTraceBugs @@ -108,7 +108,12 @@ class Tracer(object): if not self.args.no_source: self.indenter.indent_push(node[utils.JSON_INDEX_TRACE_LEVEL]) - self.indenter.add(utils.build_source_context(fname, report_line)) + mode = utils.TERMINAL_FORMATTER + if self.args.html: + mode = utils.PLAIN_FORMATTER + self.indenter.add(utils.build_source_context(fname, + mode, + report_line)) self.indenter.indent_pop() self.indenter.newline() diff --git a/infer/lib/python/inferlib/infer.py b/infer/lib/python/inferlib/infer.py index a9891ca1a..6174ab8b2 100644 --- a/infer/lib/python/inferlib/infer.py +++ b/infer/lib/python/inferlib/infer.py @@ -365,8 +365,10 @@ def print_errors(csv_report, bugs_out): msg = row[utils.CSV_INDEX_QUALIFIER] indenter = utils.Indenter() indenter.indent_push() - indenter.add(utils.build_source_context(filename, - int(line))) + indenter.add( + utils.build_source_context(filename, + utils.TERMINAL_FORMATTER, + int(line))) source_context = str(indenter) text_errors_list.append( '{0}:{1}: {2}: {3}\n {4}\n{5}'.format( diff --git a/infer/lib/python/inferlib/utils.py b/infer/lib/python/inferlib/utils.py index 7bb0b4a4b..3ca081589 100644 --- a/infer/lib/python/inferlib/utils.py +++ b/infer/lib/python/inferlib/utils.py @@ -18,6 +18,12 @@ import json import logging import os import re +try: + import pygments + import pygments.formatters + import pygments.lexers +except ImportError: + pygments = None import subprocess import sys import tempfile @@ -101,6 +107,10 @@ BASE_INDENT = 2 # how many lines of context around each report SOURCE_CONTEXT = 2 +# syntax highlighting modes +PLAIN_FORMATTER = 0 +TERMINAL_FORMATTER = 1 + # Monkey patching subprocess (I'm so sorry!). if "check_output" not in dir(subprocess): @@ -449,7 +459,20 @@ class Indenter(str): return self.text -def build_source_context(source_name, report_line): +def syntax_highlighting(source_name, mode, s): + if pygments is None or mode == PLAIN_FORMATTER: + return s + + lexer = pygments.lexers.get_lexer_for_filename(source_name) + formatter = None + if mode == TERMINAL_FORMATTER: + if not sys.stdout.isatty(): + return s + formatter = pygments.formatters.TerminalFormatter() + return pygments.highlight(s, lexer, formatter) + + +def build_source_context(source_name, mode, report_line): start_line = max(1, report_line - SOURCE_CONTEXT) # could go beyond last line, checked in the loop end_line = report_line + SOURCE_CONTEXT @@ -466,6 +489,6 @@ def build_source_context(source_name, report_line): caret = '> ' s += num + '. ' + caret + line line_number += 1 - return s + return syntax_highlighting(source_name, mode, s) # vim: set sw=4 ts=4 et: