mirror of
https://github.com/cheat/cheat.git
synced 2025-04-09 11:14:03 +02:00
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')
|
options = docopt(__doc__, version='cheat 2.3.1')
|
||||||
|
|
||||||
config = Configuration()
|
config = Configuration()
|
||||||
sheets = Sheets(config.get_default_cheat_dir(),config.get_cheatpath())
|
sheets = Sheets(config)
|
||||||
sheet = Sheet(config.get_default_cheat_dir(),config.get_cheatpath(),config.get_editor())
|
utils = Utils(config)
|
||||||
utils = Utils(config.get_cheatcolors(),config.get_editor())
|
sheet = Sheet(sheets, utils)
|
||||||
|
|
||||||
# list directories
|
# list directories
|
||||||
if options['--directories']:
|
if options['--directories']:
|
||||||
|
@ -28,6 +28,8 @@ class Configuration:
|
|||||||
|
|
||||||
merged_config.update(self._read_env_vars_config())
|
merged_config.update(self._read_env_vars_config())
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return merged_config
|
return merged_config
|
||||||
|
|
||||||
|
|
||||||
@ -51,7 +53,12 @@ class Configuration:
|
|||||||
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','CHEATPATH','CHEATCOLORS','EDITOR']
|
keys = ['DEFAULT_CHEAT_DIR',
|
||||||
|
'CHEATPATH',
|
||||||
|
'CHEATCOLORS',
|
||||||
|
'EDITOR',
|
||||||
|
'CHEAT_HIGHLIGHT'
|
||||||
|
]
|
||||||
|
|
||||||
for k in keys:
|
for k in keys:
|
||||||
self._read_env_var(read_config,k)
|
self._read_env_var(read_config,k)
|
||||||
@ -88,3 +95,6 @@ class Configuration:
|
|||||||
|
|
||||||
def get_editor(self):
|
def get_editor(self):
|
||||||
return self._saved_configuration.get('EDITOR')
|
return self._saved_configuration.get('EDITOR')
|
||||||
|
|
||||||
|
def get_highlight(self):
|
||||||
|
return self._saved_configuration.get('CHEAT_HIGHLIGHT')
|
||||||
|
@ -1,16 +1,15 @@
|
|||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
|
|
||||||
from cheat.sheets import Sheets
|
|
||||||
from cheat.utils import Utils
|
from cheat.utils import Utils
|
||||||
|
|
||||||
|
|
||||||
class Sheet:
|
class Sheet:
|
||||||
|
|
||||||
|
|
||||||
def __init__(self,default_cheat_dir,cheatpath,editor_exec):
|
def __init__(self, sheets, utils):
|
||||||
self.sheets_instance = Sheets(default_cheat_dir,cheatpath)
|
self._sheets = sheets
|
||||||
self.utils_instance = Utils(None,editor_exec)
|
self._utils = utils
|
||||||
|
|
||||||
|
|
||||||
def copy(self,current_sheet_path, new_sheet_path):
|
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
|
# if the cheatsheet exists but not in the default_path, copy it to the
|
||||||
# default path before editing
|
# default path before editing
|
||||||
elif self.exists(sheet) and not self.exists_in_default_path(sheet):
|
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)
|
self.edit(sheet)
|
||||||
|
|
||||||
# if it exists and is in the default path, then just open it
|
# if it exists and is in the default path, then just open it
|
||||||
@ -46,34 +45,34 @@ class Sheet:
|
|||||||
|
|
||||||
def create(self,sheet):
|
def create(self,sheet):
|
||||||
""" Creates a cheatsheet """
|
""" Creates a cheatsheet """
|
||||||
new_sheet_path = os.path.join(self.sheets_instance.default_path(), sheet)
|
new_sheet_path = os.path.join(self._sheets.default_path(), sheet)
|
||||||
self.utils_instance.open_with_editor(new_sheet_path)
|
self._utils.open_with_editor(new_sheet_path)
|
||||||
|
|
||||||
|
|
||||||
def edit(self,sheet):
|
def edit(self,sheet):
|
||||||
""" Opens a cheatsheet for editing """
|
""" 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):
|
def exists(self,sheet):
|
||||||
""" Predicate that returns true if the sheet exists """
|
""" 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):
|
def exists_in_default_path(self,sheet):
|
||||||
""" Predicate that returns true if the sheet exists in default_path"""
|
""" Predicate that returns true if the sheet exists in default_path"""
|
||||||
default_path_sheet = os.path.join(self.sheets_instance.default_path(), sheet)
|
default_path_sheet = os.path.join(self._sheets.default_path(), sheet)
|
||||||
return sheet in self.sheets_instance.get() and os.access(default_path_sheet, os.R_OK)
|
return sheet in self._sheets.get() and os.access(default_path_sheet, os.R_OK)
|
||||||
|
|
||||||
|
|
||||||
def is_writable(self,sheet):
|
def is_writable(self,sheet):
|
||||||
""" Predicate that returns true if the sheet is writeable """
|
""" 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):
|
def path(self,sheet):
|
||||||
""" Returns a sheet's filesystem path """
|
""" Returns a sheet's filesystem path """
|
||||||
return self.sheets_instance.get()[sheet]
|
return self._sheets.get()[sheet]
|
||||||
|
|
||||||
|
|
||||||
def read(self,sheet):
|
def read(self,sheet):
|
||||||
|
@ -1,21 +1,20 @@
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
from cheat.configuration import Configuration
|
|
||||||
from cheat.utils import Utils
|
from cheat.utils import Utils
|
||||||
|
|
||||||
class Sheets:
|
class Sheets:
|
||||||
|
|
||||||
|
|
||||||
def __init__(self,default_cheat_dir,cheatpath):
|
def __init__(self, config):
|
||||||
self.default_cheat_dir = default_cheat_dir
|
self._default_cheat_dir = config.get_default_cheat_dir()
|
||||||
self.cheatpath = cheatpath
|
self._cheatpath = config.get_cheatpath()
|
||||||
|
self._utils = Utils(config)
|
||||||
|
|
||||||
def default_path(self):
|
def default_path(self):
|
||||||
""" Returns the default cheatsheet path """
|
""" Returns the default cheatsheet path """
|
||||||
|
|
||||||
# determine the default cheatsheet dir
|
# 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))
|
default_sheets_dir = os.path.expanduser(os.path.expandvars(default_sheets_dir))
|
||||||
|
|
||||||
# create the DEFAULT_CHEAT_DIR if it does not exist
|
# create the DEFAULT_CHEAT_DIR if it does not exist
|
||||||
@ -64,8 +63,8 @@ class Sheets:
|
|||||||
]
|
]
|
||||||
|
|
||||||
# merge the CHEATPATH paths into the sheet_paths
|
# merge the CHEATPATH paths into the sheet_paths
|
||||||
if self.cheatpath:
|
if self._cheatpath:
|
||||||
for path in self.cheatpath.split(os.pathsep):
|
for path in self._cheatpath.split(os.pathsep):
|
||||||
if os.path.isdir(path):
|
if os.path.isdir(path):
|
||||||
sheet_paths.append(path)
|
sheet_paths.append(path)
|
||||||
|
|
||||||
@ -92,7 +91,7 @@ class Sheets:
|
|||||||
match = ''
|
match = ''
|
||||||
for line in open(cheatsheet[1]):
|
for line in open(cheatsheet[1]):
|
||||||
if term in line:
|
if term in line:
|
||||||
match += ' ' + line
|
match += ' ' + self._utils.highlight(term, line)
|
||||||
|
|
||||||
if match != '':
|
if match != '':
|
||||||
result += cheatsheet[0] + ":\n" + match + "\n"
|
result += cheatsheet[0] + ":\n" + match + "\n"
|
||||||
|
@ -6,16 +6,16 @@ import sys
|
|||||||
class Utils:
|
class Utils:
|
||||||
|
|
||||||
|
|
||||||
def __init__(self,cheatcolors,editor_executable):
|
def __init__(self,config):
|
||||||
self.displaycolors = cheatcolors
|
self._displaycolors = config.get_cheatcolors()
|
||||||
self.editor_executable = editor_executable
|
self._editor_executable = config.get_editor()
|
||||||
|
self._highlight_color = config.get_highlight()
|
||||||
|
|
||||||
def highlight(self, needle, haystack):
|
def highlight(self, needle, haystack):
|
||||||
""" Highlights a search term matched within a line """
|
""" Highlights a search term matched within a line """
|
||||||
|
|
||||||
# if a highlight color is not configured, exit early
|
# if a highlight color is not configured, exit early
|
||||||
if not 'CHEAT_HIGHLIGHT' in os.environ:
|
if not self._highlight_color:
|
||||||
return haystack
|
return haystack
|
||||||
|
|
||||||
# otherwise, attempt to import the termcolor library
|
# otherwise, attempt to import the termcolor library
|
||||||
@ -27,14 +27,14 @@ class Utils:
|
|||||||
return haystack
|
return haystack
|
||||||
|
|
||||||
# if the import succeeds, colorize the needle in 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):
|
def colorize(self,sheet_content):
|
||||||
""" Colorizes cheatsheet content if so configured """
|
""" Colorizes cheatsheet content if so configured """
|
||||||
|
|
||||||
# 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 not self.displaycolors or not sys.stdout.isatty():
|
if not self._displaycolors or not sys.stdout.isatty():
|
||||||
return sheet_content
|
return sheet_content
|
||||||
|
|
||||||
# don't attempt to colorize an empty cheatsheet
|
# don't attempt to colorize an empty cheatsheet
|
||||||
@ -77,13 +77,13 @@ class Utils:
|
|||||||
""" Determines the user's preferred editor """
|
""" Determines the user's preferred editor """
|
||||||
|
|
||||||
# assert that the editor is set
|
# assert that the editor is set
|
||||||
if (not self.editor_executable):
|
if (not self._editor_executable):
|
||||||
Utils.die(
|
Utils.die(
|
||||||
'You must set a CHEAT_EDITOR, VISUAL, or EDITOR environment '
|
'You must set a CHEAT_EDITOR, VISUAL, or EDITOR environment '
|
||||||
'variable or setting in order to create/edit a cheatsheet.'
|
'variable or setting in order to create/edit a cheatsheet.'
|
||||||
)
|
)
|
||||||
|
|
||||||
return self.editor_executable
|
return self._editor_executable
|
||||||
|
|
||||||
|
|
||||||
def open_with_editor(self,filepath):
|
def open_with_editor(self,filepath):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user