diff --git a/CHANGES b/CHANGES index c53144d91..4d9a68c03 100644 --- a/CHANGES +++ b/CHANGES @@ -39,6 +39,7 @@ New features submodule documentation. Thanks to Wes Turner and Luc Saffre. * #1434: Provide non-minified JS files for jquery.js and underscore.js to clarify the source of the minified files. +* PR#252: Windows color console support. Thanks to meu31. Bugs fixed ---------- diff --git a/setup.py b/setup.py index a0004197e..4ebdc81fc 100644 --- a/setup.py +++ b/setup.py @@ -49,6 +49,9 @@ requires = [ 'six', 'Jinja2>=2.3', 'Pygments>=1.2', 'docutils>=0.10', 'snowballstemmer>=1.1' ] +if sys.platform == 'win32': + requires.append('colorama') + # Provide a "compile_catalog" command that also creates the translated # JavaScript files if Babel is available. diff --git a/sphinx/util/console.py b/sphinx/util/console.py index 508d895fc..64eb4e478 100644 --- a/sphinx/util/console.py +++ b/sphinx/util/console.py @@ -13,6 +13,12 @@ import os import sys import re +try: + # check if colorama is installed to support color on Windows + import colorama +except ImportError: + colorama = None + _ansi_re = re.compile('\x1b\\[(\\d\\d;){0,2}\\d\\dm') codes = {} @@ -42,6 +48,9 @@ def term_width_line(text): return text.ljust(_tw + len(text) - len(_ansi_re.sub('', text))) + '\r' def color_terminal(): + if sys.platform == 'win32' and colorama is not None: + colorama.init() + return True if not hasattr(sys.stdout, 'isatty'): return False if not sys.stdout.isatty(): @@ -55,12 +64,7 @@ def color_terminal(): def nocolor(): - # check if colorama is installed to support color on Windows - try: - import colorama - colorama.init() - except ImportError: - codes.clear() + codes.clear() def coloron(): codes.update(_orig_codes)