ThirdParty/Pygments/pygments/formatters/terminal256.py

changeset 5713
6762afd9f963
parent 4697
c2e9bf425554
equal deleted inserted replaced
5712:f0d08bdeacf4 5713:6762afd9f963
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-2015 by the Pygments team, see AUTHORS. 14 :copyright: Copyright 2006-2017 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
25 # to "white background, black foreground", etc... 25 # to "white background, black foreground", etc...
26 26
27 import sys 27 import sys
28 28
29 from pygments.formatter import Formatter 29 from pygments.formatter import Formatter
30 from pygments.console import codes
31 from pygments.style import ansicolors
30 32
31 33
32 __all__ = ['Terminal256Formatter', 'TerminalTrueColorFormatter'] 34 __all__ = ['Terminal256Formatter', 'TerminalTrueColorFormatter']
33 35
34 36
45 return "" 47 return ""
46 48
47 def color_string(self): 49 def color_string(self):
48 attrs = [] 50 attrs = []
49 if self.fg is not None: 51 if self.fg is not None:
50 attrs.extend(("38", "5", "%i" % self.fg)) 52 if self.fg in ansicolors:
53 esc = codes[self.fg[5:]]
54 if ';01m' in esc:
55 self.bold = True
56 # extract fg color code.
57 attrs.append(esc[2:4])
58 else:
59 attrs.extend(("38", "5", "%i" % self.fg))
51 if self.bg is not None: 60 if self.bg is not None:
52 attrs.extend(("48", "5", "%i" % self.bg)) 61 if self.bg in ansicolors:
62 esc = codes[self.bg[5:]]
63 # extract fg color code, add 10 for bg.
64 attrs.append(str(int(esc[2:4])+10))
65 else:
66 attrs.extend(("48", "5", "%i" % self.bg))
53 if self.bold: 67 if self.bold:
54 attrs.append("01") 68 attrs.append("01")
55 if self.underline: 69 if self.underline:
56 attrs.append("04") 70 attrs.append("04")
57 return self.escape(attrs) 71 return self.escape(attrs)
88 The formatter takes colors from a style defined by the `style` option 102 The formatter takes colors from a style defined by the `style` option
89 and converts them to nearest ANSI 256-color escape sequences. Bold and 103 and converts them to nearest ANSI 256-color escape sequences. Bold and
90 underline attributes from the style are preserved (and displayed). 104 underline attributes from the style are preserved (and displayed).
91 105
92 .. versionadded:: 0.9 106 .. versionadded:: 0.9
107
108 .. versionchanged:: 2.2
109 If the used style defines foreground colors in the form ``#ansi*``, then
110 `Terminal256Formatter` will map these to non extended foreground color.
111 See :ref:`AnsiTerminalStyle` for more information.
93 112
94 Options accepted: 113 Options accepted:
95 114
96 `style` 115 `style`
97 The style to use, can be a string or a Style subclass (default: 116 The style to use, can be a string or a Style subclass (default:
167 distance = d 186 distance = d
168 return match 187 return match
169 188
170 def _color_index(self, color): 189 def _color_index(self, color):
171 index = self.best_match.get(color, None) 190 index = self.best_match.get(color, None)
191 if color in ansicolors:
192 # strip the `#ansi` part and look up code
193 index = color
194 self.best_match[color] = index
172 if index is None: 195 if index is None:
173 try: 196 try:
174 rgb = int(str(color), 16) 197 rgb = int(str(color), 16)
175 except ValueError: 198 except ValueError:
176 rgb = 0 199 rgb = 0
183 return index 206 return index
184 207
185 def _setup_styles(self): 208 def _setup_styles(self):
186 for ttype, ndef in self.style: 209 for ttype, ndef in self.style:
187 escape = EscapeSequence() 210 escape = EscapeSequence()
188 if ndef['color']: 211 # get foreground from ansicolor if set
212 if ndef['ansicolor']:
213 escape.fg = self._color_index(ndef['ansicolor'])
214 elif ndef['color']:
189 escape.fg = self._color_index(ndef['color']) 215 escape.fg = self._color_index(ndef['color'])
190 if ndef['bgcolor']: 216 if ndef['bgansicolor']:
217 escape.bg = self._color_index(ndef['bgansicolor'])
218 elif ndef['bgcolor']:
191 escape.bg = self._color_index(ndef['bgcolor']) 219 escape.bg = self._color_index(ndef['bgcolor'])
192 if self.usebold and ndef['bold']: 220 if self.usebold and ndef['bold']:
193 escape.bold = True 221 escape.bold = True
194 if self.useunderline and ndef['underline']: 222 if self.useunderline and ndef['underline']:
195 escape.underline = True 223 escape.underline = True

eric ide

mercurial