Added in a create option also

This commit is contained in:
Bradley Tse 2013-08-23 15:25:04 -04:00
parent dcf8e758a8
commit dc0b793b95
1 changed files with 41 additions and 15 deletions

56
cheat
View File

@ -46,6 +46,30 @@ def cheat_files(cheat_directories):
and not cheat.startswith('__')])) and not cheat.startswith('__')]))
return cheats return cheats
def edit_cheatsheet(cheat, cheatsheets):
"Edit 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:
exit()
subprocess.call([os.environ['EDITOR'],
os.path.join(cheatsheets[cheat], cheat)])
exit()
def create_cheatsheet(cheat, cheatsheets):
"Create a new cheatsheet."
if cheat in cheatsheets:
print 'Cheatsheet "%s" already exists' % cheat
exit()
import cheatsheets as cs
subprocess.call([os.environ['EDITOR'],
os.path.join(cs.cheat_dir, cheat)])
exit()
def main(): def main():
# assemble a keyphrase out of all params passed to the script # assemble a keyphrase out of all params passed to the script
@ -70,24 +94,26 @@ def main():
for key, value in cheatsheets.items()])) for key, value in cheatsheets.items()]))
exit() exit()
# if the user wants to edit a cheatsheet # create/edit option
if sys.argv[1].lower() in ['-e', '--edit']: option = sys.argv[1].lower()
if len(sys.argv) < 2: if option in ['-e', '--edit', '-c', '--create']:
print 'Must provide a cheatsheet to edit' # make sure EDITOR environment variable is set and that at least 3 arguments
exit() # are given
cheatsheet = ' '.join(sys.argv[2:])
if cheatsheet not in cheatsheets:
print 'No cheatsheet found for %s.' % cheatsheet
exit()
if 'EDITOR' not in os.environ: if 'EDITOR' not in os.environ:
print 'Must set your EDITOR environment variable to default editor path' 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()
subprocess.call([os.environ['EDITOR'], # if the user wants to edit a cheatsheet
os.path.join(cheatsheets[cheatsheet], 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)
# print the cheatsheet if it exists # print the cheatsheet if it exists
if keyphrase in cheatsheets: if keyphrase in cheatsheets: