@ -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 s yntax_highlighting( source_name , mode , s )
# vim: set sw=4 ts=4 et: