1
0
Fork 0
mirror of synced 2024-12-21 13:41:07 -05:00

Add option --no-color to suppress colorization of output

By default, if output is a TTY, dotbot will colorize the output. This
patch adds the option to pass `--no-color` to dotbot to have it suppress
this colorization.
This commit is contained in:
Philip Hallstrom 2018-10-06 16:08:26 -07:00
parent f197ededb1
commit a22d980cdf
2 changed files with 13 additions and 2 deletions

View file

@ -28,6 +28,8 @@ def add_options(parser):
action='store_true', help='disable built-in plugins')
parser.add_argument('--plugin-dir', action='append', dest='plugin_dirs', default=[],
metavar='PLUGIN_DIR', help='load all plugins in PLUGIN_DIR')
parser.add_argument('--no-color', dest='no_color', action='store_true',
help='disable color output')
parser.add_argument('--version', action='store_true',
help='show program\'s version number and exit')
@ -50,6 +52,8 @@ def main():
log.set_level(Level.INFO)
if options.verbose:
log.set_level(Level.DEBUG)
if options.no_color:
log.use_color(False)
plugin_directories = list(options.plugin_dirs)
if not options.disable_built_in_plugins:
from .plugins import Clean, Link, Shell

View file

@ -7,10 +7,14 @@ from .level import Level
class Messenger(with_metaclass(Singleton, object)):
def __init__(self, level = Level.LOWINFO):
self.set_level(level)
self.use_color(True)
def set_level(self, level):
self._level = level
def use_color(self, yesno):
self._use_color = yesno
def log(self, level, message):
if (level >= self._level):
print('%s%s%s' % (self._color(level), message, self._reset()))
@ -30,11 +34,14 @@ class Messenger(with_metaclass(Singleton, object)):
def error(self, message):
self.log(Level.ERROR, message)
def _should_use_color(self):
return self._use_color and sys.stdout.isatty()
def _color(self, level):
'''
Get a color (terminal escape sequence) according to a level.
'''
if not sys.stdout.isatty():
if not self._should_use_color():
return ''
elif level < Level.DEBUG:
return ''
@ -53,7 +60,7 @@ class Messenger(with_metaclass(Singleton, object)):
'''
Get a reset color (terminal escape sequence).
'''
if not sys.stdout.isatty():
if not self._should_use_color():
return ''
else:
return Color.RESET