mirror of https://github.com/cheat/cheat.git
Added in a create option also
This commit is contained in:
parent
dcf8e758a8
commit
dc0b793b95
56
cheat
56
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:
|
||||
|
|
Loading…
Reference in New Issue