mirror of https://github.com/cheat/cheat.git
Merge branch 'master' of https://github.com/bradtse/cheat into bradtse-master
* 'master' of https://github.com/bradtse/cheat: Added in a create option also Added in an edit option that allows a user to edit the cheatsheet from their default editor. Also made some visual changes to the output of the cheatsheet.
This commit is contained in:
commit
cd1b43118d
48
cheat
48
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:
|
||||
|
|
Loading…
Reference in New Issue