Skip to content

Commit

Permalink
added option to force color output, even if not a tty
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Molodowitch committed Nov 3, 2016
1 parent 42db059 commit 7da94e1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
19 changes: 16 additions & 3 deletions src/rez/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ class Bool(Setting):
schema = Schema(bool)
true_words = frozenset(["1", "true", "yes", "y", "on"])
false_words = frozenset(["0", "false", "no", "n", "off"])
all_words = true_words | false_words

def _parse_env_var(self, value):
value = value.lower()
Expand All @@ -134,10 +135,22 @@ def _parse_env_var(self, value):
elif value in self.false_words:
return False
else:
words = self.true_words | self.false_words
raise ConfigurationError(
"expected $%s to be one of: %s"
% (self._env_var_name, ", ".join(words)))
% (self._env_var_name, ", ".join(self.all_words)))


class ForceOrBool(Bool):
FORCE_STR = "force"

# need force first, or Bool.schema will coerce "force" to True
schema = Or(FORCE_STR, Bool.schema)
all_words = Bool.all_words | frozenset([FORCE_STR])

def _parse_env_var(self, value):
if value == self.FORCE_STR:
return value
super(ForceOrBool, self)._parse_env_var(value)


class Dict(Setting):
Expand Down Expand Up @@ -272,7 +285,7 @@ def _parse_env_var(self, value):
"memcached_resolve_min_compress_len": Int,
"allow_unversioned_packages": Bool,
"rxt_as_yaml": Bool,
"color_enabled": Bool,
"color_enabled": ForceOrBool,
"resolve_caching": Bool,
"cache_package_files": Bool,
"cache_listdir": Bool,
Expand Down
5 changes: 5 additions & 0 deletions src/rez/rezconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,11 @@
# Enables/disables colorization globally.
# Note: Turned off for Windows currently as there seems to be a problem with
# the Colorama module.
# May also set to the string "force", which will make rez output color styling
# information, even if the the output streams are not ttys. Useful if you are
# piping the output of rez, but will eventually be printing to a tty later.
# When force is used, will generally be set through an environemnt variable, ie,
# echo $(REZ_COLOR_ENABLED=force python -c "from rez.utils.colorize import Printer, local; Printer()('foo', local)")
color_enabled = (os.name == "posix")

### Do not move or delete this comment (__DOC_END__)
Expand Down
11 changes: 8 additions & 3 deletions src/rez/utils/colorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,10 @@ def is_tty(self):
"""
return stream_is_tty(self.stream)

@property
def is_colorized(self):
return config.get("color_enabled", False) == "force" or self.is_tty

def _get_style_function_for_level(self, level):
return self.STYLES.get(level, notset)

Expand All @@ -271,7 +275,7 @@ def emit(self, record):
try:
message = self.format(record)

if not self.is_tty:
if not self.is_colorized:
self.stream.write(message)
else:
style = self._get_style_function_for_level(record.levelno)
Expand All @@ -289,13 +293,14 @@ def emit(self, record):
class Printer(object):
def __init__(self, buf=sys.stdout):
self.buf = buf
self.tty = stream_is_tty(buf)
self.colorize = (config.get("color_enabled", False) == "force") \
or stream_is_tty(buf)

def __call__(self, msg='', style=None):
print >> self.buf, self.get(msg, style)

def get(self, msg, style=None):
if style and self.tty:
if style and self.colorize:
msg = style(msg)
return msg

Expand Down

0 comments on commit 7da94e1

Please sign in to comment.