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:
Chris Lane 2013-09-14 09:58:33 -04:00
parent 34f3eafb1a
commit ba4093620a
1 changed files with 44 additions and 61 deletions

105
cheat
View File

@ -50,45 +50,44 @@ 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)
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):
subprocess.call([os.environ['EDITOR'],
os.path.join(DEFAULT_CHEAT_DIR, cheat)])
# If the directory cannot be created, write to the python package
# directory, though that will likely require the use of sudo
else:
subprocess.call([os.environ['EDITOR'],
os.path.join(cs.cheat_dir, cheat)])
exit()
def edit_cheatsheet(cheat, cheatsheets):
"Edits a pre-existing cheatsheet."
"Creates or edits a cheatsheet"
# if the cheatsheet does not already exist, create it
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:
# 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()
subprocess.call([os.environ['EDITOR'],
os.path.join(cheatsheets[cheat], cheat)])
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):
subprocess.call([os.environ['EDITOR'],
os.path.join(DEFAULT_CHEAT_DIR, cheat)])
# If the directory cannot be created, write to the python package
# directory, though that will likely require the use of sudo
else:
subprocess.call([os.environ['EDITOR'],
os.path.join(cs.cheat_dir, cheat)])
# if the cheatsheet does already exist, open it for editing
else:
subprocess.call([os.environ['EDITOR'],
os.path.join(cheatsheets[cheat], cheat)])
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)
exit()
# 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)