diff --git a/cheat b/cheat index 8b86169..4b46332 100755 --- a/cheat +++ b/cheat @@ -1,6 +1,7 @@ #!/usr/bin/env python import os import sys +import subprocess DEFAULT_CHEAT_DIR = os.path.join(os.path.expanduser('~'), '.cheat') USE_PYGMENTS = False @@ -45,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 @@ -69,8 +94,30 @@ def main(): for key, value in cheatsheets.items()]))) 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('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) + # print the cheatsheet if it exists if keyphrase in cheatsheets: + print ''.join('*' for x in range(80)) + "\n" filename = os.path.join(cheatsheets[keyphrase], keyphrase) if USE_PYGMENTS: pretty_print(filename) @@ -78,6 +125,7 @@ def main(): with open(filename) as istream: for l in istream: sys.stdout.write(l) + print "\n" + ''.join('*' for x in range(80)) # if it does not, say so else: