From 902665bce668a58996de657a65c5ae3002a8810b Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Mon, 22 Dec 2014 15:17:59 -0500 Subject: add a global --color option If you want to turn off colors for commands, you have to manually adjust the git config settings (in various locations). If you're writing scripts though, you often don't want to modify those locations. Add a commandline option to explicitly control things. The default behavior is unchanged -- we still scan the config files. Change-Id: I54a3fd8e1918bac180aadd7c7d3004f069b02522 --- color.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) (limited to 'color.py') diff --git a/color.py b/color.py index 7970198a..b2799286 100644 --- a/color.py +++ b/color.py @@ -83,15 +83,38 @@ def _Color(fg = None, bg = None, attr = None): return code +DEFAULT = None + +def SetDefaultColoring(state): + """Set coloring behavior to |state|. + + This is useful for overriding config options via the command line. + """ + if state is None: + # Leave it alone -- return quick! + return + + global DEFAULT + state = state.lower() + if state in ('auto',): + DEFAULT = state + elif state in ('always', 'yes', 'true', True): + DEFAULT = 'always' + elif state in ('never', 'no', 'false', False): + DEFAULT = 'never' + + class Coloring(object): def __init__(self, config, section_type): self._section = 'color.%s' % section_type self._config = config self._out = sys.stdout - on = self._config.GetString(self._section) + on = DEFAULT if on is None: - on = self._config.GetString('color.ui') + on = self._config.GetString(self._section) + if on is None: + on = self._config.GetString('color.ui') if on == 'auto': if pager.active or os.isatty(1): -- cgit v1.2.3-54-g00ecf