summaryrefslogtreecommitdiff
path: root/pygments/formatters/terminal.py
diff options
context:
space:
mode:
Diffstat (limited to 'pygments/formatters/terminal.py')
-rw-r--r--pygments/formatters/terminal.py7
1 files changed, 7 insertions, 0 deletions
diff --git a/pygments/formatters/terminal.py b/pygments/formatters/terminal.py
index 661031e7..6ac6cd1b 100644
--- a/pygments/formatters/terminal.py
+++ b/pygments/formatters/terminal.py
@@ -9,6 +9,8 @@
:license: BSD, see LICENSE for more details.
"""
+import os
+
from pygments.formatter import Formatter
from pygments.token import Keyword, Name, Comment, String, Error, \
Number, Operator, Generic, Token
@@ -77,6 +79,11 @@ class TerminalFormatter(Formatter):
def format(self, tokensource, outfile):
enc = self.encoding
+ # hack: if the output is a terminal and has an encoding set,
+ # use that to avoid unicode encode problems
+ if not enc and hasattr(outfile, "encoding") and \
+ hasattr(outfile, "isatty") and outfile.isatty():
+ enc = outfile.encoding
for ttype, value in tokensource:
if enc:
value = value.encode(enc)