mirror of https://github.com/cheat/cheat.git
Refactored (2)
Created an `Editor` class out methods in the `Util` class to enhance code clarity.
This commit is contained in:
parent
8f757d7735
commit
ab87bb11c4
10
bin/cheat
10
bin/cheat
|
@ -36,10 +36,11 @@ Examples:
|
|||
|
||||
# require the dependencies
|
||||
from __future__ import print_function
|
||||
from cheat.sheets import Sheets
|
||||
from cheat.sheet import Sheet
|
||||
from cheat.utils import Utils
|
||||
from cheat.configuration import Configuration
|
||||
from cheat.editor import Editor
|
||||
from cheat.sheet import Sheet
|
||||
from cheat.sheets import Sheets
|
||||
from cheat.utils import Utils
|
||||
from docopt import docopt
|
||||
import os
|
||||
|
||||
|
@ -53,9 +54,10 @@ if __name__ == '__main__':
|
|||
config.validate()
|
||||
|
||||
# bootsrap
|
||||
editor = Editor(config)
|
||||
sheets = Sheets(config)
|
||||
utils = Utils(config)
|
||||
sheet = Sheet(sheets, utils)
|
||||
sheet = Sheet(sheets, utils, editor)
|
||||
|
||||
# list directories
|
||||
if options['--directories']:
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
from __future__ import print_function
|
||||
import subprocess
|
||||
|
||||
|
||||
class Editor:
|
||||
|
||||
def __init__(self, config):
|
||||
self._config = config
|
||||
|
||||
def editor(self):
|
||||
""" Determines the user's preferred editor """
|
||||
|
||||
# assert that the editor is set
|
||||
if (not self._config.cheat_editor):
|
||||
Utils.die(
|
||||
'You must set a CHEAT_EDITOR, VISUAL, or EDITOR environment '
|
||||
'variable or setting in order to create/edit a cheatsheet.'
|
||||
)
|
||||
|
||||
return self._config.cheat_editor
|
||||
|
||||
def open(self, filepath):
|
||||
""" Open `filepath` using the EDITOR specified by the env variables """
|
||||
editor_cmd = self.editor().split()
|
||||
try:
|
||||
subprocess.call(editor_cmd + [filepath])
|
||||
except OSError:
|
||||
Utils.die('Could not launch ' + self.editor())
|
|
@ -2,14 +2,16 @@ import io
|
|||
import os
|
||||
import shutil
|
||||
|
||||
from cheat.editor import Editor
|
||||
from cheat.utils import Utils
|
||||
|
||||
|
||||
class Sheet:
|
||||
|
||||
def __init__(self, sheets, utils):
|
||||
def __init__(self, sheets, utils, editor):
|
||||
self._sheets = sheets
|
||||
self._utils = utils
|
||||
self._editor = editor
|
||||
|
||||
def copy(self, current_sheet_path, new_sheet_path):
|
||||
""" Copies a sheet to a new path """
|
||||
|
@ -44,11 +46,11 @@ class Sheet:
|
|||
def create(self, sheet):
|
||||
""" Creates a cheatsheet """
|
||||
new_sheet_path = os.path.join(self._sheets.default_path(), sheet)
|
||||
self._utils.open_with_editor(new_sheet_path)
|
||||
self._editor.open(new_sheet_path)
|
||||
|
||||
def edit(self, sheet):
|
||||
""" Opens a cheatsheet for editing """
|
||||
self._utils.open_with_editor(self.path(sheet))
|
||||
self._editor.open(self.path(sheet))
|
||||
|
||||
def exists(self, sheet):
|
||||
""" Predicate that returns true if the sheet exists """
|
||||
|
|
|
@ -68,26 +68,6 @@ class Utils:
|
|||
Utils.warn(message)
|
||||
exit(1)
|
||||
|
||||
def editor(self):
|
||||
""" Determines the user's preferred editor """
|
||||
|
||||
# assert that the editor is set
|
||||
if (not self._config.cheat_editor):
|
||||
Utils.die(
|
||||
'You must set a CHEAT_EDITOR, VISUAL, or EDITOR environment '
|
||||
'variable or setting in order to create/edit a cheatsheet.'
|
||||
)
|
||||
|
||||
return self._config.cheat_editor
|
||||
|
||||
def open_with_editor(self, filepath):
|
||||
""" Open `filepath` using the EDITOR specified by the env variables """
|
||||
editor_cmd = self.editor().split()
|
||||
try:
|
||||
subprocess.call(editor_cmd + [filepath])
|
||||
except OSError:
|
||||
Utils.die('Could not launch ' + self.editor())
|
||||
|
||||
@staticmethod
|
||||
def warn(message):
|
||||
""" Prints a message to stderr """
|
||||
|
|
Loading…
Reference in New Issue