24 #: Map token types to a tuple of color values for light and dark |
24 #: Map token types to a tuple of color values for light and dark |
25 #: backgrounds. |
25 #: backgrounds. |
26 TERMINAL_COLORS = { |
26 TERMINAL_COLORS = { |
27 Token: ('', ''), |
27 Token: ('', ''), |
28 |
28 |
29 Whitespace: ('lightgray', 'darkgray'), |
29 Whitespace: ('gray', 'brightblack'), |
30 Comment: ('lightgray', 'darkgray'), |
30 Comment: ('gray', 'brightblack'), |
31 Comment.Preproc: ('teal', 'turquoise'), |
31 Comment.Preproc: ('cyan', 'brightcyan'), |
32 Keyword: ('darkblue', 'blue'), |
32 Keyword: ('blue', 'brightblue'), |
33 Keyword.Type: ('teal', 'turquoise'), |
33 Keyword.Type: ('cyan', 'brightcyan'), |
34 Operator.Word: ('purple', 'fuchsia'), |
34 Operator.Word: ('magenta', 'brightmagenta'), |
35 Name.Builtin: ('teal', 'turquoise'), |
35 Name.Builtin: ('cyan', 'brightcyan'), |
36 Name.Function: ('darkgreen', 'green'), |
36 Name.Function: ('green', 'brightgreen'), |
37 Name.Namespace: ('_teal_', '_turquoise_'), |
37 Name.Namespace: ('_cyan_', '_brightcyan_'), |
38 Name.Class: ('_darkgreen_', '_green_'), |
38 Name.Class: ('_green_', '_brightgreen_'), |
39 Name.Exception: ('teal', 'turquoise'), |
39 Name.Exception: ('cyan', 'brightcyan'), |
40 Name.Decorator: ('darkgray', 'lightgray'), |
40 Name.Decorator: ('brightblack', 'gray'), |
41 Name.Variable: ('darkred', 'red'), |
41 Name.Variable: ('red', 'brightred'), |
42 Name.Constant: ('darkred', 'red'), |
42 Name.Constant: ('red', 'brightred'), |
43 Name.Attribute: ('teal', 'turquoise'), |
43 Name.Attribute: ('cyan', 'brightcyan'), |
44 Name.Tag: ('blue', 'blue'), |
44 Name.Tag: ('brightblue', 'brightblue'), |
45 String: ('brown', 'brown'), |
45 String: ('yellow', 'yellow'), |
46 Number: ('darkblue', 'blue'), |
46 Number: ('blue', 'brightblue'), |
47 |
47 |
48 Generic.Deleted: ('red', 'red'), |
48 Generic.Deleted: ('brightred', 'brightred'), |
49 Generic.Inserted: ('darkgreen', 'green'), |
49 Generic.Inserted: ('green', 'brightgreen'), |
50 Generic.Heading: ('**', '**'), |
50 Generic.Heading: ('**', '**'), |
51 Generic.Subheading: ('*purple*', '*fuchsia*'), |
51 Generic.Subheading: ('*magenta*', '*brightmagenta*'), |
52 Generic.Prompt: ('**', '**'), |
52 Generic.Prompt: ('**', '**'), |
53 Generic.Error: ('red', 'red'), |
53 Generic.Error: ('brightred', 'brightred'), |
54 |
54 |
55 Error: ('_red_', '_red_'), |
55 Error: ('_brightred_', '_brightred_'), |
56 } |
56 } |
57 |
57 |
58 |
58 |
59 class TerminalFormatter(Formatter): |
59 class TerminalFormatter(Formatter): |
60 r""" |
60 r""" |
90 self.colorscheme = options.get('colorscheme', None) or TERMINAL_COLORS |
90 self.colorscheme = options.get('colorscheme', None) or TERMINAL_COLORS |
91 self.linenos = options.get('linenos', False) |
91 self.linenos = options.get('linenos', False) |
92 self._lineno = 0 |
92 self._lineno = 0 |
93 |
93 |
94 def format(self, tokensource, outfile): |
94 def format(self, tokensource, outfile): |
95 # hack: if the output is a terminal and has an encoding set, |
|
96 # use that to avoid unicode encode problems |
|
97 if not self.encoding and hasattr(outfile, "encoding") and \ |
|
98 hasattr(outfile, "isatty") and outfile.isatty() and \ |
|
99 sys.version_info < (3,): |
|
100 self.encoding = outfile.encoding |
|
101 return Formatter.format(self, tokensource, outfile) |
95 return Formatter.format(self, tokensource, outfile) |
102 |
96 |
103 def _write_lineno(self, outfile): |
97 def _write_lineno(self, outfile): |
104 self._lineno += 1 |
98 self._lineno += 1 |
105 outfile.write("%s%04d: " % (self._lineno != 1 and '\n' or '', self._lineno)) |
99 outfile.write("%s%04d: " % (self._lineno != 1 and '\n' or '', self._lineno)) |