diff --git a/dosagelib/output.py b/dosagelib/output.py index 3c1f303cf..c40626deb 100644 --- a/dosagelib/output.py +++ b/dosagelib/output.py @@ -17,12 +17,12 @@ import io import six try: - import curses + from shutil import get_terminal_size except ImportError: - curses = None + from backports.shutil_get_terminal_size import get_terminal_size import colorama -from colorama import Fore, Style, win32 +from colorama import Fore, Style lock = threading.Lock() @@ -109,7 +109,7 @@ class Output(object): self.stream.write(u'%s%s> ' % (timestamp, self.context)) elif self.context is None: self.stream.write(u'%s%s> ' % (timestamp, get_threadname())) - if color and self.has_color: + if color and self.is_tty: s = u'%s%s%s' % (color, s, Style.RESET_ALL) self.stream.write(six.text_type(s)) self.stream.write(six.text_type(os.linesep)) @@ -121,44 +121,20 @@ class Output(object): for line in line.rstrip(u'\n').split(u'\n'): self.write(line.rstrip(u'\n'), level=level) - @property - def has_color(self): - if not self.is_tty: - return False - elif os.name == 'nt': - return True - elif curses: - try: - curses.setupterm(os.environ.get("TERM"), - self._base_stream.fileno()) - # More than 8 colors are good enough. - return curses.tigetnum("colors") >= 8 - except curses.error: - return False - return False - @property def width(self): """Get width of this output.""" if not self.is_tty: return self.DEFAULT_WIDTH - elif os.name == 'nt': - csbi = win32.GetConsoleScreenBufferInfo(win32.STDOUT) - return csbi.dwSize.X - elif curses: - try: - curses.setupterm(os.environ.get("TERM"), - self._base_stream.fileno()) - return curses.tigetnum("cols") - except curses.error: - pass - return self.DEFAULT_WIDTH + try: + return get_terminal_size().columns + except ValueError: + return self.DEFAULT_WIDTH @property def is_tty(self): """Is this output stream a terminal?""" - return (hasattr(self._base_stream, "isatty") and - self._base_stream.isatty()) + return getattr(self._base_stream, "isatty", lambda: False)() @contextlib.contextmanager def temporary_context(self, context): diff --git a/requirements.txt b/requirements.txt index b45e21713..91b62e435 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,4 @@ lxml pbr requests>=2.0 six +backports.shutil_get_terminal_size; python_version<'3.3'