1
0
Fork 0
mirror of synced 2024-11-22 08:15:34 -05:00

Merge branch 'bobwhitelock/add-force-color-option' into master

This commit is contained in:
Anish Athalye 2020-08-23 09:16:58 -04:00
commit 5294594f5a
2 changed files with 15 additions and 7 deletions

View file

@ -1,4 +1,5 @@
import os, glob import os, glob
import sys
from argparse import ArgumentParser from argparse import ArgumentParser
from .config import ConfigReader, ReadingError from .config import ConfigReader, ReadingError
@ -32,6 +33,8 @@ def add_options(parser):
help='only run specified directives', metavar='DIRECTIVE') help='only run specified directives', metavar='DIRECTIVE')
parser.add_argument('--except', nargs='+', dest='skip', parser.add_argument('--except', nargs='+', dest='skip',
help='skip specified directives', metavar='DIRECTIVE') help='skip specified directives', metavar='DIRECTIVE')
parser.add_argument('--force-color', dest='force_color', action='store_true',
help='force color output')
parser.add_argument('--no-color', dest='no_color', action='store_true', parser.add_argument('--no-color', dest='no_color', action='store_true',
help='disable color output') help='disable color output')
parser.add_argument('--version', action='store_true', parser.add_argument('--version', action='store_true',
@ -56,8 +59,17 @@ def main():
log.set_level(Level.INFO) log.set_level(Level.INFO)
if options.verbose: if options.verbose:
log.set_level(Level.DEBUG) log.set_level(Level.DEBUG)
if options.no_color:
if options.force_color and options.no_color:
log.error("`--force-color` and `--no-color` cannot both be provided")
exit(1)
elif options.force_color:
log.use_color(True)
elif options.no_color:
log.use_color(False) log.use_color(False)
else:
log.use_color(sys.stdout.isatty())
plugin_directories = list(options.plugin_dirs) plugin_directories = list(options.plugin_dirs)
if not options.disable_built_in_plugins: if not options.disable_built_in_plugins:
from .plugins import Clean, Create, Link, Shell from .plugins import Clean, Create, Link, Shell

View file

@ -1,4 +1,3 @@
import sys
from ..util.singleton import Singleton from ..util.singleton import Singleton
from ..util.compat import with_metaclass from ..util.compat import with_metaclass
from .color import Color from .color import Color
@ -34,14 +33,11 @@ class Messenger(with_metaclass(Singleton, object)):
def error(self, message): def error(self, message):
self.log(Level.ERROR, message) self.log(Level.ERROR, message)
def _should_use_color(self):
return self._use_color and sys.stdout.isatty()
def _color(self, level): def _color(self, level):
''' '''
Get a color (terminal escape sequence) according to a level. Get a color (terminal escape sequence) according to a level.
''' '''
if not self._should_use_color(): if not self._use_color:
return '' return ''
elif level < Level.DEBUG: elif level < Level.DEBUG:
return '' return ''
@ -60,7 +56,7 @@ class Messenger(with_metaclass(Singleton, object)):
''' '''
Get a reset color (terminal escape sequence). Get a reset color (terminal escape sequence).
''' '''
if not self._should_use_color(): if not self._use_color:
return '' return ''
else: else:
return Color.RESET return Color.RESET