ThirdParty/Pygments/pygments/formatters/terminal256.py

changeset 4697
c2e9bf425554
parent 4172
4f20dba37ab6
child 5713
6762afd9f963
equal deleted inserted replaced
4696:bf4d19a7cade 4697:c2e9bf425554
9 tool (http://frexx.de/xterm-256-notes/data/xterm256-conv2.tar.bz2) 9 tool (http://frexx.de/xterm-256-notes/data/xterm256-conv2.tar.bz2)
10 by Wolfgang Frisch. 10 by Wolfgang Frisch.
11 11
12 Formatter version 1. 12 Formatter version 1.
13 13
14 :copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS. 14 :copyright: Copyright 2006-2015 by the Pygments team, see AUTHORS.
15 :license: BSD, see LICENSE for details. 15 :license: BSD, see LICENSE for details.
16 """ 16 """
17 17
18 # TODO: 18 # TODO:
19 # - Options to map style's bold/underline/italic/border attributes 19 # - Options to map style's bold/underline/italic/border attributes
27 import sys 27 import sys
28 28
29 from pygments.formatter import Formatter 29 from pygments.formatter import Formatter
30 30
31 31
32 __all__ = ['Terminal256Formatter'] 32 __all__ = ['Terminal256Formatter', 'TerminalTrueColorFormatter']
33 33
34 34
35 class EscapeSequence: 35 class EscapeSequence:
36 def __init__(self, fg=None, bg=None, bold=False, underline=False): 36 def __init__(self, fg=None, bg=None, bold=False, underline=False):
37 self.fg = fg 37 self.fg = fg
54 attrs.append("01") 54 attrs.append("01")
55 if self.underline: 55 if self.underline:
56 attrs.append("04") 56 attrs.append("04")
57 return self.escape(attrs) 57 return self.escape(attrs)
58 58
59 def true_color_string(self):
60 attrs = []
61 if self.fg:
62 attrs.extend(("38", "2", str(self.fg[0]), str(self.fg[1]), str(self.fg[2])))
63 if self.bg:
64 attrs.extend(("48", "2", str(self.bg[0]), str(self.bg[1]), str(self.bg[2])))
65 if self.bold:
66 attrs.append("01")
67 if self.underline:
68 attrs.append("04")
69 return self.escape(attrs)
70
59 def reset_string(self): 71 def reset_string(self):
60 attrs = [] 72 attrs = []
61 if self.fg is not None: 73 if self.fg is not None:
62 attrs.append("39") 74 attrs.append("39")
63 if self.bg is not None: 75 if self.bg is not None:
66 attrs.append("00") 78 attrs.append("00")
67 return self.escape(attrs) 79 return self.escape(attrs)
68 80
69 81
70 class Terminal256Formatter(Formatter): 82 class Terminal256Formatter(Formatter):
71 r""" 83 """
72 Format tokens with ANSI color sequences, for output in a 256-color 84 Format tokens with ANSI color sequences, for output in a 256-color
73 terminal or console. Like in `TerminalFormatter` color sequences 85 terminal or console. Like in `TerminalFormatter` color sequences
74 are terminated at newlines, so that paging the output works correctly. 86 are terminated at newlines, so that paging the output works correctly.
75 87
76 The formatter takes colors from a style defined by the `style` option 88 The formatter takes colors from a style defined by the `style` option
77 and converts them to nearest ANSI 256-color escape sequences. Bold and 89 and converts them to nearest ANSI 256-color escape sequences. Bold and
78 underline attributes from the style are preserved (and displayed). 90 underline attributes from the style are preserved (and displayed).
219 ttype = ttype[:-1] 231 ttype = ttype[:-1]
220 # outfile.write( '!' + str(ottype) + '->' + str(ttype) + '!' ) 232 # outfile.write( '!' + str(ottype) + '->' + str(ttype) + '!' )
221 233
222 if not_found: 234 if not_found:
223 outfile.write(value) 235 outfile.write(value)
236
237
238 class TerminalTrueColorFormatter(Terminal256Formatter):
239 r"""
240 Format tokens with ANSI color sequences, for output in a true-color
241 terminal or console. Like in `TerminalFormatter` color sequences
242 are terminated at newlines, so that paging the output works correctly.
243
244 .. versionadded:: 2.1
245
246 Options accepted:
247
248 `style`
249 The style to use, can be a string or a Style subclass (default:
250 ``'default'``).
251 """
252 name = 'TerminalTrueColor'
253 aliases = ['terminal16m', 'console16m', '16m']
254 filenames = []
255
256 def _build_color_table(self):
257 pass
258
259 def _color_tuple(self, color):
260 try:
261 rgb = int(str(color), 16)
262 except ValueError:
263 return None
264 r = (rgb >> 16) & 0xff
265 g = (rgb >> 8) & 0xff
266 b = rgb & 0xff
267 return (r, g, b)
268
269 def _setup_styles(self):
270 for ttype, ndef in self.style:
271 escape = EscapeSequence()
272 if ndef['color']:
273 escape.fg = self._color_tuple(ndef['color'])
274 if ndef['bgcolor']:
275 escape.bg = self._color_tuple(ndef['bgcolor'])
276 if self.usebold and ndef['bold']:
277 escape.bold = True
278 if self.useunderline and ndef['underline']:
279 escape.underline = True
280 self.style_string[str(ttype)] = (escape.true_color_string(),
281 escape.reset_string())

eric ide

mercurial