]>
jfr.im git - dlqueue.git/blob - venv/lib/python3.11/site-packages/pip/_vendor/pygments/formatters/terminal256.py
2 pygments.formatters.terminal256
3 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 Formatter for 256-color terminal output with ANSI sequences.
7 RGB-to-XTERM color conversion routines adapted from xterm256-conv
8 tool (http://frexx.de/xterm-256-notes/data/xterm256-conv2.tar.bz2)
13 :copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS.
14 :license: BSD, see LICENSE for details.
18 # - Options to map style's bold/underline/italic/border attributes
19 # to some ANSI attrbutes (something like 'italic=underline')
20 # - An option to output "style RGB to xterm RGB/index" conversion table
21 # - An option to indicate that we are running in "reverse background"
22 # xterm. This means that default colors are white-on-black, not
23 # black-on-while, so colors like "white background" need to be converted
24 # to "white background, black foreground", etc...
26 from pip
._vendor
.pygments
.formatter
import Formatter
27 from pip
._vendor
.pygments
.console
import codes
28 from pip
._vendor
.pygments
.style
import ansicolors
31 __all__
= ['Terminal256Formatter', 'TerminalTrueColorFormatter']
35 def __init__(self
, fg
=None, bg
=None, bold
=False, underline
=False, italic
=False):
39 self
.underline
= underline
42 def escape(self
, attrs
):
44 return "\x1b[" + ";".join(attrs
) + "m"
47 def color_string(self
):
49 if self
.fg
is not None:
50 if self
.fg
in ansicolors
:
51 esc
= codes
[self
.fg
.replace('ansi','')]
54 # extract fg color code.
55 attrs
.append(esc
[2:4])
57 attrs
.extend(("38", "5", "%i" % self
.fg
))
58 if self
.bg
is not None:
59 if self
.bg
in ansicolors
:
60 esc
= codes
[self
.bg
.replace('ansi','')]
61 # extract fg color code, add 10 for bg.
62 attrs
.append(str(int(esc
[2:4])+10))
64 attrs
.extend(("48", "5", "%i" % self
.bg
))
71 return self
.escape(attrs
)
73 def true_color_string(self
):
76 attrs
.extend(("38", "2", str(self
.fg
[0]), str(self
.fg
[1]), str(self
.fg
[2])))
78 attrs
.extend(("48", "2", str(self
.bg
[0]), str(self
.bg
[1]), str(self
.bg
[2])))
85 return self
.escape(attrs
)
87 def reset_string(self
):
89 if self
.fg
is not None:
91 if self
.bg
is not None:
93 if self
.bold
or self
.underline
or self
.italic
:
95 return self
.escape(attrs
)
98 class Terminal256Formatter(Formatter
):
100 Format tokens with ANSI color sequences, for output in a 256-color
101 terminal or console. Like in `TerminalFormatter` color sequences
102 are terminated at newlines, so that paging the output works correctly.
104 The formatter takes colors from a style defined by the `style` option
105 and converts them to nearest ANSI 256-color escape sequences. Bold and
106 underline attributes from the style are preserved (and displayed).
108 .. versionadded:: 0.9
110 .. versionchanged:: 2.2
111 If the used style defines foreground colors in the form ``#ansi*``, then
112 `Terminal256Formatter` will map these to non extended foreground color.
113 See :ref:`AnsiTerminalStyle` for more information.
115 .. versionchanged:: 2.4
116 The ANSI color names have been updated with names that are easier to
117 understand and align with colornames of other projects and terminals.
118 See :ref:`this table <new-ansi-color-names>` for more information.
124 The style to use, can be a string or a Style subclass (default:
128 Set to ``True`` to have line numbers on the terminal output as well
129 (default: ``False`` = no line numbers).
132 aliases
= ['terminal256', 'console256', '256']
135 def __init__(self
, **options
):
136 Formatter
.__init
__(self
, **options
)
138 self
.xterm_colors
= []
140 self
.style_string
= {}
142 self
.usebold
= 'nobold' not in options
143 self
.useunderline
= 'nounderline' not in options
144 self
.useitalic
= 'noitalic' not in options
146 self
._build
_color
_table
() # build an RGB-to-256 color conversion table
147 self
._setup
_styles
() # convert selected style's colors to term. colors
149 self
.linenos
= options
.get('linenos', False)
152 def _build_color_table(self
):
153 # colors 0..15: 16 basic colors
155 self
.xterm_colors
.append((0x00, 0x00, 0x00)) # 0
156 self
.xterm_colors
.append((0xcd, 0x00, 0x00)) # 1
157 self
.xterm_colors
.append((0x00, 0xcd, 0x00)) # 2
158 self
.xterm_colors
.append((0xcd, 0xcd, 0x00)) # 3
159 self
.xterm_colors
.append((0x00, 0x00, 0xee)) # 4
160 self
.xterm_colors
.append((0xcd, 0x00, 0xcd)) # 5
161 self
.xterm_colors
.append((0x00, 0xcd, 0xcd)) # 6
162 self
.xterm_colors
.append((0xe5, 0xe5, 0xe5)) # 7
163 self
.xterm_colors
.append((0x7f, 0x7f, 0x7f)) # 8
164 self
.xterm_colors
.append((0xff, 0x00, 0x00)) # 9
165 self
.xterm_colors
.append((0x00, 0xff, 0x00)) # 10
166 self
.xterm_colors
.append((0xff, 0xff, 0x00)) # 11
167 self
.xterm_colors
.append((0x5c, 0x5c, 0xff)) # 12
168 self
.xterm_colors
.append((0xff, 0x00, 0xff)) # 13
169 self
.xterm_colors
.append((0x00, 0xff, 0xff)) # 14
170 self
.xterm_colors
.append((0xff, 0xff, 0xff)) # 15
172 # colors 16..232: the 6x6x6 color cube
174 valuerange
= (0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff)
177 r
= valuerange
[(i
// 36) % 6]
178 g
= valuerange
[(i
// 6) % 6]
179 b
= valuerange
[i
% 6]
180 self
.xterm_colors
.append((r
, g
, b
))
182 # colors 233..253: grayscale
184 for i
in range(1, 22):
186 self
.xterm_colors
.append((v
, v
, v
))
188 def _closest_color(self
, r
, g
, b
):
189 distance
= 257*257*3 # "infinity" (>distance from #000000 to #ffffff)
192 for i
in range(0, 254):
193 values
= self
.xterm_colors
[i
]
198 d
= rd
*rd
+ gd
*gd
+ bd
*bd
205 def _color_index(self
, color
):
206 index
= self
.best_match
.get(color
, None)
207 if color
in ansicolors
:
208 # strip the `ansi/#ansi` part and look up code
210 self
.best_match
[color
] = index
213 rgb
= int(str(color
), 16)
217 r
= (rgb
>> 16) & 0xff
218 g
= (rgb
>> 8) & 0xff
220 index
= self
._closest
_color
(r
, g
, b
)
221 self
.best_match
[color
] = index
224 def _setup_styles(self
):
225 for ttype
, ndef
in self
.style
:
226 escape
= EscapeSequence()
227 # get foreground from ansicolor if set
228 if ndef
['ansicolor']:
229 escape
.fg
= self
._color
_index
(ndef
['ansicolor'])
231 escape
.fg
= self
._color
_index
(ndef
['color'])
232 if ndef
['bgansicolor']:
233 escape
.bg
= self
._color
_index
(ndef
['bgansicolor'])
234 elif ndef
['bgcolor']:
235 escape
.bg
= self
._color
_index
(ndef
['bgcolor'])
236 if self
.usebold
and ndef
['bold']:
238 if self
.useunderline
and ndef
['underline']:
239 escape
.underline
= True
240 if self
.useitalic
and ndef
['italic']:
242 self
.style_string
[str(ttype
)] = (escape
.color_string(),
243 escape
.reset_string())
245 def _write_lineno(self
, outfile
):
247 outfile
.write("%s%04d: " % (self
._lineno
!= 1 and '\n' or '', self
._lineno
))
249 def format(self
, tokensource
, outfile
):
250 return Formatter
.format(self
, tokensource
, outfile
)
252 def format_unencoded(self
, tokensource
, outfile
):
254 self
._write
_lineno
(outfile
)
256 for ttype
, value
in tokensource
:
258 while ttype
and not_found
:
260 # outfile.write( "<" + str(ttype) + ">" )
261 on
, off
= self
.style_string
[str(ttype
)]
263 # Like TerminalFormatter, add "reset colors" escape sequence
265 spl
= value
.split('\n')
266 for line
in spl
[:-1]:
268 outfile
.write(on
+ line
+ off
)
270 self
._write
_lineno
(outfile
)
275 outfile
.write(on
+ spl
[-1] + off
)
278 # outfile.write( '#' + str(ttype) + '#' )
283 # outfile.write( '!' + str(ottype) + '->' + str(ttype) + '!' )
293 class TerminalTrueColorFormatter(Terminal256Formatter
):
295 Format tokens with ANSI color sequences, for output in a true-color
296 terminal or console. Like in `TerminalFormatter` color sequences
297 are terminated at newlines, so that paging the output works correctly.
299 .. versionadded:: 2.1
304 The style to use, can be a string or a Style subclass (default:
307 name
= 'TerminalTrueColor'
308 aliases
= ['terminal16m', 'console16m', '16m']
311 def _build_color_table(self
):
314 def _color_tuple(self
, color
):
316 rgb
= int(str(color
), 16)
319 r
= (rgb
>> 16) & 0xff
320 g
= (rgb
>> 8) & 0xff
324 def _setup_styles(self
):
325 for ttype
, ndef
in self
.style
:
326 escape
= EscapeSequence()
328 escape
.fg
= self
._color
_tuple
(ndef
['color'])
330 escape
.bg
= self
._color
_tuple
(ndef
['bgcolor'])
331 if self
.usebold
and ndef
['bold']:
333 if self
.useunderline
and ndef
['underline']:
334 escape
.underline
= True
335 if self
.useitalic
and ndef
['italic']:
337 self
.style_string
[str(ttype
)] = (escape
.true_color_string(),
338 escape
.reset_string())