From dc0b793b95d903a4c2edde7804f7c97a00b06464 Mon Sep 17 00:00:00 2001 From: Bradley Tse Date: Fri, 23 Aug 2013 15:25:04 -0400 Subject: [PATCH] Added in a create option also --- cheat | 56 +++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 41 insertions(+), 15 deletions(-) diff --git a/cheat b/cheat index 7d1ce81..ede3e80 100755 --- a/cheat +++ b/cheat @@ -46,6 +46,30 @@ def cheat_files(cheat_directories): and not cheat.startswith('__')])) 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(): # assemble a keyphrase out of all params passed to the script @@ -70,24 +94,26 @@ def main(): for key, value in cheatsheets.items()])) exit() - # if the user wants to edit a cheatsheet - if sys.argv[1].lower() in ['-e', '--edit']: - if len(sys.argv) < 2: - print 'Must provide a cheatsheet to edit' - exit() - - cheatsheet = ' '.join(sys.argv[2:]) - - if cheatsheet not in cheatsheets: - print 'No cheatsheet found for %s.' % cheatsheet - exit() - + # 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 '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() - subprocess.call([os.environ['EDITOR'], - os.path.join(cheatsheets[cheatsheet], cheatsheet)]) + # 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) # print the cheatsheet if it exists if keyphrase in cheatsheets: