]>
jfr.im git - dlqueue.git/blob - venv/lib/python3.11/site-packages/pip/_vendor/pygments/formatters/terminal.py
2 pygments.formatters.terminal
3 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 Formatter for terminal output with ANSI sequences.
7 :copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS.
8 :license: BSD, see LICENSE for details.
11 from pip
._vendor
.pygments
.formatter
import Formatter
12 from pip
._vendor
.pygments
.token
import Keyword
, Name
, Comment
, String
, Error
, \
13 Number
, Operator
, Generic
, Token
, Whitespace
14 from pip
._vendor
.pygments
.console
import ansiformat
15 from pip
._vendor
.pygments
.util
import get_choice_opt
18 __all__
= ['TerminalFormatter']
21 #: Map token types to a tuple of color values for light and dark
26 Whitespace
: ('gray', 'brightblack'),
27 Comment
: ('gray', 'brightblack'),
28 Comment
.Preproc
: ('cyan', 'brightcyan'),
29 Keyword
: ('blue', 'brightblue'),
30 Keyword
.Type
: ('cyan', 'brightcyan'),
31 Operator
.Word
: ('magenta', 'brightmagenta'),
32 Name
.Builtin
: ('cyan', 'brightcyan'),
33 Name
.Function
: ('green', 'brightgreen'),
34 Name
.Namespace
: ('_cyan_', '_brightcyan_'),
35 Name
.Class
: ('_green_', '_brightgreen_'),
36 Name
.Exception: ('cyan', 'brightcyan'),
37 Name
.Decorator
: ('brightblack', 'gray'),
38 Name
.Variable
: ('red', 'brightred'),
39 Name
.Constant
: ('red', 'brightred'),
40 Name
.Attribute
: ('cyan', 'brightcyan'),
41 Name
.Tag
: ('brightblue', 'brightblue'),
42 String
: ('yellow', 'yellow'),
43 Number
: ('blue', 'brightblue'),
45 Generic
.Deleted
: ('brightred', 'brightred'),
46 Generic
.Inserted
: ('green', 'brightgreen'),
47 Generic
.Heading
: ('**', '**'),
48 Generic
.Subheading
: ('*magenta*', '*brightmagenta*'),
49 Generic
.Prompt
: ('**', '**'),
50 Generic
.Error
: ('brightred', 'brightred'),
52 Error
: ('_brightred_', '_brightred_'),
56 class TerminalFormatter(Formatter
):
58 Format tokens with ANSI color sequences, for output in a text console.
59 Color sequences are terminated at newlines, so that paging the output
62 The `get_style_defs()` method doesn't do anything special since there is
63 no support for common styles.
68 Set to ``"light"`` or ``"dark"`` depending on the terminal's background
69 (default: ``"light"``).
72 A dictionary mapping token types to (lightbg, darkbg) color names or
73 ``None`` (default: ``None`` = use builtin colorscheme).
76 Set to ``True`` to have line numbers on the terminal output as well
77 (default: ``False`` = no line numbers).
80 aliases
= ['terminal', 'console']
83 def __init__(self
, **options
):
84 Formatter
.__init
__(self
, **options
)
85 self
.darkbg
= get_choice_opt(options
, 'bg',
86 ['light', 'dark'], 'light') == 'dark'
87 self
.colorscheme
= options
.get('colorscheme', None) or TERMINAL_COLORS
88 self
.linenos
= options
.get('linenos', False)
91 def format(self
, tokensource
, outfile
):
92 return Formatter
.format(self
, tokensource
, outfile
)
94 def _write_lineno(self
, outfile
):
96 outfile
.write("%s%04d: " % (self
._lineno
!= 1 and '\n' or '', self
._lineno
))
98 def _get_color(self
, ttype
):
99 # self.colorscheme is a dict containing usually generic types, so we
100 # have to walk the tree of dots. The base Token type must be a key,
101 # even if it's empty string, as in the default above.
102 colors
= self
.colorscheme
.get(ttype
)
103 while colors
is None:
105 colors
= self
.colorscheme
.get(ttype
)
106 return colors
[self
.darkbg
]
108 def format_unencoded(self
, tokensource
, outfile
):
110 self
._write
_lineno
(outfile
)
112 for ttype
, value
in tokensource
:
113 color
= self
._get
_color
(ttype
)
115 for line
in value
.splitlines(True):
117 outfile
.write(ansiformat(color
, line
.rstrip('\n')))
119 outfile
.write(line
.rstrip('\n'))
120 if line
.endswith('\n'):
122 self
._write
_lineno
(outfile
)