mirror of https://github.com/cheat/cheat.git
Add new env variables but hold compatibility with old ones
Legacy environmental variables like CHEATCOLORS are now higher in configuration hiearchy than new environmental variables in configuration files
This commit is contained in:
parent
8a8f30679d
commit
80b8cfc06b
|
@ -49,23 +49,26 @@ class Configuration:
|
||||||
def _read_env_vars_config(self):
|
def _read_env_vars_config(self):
|
||||||
read_config = {}
|
read_config = {}
|
||||||
|
|
||||||
# All these variables are left here because of backwards compatibility
|
# NOTE: These variables are left here because of backwards
|
||||||
|
# compatibility and are supported only as env vars but not in
|
||||||
if (os.environ.get('CHEAT_EDITOR')):
|
# configuration file
|
||||||
read_config['EDITOR'] = os.environ.get('CHEAT_EDITOR')
|
|
||||||
|
|
||||||
if (os.environ.get('VISUAL')):
|
if (os.environ.get('VISUAL')):
|
||||||
read_config['EDITOR'] = os.environ.get('VISUAL')
|
read_config['EDITOR'] = os.environ.get('VISUAL')
|
||||||
|
|
||||||
keys = ['DEFAULT_CHEAT_DIR',
|
# variables supported both in environment and configuration file
|
||||||
'CHEATPATH',
|
# NOTE: Variables without CHEAT_ prefix are legacy
|
||||||
'CHEATCOLORS',
|
# key is variable name and value is its legacy_alias
|
||||||
'EDITOR',
|
# if variable has no legacy alias then set to None
|
||||||
'CHEAT_HIGHLIGHT'
|
variables = {'CHEAT_DEFAULT_DIR': 'DEFAULT_CHEAT_DIR',
|
||||||
]
|
'CHEAT_PATH': 'CHEATPATH',
|
||||||
|
'CHEAT_COLORS': 'CHEATCOLORS',
|
||||||
|
'CHEAT_EDITOR': 'EDITOR',
|
||||||
|
'CHEAT_HIGHLIGHT': None
|
||||||
|
}
|
||||||
|
|
||||||
for k in keys:
|
for (k, v) in variables.items():
|
||||||
self._read_env_var(read_config, k)
|
self._read_env_var(read_config, k, v)
|
||||||
|
|
||||||
return read_config
|
return read_config
|
||||||
|
|
||||||
|
@ -83,9 +86,13 @@ class Configuration:
|
||||||
):
|
):
|
||||||
Utils.die("%s %s" % ('CHEAT_HIGHLIGHT must be one of:', colors))
|
Utils.die("%s %s" % ('CHEAT_HIGHLIGHT must be one of:', colors))
|
||||||
|
|
||||||
def _read_env_var(self, current_config, key):
|
def _read_env_var(self, current_config, key, alias=None):
|
||||||
if (os.environ.get(key)):
|
if os.environ.get(key) is not None:
|
||||||
current_config[key] = os.environ.get(key)
|
current_config[key] = os.environ.get(key)
|
||||||
|
return
|
||||||
|
elif alias is not None and os.environ.get(alias) is not None:
|
||||||
|
current_config[key] = os.environ.get(alias)
|
||||||
|
return
|
||||||
|
|
||||||
def _get_global_conf_file_path(self):
|
def _get_global_conf_file_path(self):
|
||||||
self.glob_config_path = (os.environ.get('CHEAT_GLOBAL_CONF_PATH')
|
self.glob_config_path = (os.environ.get('CHEAT_GLOBAL_CONF_PATH')
|
||||||
|
@ -96,17 +103,32 @@ class Configuration:
|
||||||
or os.path.expanduser('~/.config/cheat/cheat'))
|
or os.path.expanduser('~/.config/cheat/cheat'))
|
||||||
self.local_config_path = path
|
self.local_config_path = path
|
||||||
|
|
||||||
|
def _choose_value(self, primary_value_name, secondary_value_name):
|
||||||
|
""" Return primary or secondary value in saved_configuration
|
||||||
|
|
||||||
|
If primary value is in configuration then return it. If it is not
|
||||||
|
then return secondary. In the absence of both values return None
|
||||||
|
"""
|
||||||
|
|
||||||
|
primary_value = self._saved_configuration.get(primary_value_name)
|
||||||
|
secondary_value = self._saved_configuration.get(secondary_value_name)
|
||||||
|
|
||||||
|
if primary_value is not None:
|
||||||
|
return primary_value
|
||||||
|
else:
|
||||||
|
return secondary_value
|
||||||
|
|
||||||
def get_default_cheat_dir(self):
|
def get_default_cheat_dir(self):
|
||||||
return self._saved_configuration.get('DEFAULT_CHEAT_DIR')
|
return self._choose_value('CHEAT_DEFAULT_DIR', 'DEFAULT_CHEAT_DIR')
|
||||||
|
|
||||||
def get_cheatpath(self):
|
def get_cheatpath(self):
|
||||||
return self._saved_configuration.get('CHEATPATH')
|
return self._choose_value('CHEAT_PATH', 'CHEATPATH')
|
||||||
|
|
||||||
def get_cheatcolors(self):
|
def get_cheatcolors(self):
|
||||||
return self._saved_configuration.get('CHEATCOLORS')
|
return self._choose_value('CHEAT_COLORS', 'CHEATCOLORS')
|
||||||
|
|
||||||
def get_editor(self):
|
def get_editor(self):
|
||||||
return self._saved_configuration.get('EDITOR')
|
return self._choose_value('CHEAT_EDITOR', 'EDITOR')
|
||||||
|
|
||||||
def get_highlight(self):
|
def get_highlight(self):
|
||||||
return self._saved_configuration.get('CHEAT_HIGHLIGHT')
|
return self._saved_configuration.get('CHEAT_HIGHLIGHT')
|
||||||
|
|
|
@ -32,8 +32,11 @@ class Utils:
|
||||||
def colorize(self, sheet_content):
|
def colorize(self, sheet_content):
|
||||||
""" Colorizes cheatsheet content if so configured """
|
""" Colorizes cheatsheet content if so configured """
|
||||||
|
|
||||||
|
# cover all possible positive values to be safe
|
||||||
|
positive_values = ["True", "true", "1", 1, True]
|
||||||
|
|
||||||
# only colorize if configured to do so, and if stdout is a tty
|
# only colorize if configured to do so, and if stdout is a tty
|
||||||
if (self._displaycolors not in ["True", "true", "1", 1] or
|
if (self._displaycolors not in positive_values or
|
||||||
not sys.stdout.isatty()):
|
not sys.stdout.isatty()):
|
||||||
return sheet_content
|
return sheet_content
|
||||||
|
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
{"CHEATCOLORS":true,"EDITOR":"vi"}
|
{"CHEAT_COLORS":true,"CHEAT_EDITOR":"vi"}
|
||||||
|
|
Loading…
Reference in New Issue