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

View File

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