[Python-checkins] r66933 - in doctools/trunk: CHANGES sphinx/__init__.py sphinx/util/console.py

georg.brandl python-checkins at python.org
Thu Oct 16 23:16:29 CEST 2008


Author: georg.brandl
Date: Thu Oct 16 23:16:28 2008
New Revision: 66933

Log:
Only do colors on terminals that support them. #4102.


Modified:
   doctools/trunk/CHANGES
   doctools/trunk/sphinx/__init__.py
   doctools/trunk/sphinx/util/console.py

Modified: doctools/trunk/CHANGES
==============================================================================
--- doctools/trunk/CHANGES	(original)
+++ doctools/trunk/CHANGES	Thu Oct 16 23:16:28 2008
@@ -120,6 +120,8 @@
 
 * Don't crash on weird casing of role names (like ``:Class:``).
 
+* Only output ANSI colors on color terminals.
+
 
 Release 0.4.3 (Oct 8, 2008)
 ===========================

Modified: doctools/trunk/sphinx/__init__.py
==============================================================================
--- doctools/trunk/sphinx/__init__.py	(original)
+++ doctools/trunk/sphinx/__init__.py	Thu Oct 16 23:16:28 2008
@@ -17,7 +17,7 @@
 from cStringIO import StringIO
 
 from sphinx.util import format_exception_cut_frames, save_traceback
-from sphinx.util.console import darkred, nocolor
+from sphinx.util.console import darkred, nocolor, color_terminal
 
 __revision__ = '$Revision$'
 __version__ = '0.5'
@@ -54,7 +54,7 @@
     from sphinx.application import Sphinx, SphinxError
     from docutils.utils import SystemMessage
 
-    if not sys.stdout.isatty() or sys.platform == 'win32':
+    if not sys.stdout.isatty() or sys.platform == 'win32' or not color_terminal():
         # Windows' poor cmd box doesn't understand ANSI sequences
         nocolor()
 

Modified: doctools/trunk/sphinx/util/console.py
==============================================================================
--- doctools/trunk/sphinx/util/console.py	(original)
+++ doctools/trunk/sphinx/util/console.py	Thu Oct 16 23:16:28 2008
@@ -9,6 +9,8 @@
     :license: BSD.
 """
 
+import os
+
 codes = {}
 
 def get_terminal_width():
@@ -34,6 +36,14 @@
     else:
         func(text.ljust(_tw) + _tw * "\b")
 
+def color_terminal():
+    if 'COLORTERM' in os.environ:
+        return True
+    term = os.environ.get('TERM', 'dumb').lower()
+    if 'xterm' in term or 'color' in term:
+        return True
+    return False
+
 
 def nocolor():
     codes.clear()


More information about the Python-checkins mailing list