Use terminal size calculation from standard library.
This commit is contained in:
parent
06be2a026b
commit
f94caa8a16
2 changed files with 10 additions and 33 deletions
|
@ -17,12 +17,12 @@ import io
|
||||||
import six
|
import six
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import curses
|
from shutil import get_terminal_size
|
||||||
except ImportError:
|
except ImportError:
|
||||||
curses = None
|
from backports.shutil_get_terminal_size import get_terminal_size
|
||||||
|
|
||||||
import colorama
|
import colorama
|
||||||
from colorama import Fore, Style, win32
|
from colorama import Fore, Style
|
||||||
|
|
||||||
|
|
||||||
lock = threading.Lock()
|
lock = threading.Lock()
|
||||||
|
@ -109,7 +109,7 @@ class Output(object):
|
||||||
self.stream.write(u'%s%s> ' % (timestamp, self.context))
|
self.stream.write(u'%s%s> ' % (timestamp, self.context))
|
||||||
elif self.context is None:
|
elif self.context is None:
|
||||||
self.stream.write(u'%s%s> ' % (timestamp, get_threadname()))
|
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)
|
s = u'%s%s%s' % (color, s, Style.RESET_ALL)
|
||||||
self.stream.write(six.text_type(s))
|
self.stream.write(six.text_type(s))
|
||||||
self.stream.write(six.text_type(os.linesep))
|
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'):
|
for line in line.rstrip(u'\n').split(u'\n'):
|
||||||
self.write(line.rstrip(u'\n'), level=level)
|
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
|
@property
|
||||||
def width(self):
|
def width(self):
|
||||||
"""Get width of this output."""
|
"""Get width of this output."""
|
||||||
if not self.is_tty:
|
if not self.is_tty:
|
||||||
return self.DEFAULT_WIDTH
|
return self.DEFAULT_WIDTH
|
||||||
elif os.name == 'nt':
|
try:
|
||||||
csbi = win32.GetConsoleScreenBufferInfo(win32.STDOUT)
|
return get_terminal_size().columns
|
||||||
return csbi.dwSize.X
|
except ValueError:
|
||||||
elif curses:
|
return self.DEFAULT_WIDTH
|
||||||
try:
|
|
||||||
curses.setupterm(os.environ.get("TERM"),
|
|
||||||
self._base_stream.fileno())
|
|
||||||
return curses.tigetnum("cols")
|
|
||||||
except curses.error:
|
|
||||||
pass
|
|
||||||
return self.DEFAULT_WIDTH
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_tty(self):
|
def is_tty(self):
|
||||||
"""Is this output stream a terminal?"""
|
"""Is this output stream a terminal?"""
|
||||||
return (hasattr(self._base_stream, "isatty") and
|
return getattr(self._base_stream, "isatty", lambda: False)()
|
||||||
self._base_stream.isatty())
|
|
||||||
|
|
||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
def temporary_context(self, context):
|
def temporary_context(self, context):
|
||||||
|
|
|
@ -3,3 +3,4 @@ lxml
|
||||||
pbr
|
pbr
|
||||||
requests>=2.0
|
requests>=2.0
|
||||||
six
|
six
|
||||||
|
backports.shutil_get_terminal_size; python_version<'3.3'
|
||||||
|
|
Loading…
Reference in a new issue