3 pygments.formatters.terminal |
3 pygments.formatters.terminal |
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
5 |
5 |
6 Formatter for terminal output with ANSI sequences. |
6 Formatter for terminal output with ANSI sequences. |
7 |
7 |
8 :copyright: Copyright 2006-2013 by the Pygments team, see AUTHORS. |
8 :copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS. |
9 :license: BSD, see LICENSE for details. |
9 :license: BSD, see LICENSE for details. |
10 """ |
10 """ |
11 |
|
12 from __future__ import unicode_literals |
|
13 |
11 |
14 import sys |
12 import sys |
15 |
13 |
16 from pygments.formatter import Formatter |
14 from pygments.formatter import Formatter |
17 from pygments.token import Keyword, Name, Comment, String, Error, \ |
15 from pygments.token import Keyword, Name, Comment, String, Error, \ |
18 Number, Operator, Generic, Token, Whitespace |
16 Number, Operator, Generic, Token, Whitespace |
19 from pygments.console import ansiformat |
17 from pygments.console import ansiformat |
20 from pygments.util import get_choice_opt |
18 from pygments.util import get_choice_opt |
21 |
19 |
22 |
20 |
23 __all__ = ['TerminalFormatter'] |
21 __all__ = ['TerminalFormatter'] |
73 (default: ``"light"``). |
71 (default: ``"light"``). |
74 |
72 |
75 `colorscheme` |
73 `colorscheme` |
76 A dictionary mapping token types to (lightbg, darkbg) color names or |
74 A dictionary mapping token types to (lightbg, darkbg) color names or |
77 ``None`` (default: ``None`` = use builtin colorscheme). |
75 ``None`` (default: ``None`` = use builtin colorscheme). |
|
76 |
|
77 `linenos` |
|
78 Set to ``True`` to have line numbers on the terminal output as well |
|
79 (default: ``False`` = no line numbers). |
78 """ |
80 """ |
79 name = 'Terminal' |
81 name = 'Terminal' |
80 aliases = ['terminal', 'console'] |
82 aliases = ['terminal', 'console'] |
81 filenames = [] |
83 filenames = [] |
82 |
84 |
83 def __init__(self, **options): |
85 def __init__(self, **options): |
84 Formatter.__init__(self, **options) |
86 Formatter.__init__(self, **options) |
85 self.darkbg = get_choice_opt(options, 'bg', |
87 self.darkbg = get_choice_opt(options, 'bg', |
86 ['light', 'dark'], 'light') == 'dark' |
88 ['light', 'dark'], 'light') == 'dark' |
87 self.colorscheme = options.get('colorscheme', None) or TERMINAL_COLORS |
89 self.colorscheme = options.get('colorscheme', None) or TERMINAL_COLORS |
|
90 self.linenos = options.get('linenos', False) |
|
91 self._lineno = 0 |
88 |
92 |
89 def format(self, tokensource, outfile): |
93 def format(self, tokensource, outfile): |
90 # hack: if the output is a terminal and has an encoding set, |
94 # hack: if the output is a terminal and has an encoding set, |
91 # use that to avoid unicode encode problems |
95 # use that to avoid unicode encode problems |
92 if not self.encoding and hasattr(outfile, "encoding") and \ |
96 if not self.encoding and hasattr(outfile, "encoding") and \ |
93 hasattr(outfile, "isatty") and outfile.isatty() and \ |
97 hasattr(outfile, "isatty") and outfile.isatty() and \ |
94 sys.version_info < (3,): |
98 sys.version_info < (3,): |
95 self.encoding = outfile.encoding |
99 self.encoding = outfile.encoding |
96 return Formatter.format(self, tokensource, outfile) |
100 return Formatter.format(self, tokensource, outfile) |
97 |
101 |
|
102 def _write_lineno(self, outfile): |
|
103 self._lineno += 1 |
|
104 outfile.write("\n%04d: " % self._lineno) |
|
105 |
|
106 def _format_unencoded_with_lineno(self, tokensource, outfile): |
|
107 self._write_lineno(outfile) |
|
108 |
|
109 for ttype, value in tokensource: |
|
110 if value.endswith("\n"): |
|
111 self._write_lineno(outfile) |
|
112 value = value[:-1] |
|
113 color = self.colorscheme.get(ttype) |
|
114 while color is None: |
|
115 ttype = ttype[:-1] |
|
116 color = self.colorscheme.get(ttype) |
|
117 if color: |
|
118 color = color[self.darkbg] |
|
119 spl = value.split('\n') |
|
120 for line in spl[:-1]: |
|
121 self._write_lineno(outfile) |
|
122 if line: |
|
123 outfile.write(ansiformat(color, line[:-1])) |
|
124 if spl[-1]: |
|
125 outfile.write(ansiformat(color, spl[-1])) |
|
126 else: |
|
127 outfile.write(value) |
|
128 |
|
129 outfile.write("\n") |
|
130 |
98 def format_unencoded(self, tokensource, outfile): |
131 def format_unencoded(self, tokensource, outfile): |
|
132 if self.linenos: |
|
133 self._format_unencoded_with_lineno(tokensource, outfile) |
|
134 return |
|
135 |
99 for ttype, value in tokensource: |
136 for ttype, value in tokensource: |
100 color = self.colorscheme.get(ttype) |
137 color = self.colorscheme.get(ttype) |
101 while color is None: |
138 while color is None: |
102 ttype = ttype[:-1] |
139 ttype = ttype[:-1] |
103 color = self.colorscheme.get(ttype) |
140 color = self.colorscheme.get(ttype) |