mirror of https://github.com/cheat/cheat.git
Improve handling of settings
Constructors of classes which need direct access to configuration now take Config class instance as parameter which will give them better maintainability in the future CHEAT_HIGHLIGHT has been added to Configuration class
This commit is contained in:
parent
3a4c2a887d
commit
5eec6bf040
|
@ -61,9 +61,9 @@ if __name__ == '__main__':
|
|||
options = docopt(__doc__, version='cheat 2.3.1')
|
||||
|
||||
config = Configuration()
|
||||
sheets = Sheets(config.get_default_cheat_dir(),config.get_cheatpath())
|
||||
sheet = Sheet(config.get_default_cheat_dir(),config.get_cheatpath(),config.get_editor())
|
||||
utils = Utils(config.get_cheatcolors(),config.get_editor())
|
||||
sheets = Sheets(config)
|
||||
utils = Utils(config)
|
||||
sheet = Sheet(sheets, utils)
|
||||
|
||||
# list directories
|
||||
if options['--directories']:
|
||||
|
|
|
@ -28,6 +28,8 @@ class Configuration:
|
|||
|
||||
merged_config.update(self._read_env_vars_config())
|
||||
|
||||
|
||||
|
||||
return merged_config
|
||||
|
||||
|
||||
|
@ -51,7 +53,12 @@ class Configuration:
|
|||
if (os.environ.get('VISUAL')):
|
||||
read_config['EDITOR'] = os.environ.get('VISUAL')
|
||||
|
||||
keys = ['DEFAULT_CHEAT_DIR','CHEATPATH','CHEATCOLORS','EDITOR']
|
||||
keys = ['DEFAULT_CHEAT_DIR',
|
||||
'CHEATPATH',
|
||||
'CHEATCOLORS',
|
||||
'EDITOR',
|
||||
'CHEAT_HIGHLIGHT'
|
||||
]
|
||||
|
||||
for k in keys:
|
||||
self._read_env_var(read_config,k)
|
||||
|
@ -88,3 +95,6 @@ class Configuration:
|
|||
|
||||
def get_editor(self):
|
||||
return self._saved_configuration.get('EDITOR')
|
||||
|
||||
def get_highlight(self):
|
||||
return self._saved_configuration.get('CHEAT_HIGHLIGHT')
|
||||
|
|
|
@ -1,16 +1,15 @@
|
|||
import os
|
||||
import shutil
|
||||
|
||||
from cheat.sheets import Sheets
|
||||
from cheat.utils import Utils
|
||||
|
||||
|
||||
class Sheet:
|
||||
|
||||
|
||||
def __init__(self,default_cheat_dir,cheatpath,editor_exec):
|
||||
self.sheets_instance = Sheets(default_cheat_dir,cheatpath)
|
||||
self.utils_instance = Utils(None,editor_exec)
|
||||
def __init__(self, sheets, utils):
|
||||
self._sheets = sheets
|
||||
self._utils = utils
|
||||
|
||||
|
||||
def copy(self,current_sheet_path, new_sheet_path):
|
||||
|
@ -36,7 +35,7 @@ class Sheet:
|
|||
# if the cheatsheet exists but not in the default_path, copy it to the
|
||||
# default path before editing
|
||||
elif self.exists(sheet) and not self.exists_in_default_path(sheet):
|
||||
self.copy(self.path(sheet), os.path.join(self.sheets_instance.default_path(), sheet))
|
||||
self.copy(self.path(sheet), os.path.join(self._sheets.default_path(), sheet))
|
||||
self.edit(sheet)
|
||||
|
||||
# if it exists and is in the default path, then just open it
|
||||
|
@ -46,34 +45,34 @@ class Sheet:
|
|||
|
||||
def create(self,sheet):
|
||||
""" Creates a cheatsheet """
|
||||
new_sheet_path = os.path.join(self.sheets_instance.default_path(), sheet)
|
||||
self.utils_instance.open_with_editor(new_sheet_path)
|
||||
new_sheet_path = os.path.join(self._sheets.default_path(), sheet)
|
||||
self._utils.open_with_editor(new_sheet_path)
|
||||
|
||||
|
||||
def edit(self,sheet):
|
||||
""" Opens a cheatsheet for editing """
|
||||
self.utils_instance.open_with_editor(self.path(sheet))
|
||||
self._utils.open_with_editor(self.path(sheet))
|
||||
|
||||
|
||||
def exists(self,sheet):
|
||||
""" Predicate that returns true if the sheet exists """
|
||||
return sheet in self.sheets_instance.get() and os.access(self.path(sheet), os.R_OK)
|
||||
return sheet in self._sheets.get() and os.access(self.path(sheet), os.R_OK)
|
||||
|
||||
|
||||
def exists_in_default_path(self,sheet):
|
||||
""" Predicate that returns true if the sheet exists in default_path"""
|
||||
default_path_sheet = os.path.join(self.sheets_instance.default_path(), sheet)
|
||||
return sheet in self.sheets_instance.get() and os.access(default_path_sheet, os.R_OK)
|
||||
default_path_sheet = os.path.join(self._sheets.default_path(), sheet)
|
||||
return sheet in self._sheets.get() and os.access(default_path_sheet, os.R_OK)
|
||||
|
||||
|
||||
def is_writable(self,sheet):
|
||||
""" Predicate that returns true if the sheet is writeable """
|
||||
return sheet in self.sheets_instance.get() and os.access(self.path(sheet), os.W_OK)
|
||||
return sheet in self._sheets.get() and os.access(self.path(sheet), os.W_OK)
|
||||
|
||||
|
||||
def path(self,sheet):
|
||||
""" Returns a sheet's filesystem path """
|
||||
return self.sheets_instance.get()[sheet]
|
||||
return self._sheets.get()[sheet]
|
||||
|
||||
|
||||
def read(self,sheet):
|
||||
|
|
|
@ -1,21 +1,20 @@
|
|||
import os
|
||||
|
||||
from cheat.configuration import Configuration
|
||||
from cheat.utils import Utils
|
||||
|
||||
class Sheets:
|
||||
|
||||
|
||||
def __init__(self,default_cheat_dir,cheatpath):
|
||||
self.default_cheat_dir = default_cheat_dir
|
||||
self.cheatpath = cheatpath
|
||||
|
||||
def __init__(self, config):
|
||||
self._default_cheat_dir = config.get_default_cheat_dir()
|
||||
self._cheatpath = config.get_cheatpath()
|
||||
self._utils = Utils(config)
|
||||
|
||||
def default_path(self):
|
||||
""" Returns the default cheatsheet path """
|
||||
|
||||
# determine the default cheatsheet dir
|
||||
default_sheets_dir = self.default_cheat_dir or os.path.join('~', '.cheat')
|
||||
default_sheets_dir = self._default_cheat_dir or os.path.join('~', '.cheat')
|
||||
default_sheets_dir = os.path.expanduser(os.path.expandvars(default_sheets_dir))
|
||||
|
||||
# create the DEFAULT_CHEAT_DIR if it does not exist
|
||||
|
@ -64,8 +63,8 @@ class Sheets:
|
|||
]
|
||||
|
||||
# merge the CHEATPATH paths into the sheet_paths
|
||||
if self.cheatpath:
|
||||
for path in self.cheatpath.split(os.pathsep):
|
||||
if self._cheatpath:
|
||||
for path in self._cheatpath.split(os.pathsep):
|
||||
if os.path.isdir(path):
|
||||
sheet_paths.append(path)
|
||||
|
||||
|
@ -92,7 +91,7 @@ class Sheets:
|
|||
match = ''
|
||||
for line in open(cheatsheet[1]):
|
||||
if term in line:
|
||||
match += ' ' + line
|
||||
match += ' ' + self._utils.highlight(term, line)
|
||||
|
||||
if match != '':
|
||||
result += cheatsheet[0] + ":\n" + match + "\n"
|
||||
|
|
|
@ -6,16 +6,16 @@ import sys
|
|||
class Utils:
|
||||
|
||||
|
||||
def __init__(self,cheatcolors,editor_executable):
|
||||
self.displaycolors = cheatcolors
|
||||
self.editor_executable = editor_executable
|
||||
|
||||
def __init__(self,config):
|
||||
self._displaycolors = config.get_cheatcolors()
|
||||
self._editor_executable = config.get_editor()
|
||||
self._highlight_color = config.get_highlight()
|
||||
|
||||
def highlight(self, needle, haystack):
|
||||
""" Highlights a search term matched within a line """
|
||||
|
||||
# if a highlight color is not configured, exit early
|
||||
if not 'CHEAT_HIGHLIGHT' in os.environ:
|
||||
if not self._highlight_color:
|
||||
return haystack
|
||||
|
||||
# otherwise, attempt to import the termcolor library
|
||||
|
@ -27,14 +27,14 @@ class Utils:
|
|||
return haystack
|
||||
|
||||
# if the import succeeds, colorize the needle in haystack
|
||||
return haystack.replace(needle, colored(needle, os.environ.get('CHEAT_HIGHLIGHT')))
|
||||
return haystack.replace(needle, colored(needle, self._highlight_color))
|
||||
|
||||
|
||||
def colorize(self,sheet_content):
|
||||
""" Colorizes cheatsheet content if so configured """
|
||||
|
||||
# only colorize if configured to do so, and if stdout is a tty
|
||||
if not self.displaycolors or not sys.stdout.isatty():
|
||||
if not self._displaycolors or not sys.stdout.isatty():
|
||||
return sheet_content
|
||||
|
||||
# don't attempt to colorize an empty cheatsheet
|
||||
|
@ -77,13 +77,13 @@ class Utils:
|
|||
""" Determines the user's preferred editor """
|
||||
|
||||
# assert that the editor is set
|
||||
if (not self.editor_executable):
|
||||
if (not self._editor_executable):
|
||||
Utils.die(
|
||||
'You must set a CHEAT_EDITOR, VISUAL, or EDITOR environment '
|
||||
'variable or setting in order to create/edit a cheatsheet.'
|
||||
)
|
||||
|
||||
return self.editor_executable
|
||||
return self._editor_executable
|
||||
|
||||
|
||||
def open_with_editor(self,filepath):
|
||||
|
|
Loading…
Reference in New Issue