3 pygments.formatters.terminal |
3 pygments.formatters.terminal |
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
5 |
5 |
6 Formatter for terminal output with ANSI sequences. |
6 Formatter for terminal output with ANSI sequences. |
7 |
7 |
8 :copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS. |
8 :copyright: Copyright 2006-2015 by the Pygments team, see AUTHORS. |
9 :license: BSD, see LICENSE for details. |
9 :license: BSD, see LICENSE for details. |
10 """ |
10 """ |
11 |
11 |
12 import sys |
12 import sys |
13 |
13 |
47 |
47 |
48 Generic.Deleted: ('red', 'red'), |
48 Generic.Deleted: ('red', 'red'), |
49 Generic.Inserted: ('darkgreen', 'green'), |
49 Generic.Inserted: ('darkgreen', 'green'), |
50 Generic.Heading: ('**', '**'), |
50 Generic.Heading: ('**', '**'), |
51 Generic.Subheading: ('*purple*', '*fuchsia*'), |
51 Generic.Subheading: ('*purple*', '*fuchsia*'), |
|
52 Generic.Prompt: ('**', '**'), |
52 Generic.Error: ('red', 'red'), |
53 Generic.Error: ('red', 'red'), |
53 |
54 |
54 Error: ('_red_', '_red_'), |
55 Error: ('_red_', '_red_'), |
55 } |
56 } |
56 |
57 |
99 self.encoding = outfile.encoding |
100 self.encoding = outfile.encoding |
100 return Formatter.format(self, tokensource, outfile) |
101 return Formatter.format(self, tokensource, outfile) |
101 |
102 |
102 def _write_lineno(self, outfile): |
103 def _write_lineno(self, outfile): |
103 self._lineno += 1 |
104 self._lineno += 1 |
104 outfile.write("\n%04d: " % self._lineno) |
105 outfile.write("%s%04d: " % (self._lineno != 1 and '\n' or '', self._lineno)) |
105 |
106 |
106 def _format_unencoded_with_lineno(self, tokensource, outfile): |
107 def _get_color(self, ttype): |
107 self._write_lineno(outfile) |
108 # self.colorscheme is a dict containing usually generic types, so we |
108 |
109 # have to walk the tree of dots. The base Token type must be a key, |
109 for ttype, value in tokensource: |
110 # even if it's empty string, as in the default above. |
110 if value.endswith("\n"): |
111 colors = self.colorscheme.get(ttype) |
111 self._write_lineno(outfile) |
112 while colors is None: |
112 value = value[:-1] |
113 ttype = ttype.parent |
113 color = self.colorscheme.get(ttype) |
114 colors = self.colorscheme.get(ttype) |
114 while color is None: |
115 return colors[self.darkbg] |
115 ttype = ttype[:-1] |
|
116 color = self.colorscheme.get(ttype) |
|
117 if color: |
|
118 color = color[self.darkbg] |
|
119 spl = value.split('\n') |
|
120 for line in spl[:-1]: |
|
121 self._write_lineno(outfile) |
|
122 if line: |
|
123 outfile.write(ansiformat(color, line[:-1])) |
|
124 if spl[-1]: |
|
125 outfile.write(ansiformat(color, spl[-1])) |
|
126 else: |
|
127 outfile.write(value) |
|
128 |
|
129 outfile.write("\n") |
|
130 |
116 |
131 def format_unencoded(self, tokensource, outfile): |
117 def format_unencoded(self, tokensource, outfile): |
132 if self.linenos: |
118 if self.linenos: |
133 self._format_unencoded_with_lineno(tokensource, outfile) |
119 self._write_lineno(outfile) |
134 return |
|
135 |
120 |
136 for ttype, value in tokensource: |
121 for ttype, value in tokensource: |
137 color = self.colorscheme.get(ttype) |
122 color = self._get_color(ttype) |
138 while color is None: |
123 |
139 ttype = ttype[:-1] |
124 for line in value.splitlines(True): |
140 color = self.colorscheme.get(ttype) |
125 if color: |
141 if color: |
126 outfile.write(ansiformat(color, line.rstrip('\n'))) |
142 color = color[self.darkbg] |
127 else: |
143 spl = value.split('\n') |
128 outfile.write(line.rstrip('\n')) |
144 for line in spl[:-1]: |
129 if line.endswith('\n'): |
145 if line: |
130 if self.linenos: |
146 outfile.write(ansiformat(color, line)) |
131 self._write_lineno(outfile) |
147 outfile.write('\n') |
132 else: |
148 if spl[-1]: |
133 outfile.write('\n') |
149 outfile.write(ansiformat(color, spl[-1])) |
134 |
150 else: |
135 if self.linenos: |
151 outfile.write(value) |
136 outfile.write("\n") |