Commit 224e414a authored by Bram Schoenmakers's avatar Bram Schoenmakers

Enforce the value of commandline argument -C if passed

Also, disable colors when not connected to a TTY. Before, when the
configuration file configured 256 colors, it would be used whether
regardless of being connected to a TTY.
parent 739a0631
...@@ -45,7 +45,7 @@ class ConfigTest(TopydoTest): ...@@ -45,7 +45,7 @@ class ConfigTest(TopydoTest):
""" Bad colour switch value. """ """ Bad colour switch value. """
# boolean settings must first be typecast to integers, because all # boolean settings must first be typecast to integers, because all
# strings evaulate to 'True' # strings evaulate to 'True'
self.assertEqual(config("test/data/ConfigTest4.conf").colors(), 0) self.assertEqual(config("test/data/ConfigTest4.conf").colors(), 16)
def test_config06(self): def test_config06(self):
""" Bad auto creation date switch value. """ """ Bad auto creation date switch value. """
......
...@@ -74,7 +74,7 @@ def write(p_file, p_string): ...@@ -74,7 +74,7 @@ def write(p_file, p_string):
ANSI codes are removed when the file is not a TTY (and colors are ANSI codes are removed when the file is not a TTY (and colors are
automatically determined). automatically determined).
""" """
if not config().colors(16 if p_file.isatty() else 0): if not config().colors(p_file.isatty()):
p_string = escape_ansi(p_string) p_string = escape_ansi(p_string)
if p_string: if p_string:
...@@ -185,6 +185,7 @@ class CLIApplicationBase(object): ...@@ -185,6 +185,7 @@ class CLIApplicationBase(object):
elif opt == "-c": elif opt == "-c":
alt_config_path = value alt_config_path = value
elif opt == "-C": elif opt == "-C":
overrides[('topydo', 'force_colors')] = '1'
overrides[('topydo', 'colors')] = value overrides[('topydo', 'colors')] = value
elif opt == "-t": elif opt == "-t":
overrides[('topydo', 'filename')] = value overrides[('topydo', 'filename')] = value
......
...@@ -63,6 +63,7 @@ class _Config: ...@@ -63,6 +63,7 @@ class _Config:
'topydo': { 'topydo': {
'default_command': 'ls', 'default_command': 'ls',
'colors': 'auto', 'colors': 'auto',
'force_colors': '0',
'filename': 'todo.txt', 'filename': 'todo.txt',
'archive_filename': 'done.txt', 'archive_filename': 'done.txt',
'identifiers': 'linenumber', 'identifiers': 'linenumber',
...@@ -188,14 +189,15 @@ class _Config: ...@@ -188,14 +189,15 @@ class _Config:
def default_command(self): def default_command(self):
return self.cp.get('topydo', 'default_command') return self.cp.get('topydo', 'default_command')
def colors(self, p_default=16): def colors(self, p_hint_possible=True):
""" """
Returns 0, 16 or 256 representing the number of colors that should be Returns 0, 16 or 256 representing the number of colors that should be
used in the output. When the configured value is 'auto', the default used in the output.
value is used.
A hint can be passed whether the device that will output the text
supports colors.
""" """
lookup = { lookup = {
'auto': p_default,
'false': 0, 'false': 0,
'no': 0, 'no': 0,
'0': 0, '0': 0,
...@@ -207,11 +209,21 @@ class _Config: ...@@ -207,11 +209,21 @@ class _Config:
} }
try: try:
return lookup[self.cp.get('topydo', 'colors').lower()] # pylint: disable=no-member forced = self.cp.get('topydo', 'force_colors') == '1'
except ValueError: except ValueError:
return lookup[self.defaults['topydo']['colors'].lower()] # pylint: disable=no-member forced = self.defaults['topydo']['force_colors'] == '1'
try:
colors = lookup[self.cp.get('topydo', 'colors').lower()] # pylint: disable=no-member
except ValueError:
colors = lookup[self.defaults['topydo']['colors'].lower()] # pylint: disable=no-member
except KeyError: except KeyError:
return 0 # for invalid values or 'auto'
colors = 16 if p_hint_possible else 0
# disable colors when no colors are enforced on the commandline and
# color support is determined automatically
return 0 if not forced and not p_hint_possible else colors
def todotxt(self): def todotxt(self):
return os.path.expanduser(self.cp.get('topydo', 'filename')) return os.path.expanduser(self.cp.get('topydo', 'filename'))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment