summaryrefslogtreecommitdiff
path: root/dotbot/messenger
diff options
context:
space:
mode:
authorPhilip Hallstrom <philip@pjkh.com>2018-10-06 16:08:26 -0700
committerPhilip Hallstrom <philip@pjkh.com>2018-10-06 16:08:26 -0700
commita22d980cdfc31718e28b5ba00b135e4396698660 (patch)
tree8e69dbce24fb7510d04e631ebcb25c1e0bd38b86 /dotbot/messenger
parentf197ededb11e7613aa0f4c5f88311e9ab4653749 (diff)
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.
Diffstat (limited to 'dotbot/messenger')
-rw-r--r--dotbot/messenger/messenger.py11
1 files changed, 9 insertions, 2 deletions
diff --git a/dotbot/messenger/messenger.py b/dotbot/messenger/messenger.py
index f87a367..b83e3f2 100644
--- a/dotbot/messenger/messenger.py
+++ b/dotbot/messenger/messenger.py
@@ -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