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-2010 by the Pygments team, see AUTHORS. |
8 :copyright: Copyright 2006-2012 by the Pygments team, see AUTHORS. |
9 :license: BSD, see LICENSE for details. |
9 :license: BSD, see LICENSE for details. |
10 """ |
10 """ |
|
11 |
|
12 import sys |
11 |
13 |
12 from pygments.formatter import Formatter |
14 from pygments.formatter import Formatter |
13 from pygments.token import Keyword, Name, Comment, String, Error, \ |
15 from pygments.token import Keyword, Name, Comment, String, Error, \ |
14 Number, Operator, Generic, Token, Whitespace |
16 Number, Operator, Generic, Token, Whitespace |
15 from pygments.console import ansiformat |
17 from pygments.console import ansiformat |
84 |
86 |
85 def format(self, tokensource, outfile): |
87 def format(self, tokensource, outfile): |
86 # hack: if the output is a terminal and has an encoding set, |
88 # hack: if the output is a terminal and has an encoding set, |
87 # use that to avoid unicode encode problems |
89 # use that to avoid unicode encode problems |
88 if not self.encoding and hasattr(outfile, "encoding") and \ |
90 if not self.encoding and hasattr(outfile, "encoding") and \ |
89 hasattr(outfile, "isatty") and outfile.isatty(): |
91 hasattr(outfile, "isatty") and outfile.isatty() and \ |
|
92 sys.version_info < (3,): |
90 self.encoding = outfile.encoding |
93 self.encoding = outfile.encoding |
91 return Formatter.format(self, tokensource, outfile) |
94 return Formatter.format(self, tokensource, outfile) |
92 |
95 |
93 def format_unencoded(self, tokensource, outfile): |
96 def format_unencoded(self, tokensource, outfile): |
94 for ttype, value in tokensource: |
97 for ttype, value in tokensource: |