mirror of
https://github.com/Erreur32/cheat.git
synced 2024-11-16 17:08:35 +01:00
Merged the 'create' and 'edit' functionality in order to DRY out the code a bit, because (IMO) the user experience is actually somewhat better when these two commands are merged.
This commit is contained in:
parent
34f3eafb1a
commit
ba4093620a
1 changed files with 44 additions and 61 deletions
75
cheat
75
cheat
|
@ -50,14 +50,23 @@ def cheat_files(cheat_directories):
|
|||
return cheats
|
||||
|
||||
|
||||
def create_cheatsheet(cheat, cheatsheets):
|
||||
"Creates a new cheatsheet."
|
||||
if cheat in cheatsheets:
|
||||
print ('Cheatsheet "%s" already exists' % cheat)
|
||||
def edit_cheatsheet(cheat, cheatsheets):
|
||||
"Creates or edits a cheatsheet"
|
||||
|
||||
# if the cheatsheet does not already exist, create it
|
||||
if cheat not in cheatsheets:
|
||||
# make sure EDITOR environment variable is set and that at least 3 arguments
|
||||
# are given
|
||||
if 'EDITOR' not in os.environ:
|
||||
print('In order to use "create" or "edit" you must set your '
|
||||
'EDITOR environment variable to your favorite editor\'s path')
|
||||
exit()
|
||||
|
||||
elif len(sys.argv) < 3:
|
||||
print ('Must provide a cheatsheet to edit/create')
|
||||
exit()
|
||||
|
||||
import cheatsheets as cs
|
||||
|
||||
# Attempt to write the new cheatsheet to the user's ~/.cheat dir if it
|
||||
# exists. If it does not exist, attempt to create it.
|
||||
if os.access(DEFAULT_CHEAT_DIR, os.W_OK) or os.makedirs(DEFAULT_CHEAT_DIR):
|
||||
|
@ -70,25 +79,15 @@ def create_cheatsheet(cheat, cheatsheets):
|
|||
subprocess.call([os.environ['EDITOR'],
|
||||
os.path.join(cs.cheat_dir, cheat)])
|
||||
|
||||
exit()
|
||||
|
||||
|
||||
def edit_cheatsheet(cheat, cheatsheets):
|
||||
"Edits a pre-existing cheatsheet."
|
||||
if cheat not in cheatsheets:
|
||||
print ('No cheatsheet found for "%s"' % cheat)
|
||||
ans = raw_input('Create cheatsheet for "%s" (Y/N)? ' % cheat)
|
||||
if ans.lower() in ['y', 'yes']:
|
||||
create_cheatsheet(cheat, cheatsheets)
|
||||
# if the cheatsheet does already exist, open it for editing
|
||||
else:
|
||||
exit()
|
||||
|
||||
subprocess.call([os.environ['EDITOR'],
|
||||
os.path.join(cheatsheets[cheat], cheat)])
|
||||
exit()
|
||||
|
||||
|
||||
def help(cheatsheets):
|
||||
"Displays the program help"
|
||||
|
||||
print dedent('''
|
||||
Usage: cheat [OPTIONS] <keyphrase>
|
||||
Examples:
|
||||
|
@ -99,10 +98,7 @@ def help(cheatsheets):
|
|||
To look up 'git commit' (a subcommand):
|
||||
cheat git commit
|
||||
|
||||
To create a cheatsheet for 'foo':
|
||||
cheat -c foo
|
||||
|
||||
To edit the cheatsheet for 'foo':
|
||||
To edit or edit the cheatsheet for 'foo':
|
||||
cheat -e foo
|
||||
|
||||
Available keyphrases:
|
||||
|
@ -151,41 +147,28 @@ def main():
|
|||
# list the files in the ~/.cheat directory
|
||||
cheatsheets = cheat_files(cheat_dirs)
|
||||
|
||||
# print help if requested
|
||||
if keyphrase.lower() in ['', 'cheat', 'help', '-h', '-help', '--help']:
|
||||
# @TODO: refactor the below into argparse
|
||||
|
||||
# print help
|
||||
if keyphrase in ['', 'cheat', 'help', '-h', '-help', '--help']:
|
||||
help(cheatsheets)
|
||||
|
||||
# create/edit option
|
||||
option = sys.argv[1].lower()
|
||||
if option in ['-e', '--edit', '-c', '--create']:
|
||||
# make sure EDITOR environment variable is set and that at least 3 arguments
|
||||
# are given
|
||||
if 'EDITOR' not in os.environ:
|
||||
print('In order to use "create" or "edit" you must set your '
|
||||
'EDITOR environment variable to your favorite editor\'s path')
|
||||
exit()
|
||||
elif len(sys.argv) < 3:
|
||||
print ('Must provide a cheatsheet to edit/create')
|
||||
exit()
|
||||
|
||||
# if the user wants to edit a cheatsheet
|
||||
if option in ['-e', '--edit']:
|
||||
edit_cheatsheet(' '.join(sys.argv[2:]), cheatsheets)
|
||||
|
||||
# if the user wants to create a cheatsheet
|
||||
elif option in ['-c', '--create']:
|
||||
create_cheatsheet(' '.join(sys.argv[2:]), cheatsheets)
|
||||
|
||||
# list cheat directories and exit
|
||||
if option in ['-d', '--cheat-directories']:
|
||||
if keyphrase in ['-d', '--cheat-directories']:
|
||||
print("\n".join(cheat_directories()))
|
||||
exit()
|
||||
|
||||
# list cheatsheets and exit
|
||||
if option in ['-l', '--list']:
|
||||
if keyphrase in ['-l', '--list']:
|
||||
print list_cheatsheets(cheatsheets)
|
||||
exit()
|
||||
|
||||
# if the user wants to edit a cheatsheet
|
||||
if (sys.argv[1]) and (sys.argv[1] in ['-e', '--edit']):
|
||||
edit_cheatsheet(' '.join(sys.argv[2:]), cheatsheets)
|
||||
exit()
|
||||
|
||||
# print the cheatsheet if it exists
|
||||
if keyphrase in cheatsheets:
|
||||
filename = os.path.join(cheatsheets[keyphrase], keyphrase)
|
||||
|
|
Loading…
Reference in a new issue