Skip to content

Commit

Permalink
maybe fix win32 colors
Browse files Browse the repository at this point in the history
  • Loading branch information
Erotemic committed Jan 27, 2021
1 parent 63a7743 commit 9862e9c
Showing 1 changed file with 29 additions and 30 deletions.
59 changes: 29 additions & 30 deletions xdoctest/utils/util_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import six
import math
import textwrap
# import warnings
import warnings
import re
import os
import sys
Expand Down Expand Up @@ -82,36 +82,34 @@ def color_text(text, color):
if NO_COLOR or color is None:
return text
try:
import pygments
import pygments.console

if sys.platform.startswith('win32'): # nocover
# Hack on win32 to support colored output
try:
import colorama
if colorama.initialise.atexit_done is None:
if not colorama.initialise.atexit_done:
# Only init if it hasn't been done
colorama.init()
except ImportError as ex:
import warnings
warnings.warn('os is win32 and colorma is not installed {!r}'.format(ex))
import os
if os.environ.get('XDOC_WIN32_COLORS', 'False') == 'False':
# hack: dont color on windows by default, but do init colorama
return text
except ImportError:
warnings.warn(
'colorama is not installed, ansi colors may not work')
# import os
# if os.environ.get('XDOC_WIN32_COLORS', 'False') == 'False':
# # hack: dont color on windows by default, but do init colorama
# return text

import pygments
import pygments.console
try:
ansi_text = pygments.console.colorize(color, text)
except KeyError:
import warnings
warnings.warn('unable to find color: {!r}'.format(color))
return text
except Exception as ex:
import warnings
except Exception as ex: # nocover
warnings.warn('some other issue with text color: {!r}'.format(ex))
return text
return ansi_text
except ImportError: # nocover
import warnings
warnings.warn('pygments is not installed, text will not be colored')
return text

Expand Down Expand Up @@ -196,27 +194,28 @@ def highlight_code(text, lexer_name='python', **kwargs):
'cxx': 'cpp',
'c': 'cpp',
}.get(lexer_name.replace('.', ''), lexer_name)
try:

# if sys.platform.startswith('win32'): # nocover
# # Hack on win32 to support colored output
# import os
# if os.environ.get('XDOC_WIN32_COLORS', 'False') == 'False':
# # hack: dont color on windows by default, but do init colorama
# return text
if sys.platform.startswith('win32'): # nocover
# Hack on win32 to support colored output
try:
import colorama
if not colorama.initialise.atexit_done:
# Only init if it hasn't been done
colorama.init()
except ImportError:
warnings.warn(
'colorama is not installed, ansi colors may not work')
# import os
# if os.environ.get('XDOC_WIN32_COLORS', 'False') == 'False':
# # hack: dont color on windows by default, but do init colorama
# return text

try:
import pygments
import pygments.lexers
import pygments.formatters
import pygments.formatters.terminal

if sys.platform.startswith('win32'): # nocover
# Hack on win32 to support colored output
import colorama
if colorama.initialise.atexit_done is None:
# Only init if it hasn't been done
colorama.init()

# formater = pygments.formatters.terminal.TerminalFormatter(bg='dark')
# lexer = pygments.lexers.get_lexer_by_name(lexer_name, ensurenl=False, **kwargs)
# new_text = pygments.highlight(text, lexer, formater)
Expand All @@ -225,7 +224,7 @@ def highlight_code(text, lexer_name='python', **kwargs):
new_text = pygments.highlight(text, lexer, formater)

except ImportError: # nocover
# warnings.warn('pygments is not installed')
warnings.warn('pygments is not installed, code will not be highlighted')
new_text = text
return new_text

Expand Down

0 comments on commit 9862e9c

Please sign in to comment.