Improved terminal color depth detection via curses

Closes #244
This commit is contained in:
Jakub Roztocil 2014-09-08 07:46:53 +02:00
parent 6aa711c69f
commit 58b51a8277
2 changed files with 19 additions and 9 deletions

View File

@ -1,4 +1,3 @@
import os
import sys
from requests.compat import is_windows
@ -18,7 +17,6 @@ class Environment(object):
"""
is_windows = is_windows
config_dir = DEFAULT_CONFIG_DIR
colors = 256 if '256color' in os.environ.get('TERM', '') else 88
stdin = sys.stdin
stdin_isatty = stdin.isatty()
stdin_encoding = None
@ -27,13 +25,24 @@ class Environment(object):
stdout_encoding = None
stderr = sys.stderr
stderr_isatty = stderr.isatty()
if is_windows:
if not is_windows:
import curses
curses.setupterm()
colors = curses.tigetnum('colors')
del curses
else:
colors = 256
# noinspection PyUnresolvedReferences
from colorama.initialise import wrap_stream
stdout = wrap_stream(stdout, convert=None, strip=None,
autoreset=True, wrap=True)
stderr = wrap_stream(stderr, convert=None, strip=None,
autoreset=True, wrap=True)
import colorama.initialise
stdout = colorama.initialise.wrap_stream(
stdout, convert=None, strip=None,
autoreset=True, wrap=True
)
stderr = colorama.initialise.wrap_stream(
stderr, convert=None, strip=None,
autoreset=True, wrap=True
)
del colorama
def __init__(self, **kwargs):
"""
@ -51,6 +60,7 @@ class Environment(object):
if self.stdout_encoding is None:
actual_stdout = self.stdout
if is_windows:
# noinspection PyUnresolvedReferences
from colorama import AnsiToWin32
if isinstance(self.stdout, AnsiToWin32):
actual_stdout = self.stdout.wrapped

View File

@ -42,7 +42,7 @@ class ColorFormatter(FormatterPlugin):
except ClassNotFound:
style_class = Solarized256Style
if env.is_windows or env.colors == 256:
if env.colors == 256:
fmt_class = Terminal256Formatter
else:
fmt_class = TerminalFormatter