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
master
Jules Villard 9 years ago committed by facebook-github-bot-7
parent 3b4dda102b
commit 6bf9f47c14

@ -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()

@ -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(

@ -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:

Loading…
Cancel
Save