mirror of https://github.com/cheat/cheat.git
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
75
cheat
75
cheat
|
@ -50,14 +50,23 @@ def cheat_files(cheat_directories):
|
||||||
return cheats
|
return cheats
|
||||||
|
|
||||||
|
|
||||||
def create_cheatsheet(cheat, cheatsheets):
|
def edit_cheatsheet(cheat, cheatsheets):
|
||||||
"Creates a new cheatsheet."
|
"Creates or edits a cheatsheet"
|
||||||
if cheat in cheatsheets:
|
|
||||||
print ('Cheatsheet "%s" already exists' % cheat)
|
# 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()
|
exit()
|
||||||
|
|
||||||
import cheatsheets as cs
|
import cheatsheets as cs
|
||||||
|
|
||||||
# Attempt to write the new cheatsheet to the user's ~/.cheat dir if it
|
# Attempt to write the new cheatsheet to the user's ~/.cheat dir if it
|
||||||
# exists. If it does not exist, attempt to create 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):
|
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'],
|
subprocess.call([os.environ['EDITOR'],
|
||||||
os.path.join(cs.cheat_dir, cheat)])
|
os.path.join(cs.cheat_dir, cheat)])
|
||||||
|
|
||||||
exit()
|
# if the cheatsheet does already exist, open it for editing
|
||||||
|
|
||||||
|
|
||||||
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)
|
|
||||||
else:
|
else:
|
||||||
exit()
|
|
||||||
|
|
||||||
subprocess.call([os.environ['EDITOR'],
|
subprocess.call([os.environ['EDITOR'],
|
||||||
os.path.join(cheatsheets[cheat], cheat)])
|
os.path.join(cheatsheets[cheat], cheat)])
|
||||||
exit()
|
|
||||||
|
|
||||||
|
|
||||||
def help(cheatsheets):
|
def help(cheatsheets):
|
||||||
|
"Displays the program help"
|
||||||
|
|
||||||
print dedent('''
|
print dedent('''
|
||||||
Usage: cheat [OPTIONS] <keyphrase>
|
Usage: cheat [OPTIONS] <keyphrase>
|
||||||
Examples:
|
Examples:
|
||||||
|
@ -99,10 +98,7 @@ def help(cheatsheets):
|
||||||
To look up 'git commit' (a subcommand):
|
To look up 'git commit' (a subcommand):
|
||||||
cheat git commit
|
cheat git commit
|
||||||
|
|
||||||
To create a cheatsheet for 'foo':
|
To edit or edit the cheatsheet for 'foo':
|
||||||
cheat -c foo
|
|
||||||
|
|
||||||
To edit the cheatsheet for 'foo':
|
|
||||||
cheat -e foo
|
cheat -e foo
|
||||||
|
|
||||||
Available keyphrases:
|
Available keyphrases:
|
||||||
|
@ -151,41 +147,28 @@ def main():
|
||||||
# list the files in the ~/.cheat directory
|
# list the files in the ~/.cheat directory
|
||||||
cheatsheets = cheat_files(cheat_dirs)
|
cheatsheets = cheat_files(cheat_dirs)
|
||||||
|
|
||||||
# print help if requested
|
# @TODO: refactor the below into argparse
|
||||||
if keyphrase.lower() in ['', 'cheat', 'help', '-h', '-help', '--help']:
|
|
||||||
|
# print help
|
||||||
|
if keyphrase in ['', 'cheat', 'help', '-h', '-help', '--help']:
|
||||||
help(cheatsheets)
|
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()
|
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
|
# list cheat directories and exit
|
||||||
if option in ['-d', '--cheat-directories']:
|
if keyphrase in ['-d', '--cheat-directories']:
|
||||||
print("\n".join(cheat_directories()))
|
print("\n".join(cheat_directories()))
|
||||||
exit()
|
exit()
|
||||||
|
|
||||||
# list cheatsheets and exit
|
# list cheatsheets and exit
|
||||||
if option in ['-l', '--list']:
|
if keyphrase in ['-l', '--list']:
|
||||||
print list_cheatsheets(cheatsheets)
|
print list_cheatsheets(cheatsheets)
|
||||||
exit()
|
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
|
# print the cheatsheet if it exists
|
||||||
if keyphrase in cheatsheets:
|
if keyphrase in cheatsheets:
|
||||||
filename = os.path.join(cheatsheets[keyphrase], keyphrase)
|
filename = os.path.join(cheatsheets[keyphrase], keyphrase)
|
||||||
|
|
Loading…
Reference in New Issue