eric6/ThirdParty/Pygments/pygments/formatters/terminal.py

changeset 8258
82b608e352ec
parent 8257
28146736bbfc
child 8259
2bbec88047dd
equal deleted inserted replaced
8257:28146736bbfc 8258:82b608e352ec
1 # -*- coding: utf-8 -*-
2 """
3 pygments.formatters.terminal
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5
6 Formatter for terminal output with ANSI sequences.
7
8 :copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
9 :license: BSD, see LICENSE for details.
10 """
11
12 import sys
13
14 from pygments.formatter import Formatter
15 from pygments.token import Keyword, Name, Comment, String, Error, \
16 Number, Operator, Generic, Token, Whitespace
17 from pygments.console import ansiformat
18 from pygments.util import get_choice_opt
19
20
21 __all__ = ['TerminalFormatter']
22
23
24 #: Map token types to a tuple of color values for light and dark
25 #: backgrounds.
26 TERMINAL_COLORS = {
27 Token: ('', ''),
28
29 Whitespace: ('gray', 'brightblack'),
30 Comment: ('gray', 'brightblack'),
31 Comment.Preproc: ('cyan', 'brightcyan'),
32 Keyword: ('blue', 'brightblue'),
33 Keyword.Type: ('cyan', 'brightcyan'),
34 Operator.Word: ('magenta', 'brightmagenta'),
35 Name.Builtin: ('cyan', 'brightcyan'),
36 Name.Function: ('green', 'brightgreen'),
37 Name.Namespace: ('_cyan_', '_brightcyan_'),
38 Name.Class: ('_green_', '_brightgreen_'),
39 Name.Exception: ('cyan', 'brightcyan'),
40 Name.Decorator: ('brightblack', 'gray'),
41 Name.Variable: ('red', 'brightred'),
42 Name.Constant: ('red', 'brightred'),
43 Name.Attribute: ('cyan', 'brightcyan'),
44 Name.Tag: ('brightblue', 'brightblue'),
45 String: ('yellow', 'yellow'),
46 Number: ('blue', 'brightblue'),
47
48 Generic.Deleted: ('brightred', 'brightred'),
49 Generic.Inserted: ('green', 'brightgreen'),
50 Generic.Heading: ('**', '**'),
51 Generic.Subheading: ('*magenta*', '*brightmagenta*'),
52 Generic.Prompt: ('**', '**'),
53 Generic.Error: ('brightred', 'brightred'),
54
55 Error: ('_brightred_', '_brightred_'),
56 }
57
58
59 class TerminalFormatter(Formatter):
60 r"""
61 Format tokens with ANSI color sequences, for output in a text console.
62 Color sequences are terminated at newlines, so that paging the output
63 works correctly.
64
65 The `get_style_defs()` method doesn't do anything special since there is
66 no support for common styles.
67
68 Options accepted:
69
70 `bg`
71 Set to ``"light"`` or ``"dark"`` depending on the terminal's background
72 (default: ``"light"``).
73
74 `colorscheme`
75 A dictionary mapping token types to (lightbg, darkbg) color names or
76 ``None`` (default: ``None`` = use builtin colorscheme).
77
78 `linenos`
79 Set to ``True`` to have line numbers on the terminal output as well
80 (default: ``False`` = no line numbers).
81 """
82 name = 'Terminal'
83 aliases = ['terminal', 'console']
84 filenames = []
85
86 def __init__(self, **options):
87 Formatter.__init__(self, **options)
88 self.darkbg = get_choice_opt(options, 'bg',
89 ['light', 'dark'], 'light') == 'dark'
90 self.colorscheme = options.get('colorscheme', None) or TERMINAL_COLORS
91 self.linenos = options.get('linenos', False)
92 self._lineno = 0
93
94 def format(self, tokensource, outfile):
95 return Formatter.format(self, tokensource, outfile)
96
97 def _write_lineno(self, outfile):
98 self._lineno += 1
99 outfile.write("%s%04d: " % (self._lineno != 1 and '\n' or '', self._lineno))
100
101 def _get_color(self, ttype):
102 # self.colorscheme is a dict containing usually generic types, so we
103 # have to walk the tree of dots. The base Token type must be a key,
104 # even if it's empty string, as in the default above.
105 colors = self.colorscheme.get(ttype)
106 while colors is None:
107 ttype = ttype.parent
108 colors = self.colorscheme.get(ttype)
109 return colors[self.darkbg]
110
111 def format_unencoded(self, tokensource, outfile):
112 if self.linenos:
113 self._write_lineno(outfile)
114
115 for ttype, value in tokensource:
116 color = self._get_color(ttype)
117
118 for line in value.splitlines(True):
119 if color:
120 outfile.write(ansiformat(color, line.rstrip('\n')))
121 else:
122 outfile.write(line.rstrip('\n'))
123 if line.endswith('\n'):
124 if self.linenos:
125 self._write_lineno(outfile)
126 else:
127 outfile.write('\n')
128
129 if self.linenos:
130 outfile.write("\n")

eric ide

mercurial