Add colors support for Microsoft Windows via optional colorama dependency.

This commit is contained in:
Andris Raugulis 2016-10-25 11:50:12 +03:00
parent 855d64f5b1
commit 385c230376

View File

@ -42,6 +42,11 @@ try:
from typing import List, Tuple, Optional, Callable, Union, Any from typing import List, Tuple, Optional, Callable, Union, Any
except ImportError: except ImportError:
pass pass
try:
from colorama import init as colorama_init
colorama_init()
except ImportError:
pass
def usage(err=None): def usage(err=None):
@ -177,6 +182,11 @@ class Output(object):
if not self.batch: if not self.batch:
print() print()
@property
def colors_supported(self):
# type: () -> bool
return 'colorama' in sys.modules or os.name == 'posix'
@staticmethod @staticmethod
def _colorized(color): def _colorized(color):
# type: (str) -> Callable[[text_type], None] # type: (str) -> Callable[[text_type], None]
@ -188,7 +198,7 @@ class Output(object):
return lambda x: None return lambda x: None
if not self.getlevel(name) >= self.__minlevel: if not self.getlevel(name) >= self.__minlevel:
return lambda x: None return lambda x: None
if self.colors and os.name == 'posix' and name in self.COLORS: if self.colors and self.colors_supported and name in self.COLORS:
color = '\033[0;{0}m'.format(self.COLORS[name]) color = '\033[0;{0}m'.format(self.COLORS[name])
return self._colorized(color) return self._colorized(color)
else: else: