2013-07-31 04:48:07 +02:00
|
|
|
#!/usr/bin/env python
|
2013-08-11 21:37:11 +02:00
|
|
|
import os
|
2013-07-31 04:48:07 +02:00
|
|
|
import sys
|
|
|
|
|
2013-08-16 04:02:33 +02:00
|
|
|
try:
|
|
|
|
# check to see if the cheat package is available
|
|
|
|
import cheatsheets
|
|
|
|
cheat_dir = cheatsheets.cheat_dir
|
|
|
|
cheatsheets = [(f, cheat_dir) for f in os.listdir(cheat_dir) if '.' not in f]
|
|
|
|
except ImportError:
|
|
|
|
cheatsheets = []
|
2013-07-31 05:25:36 +02:00
|
|
|
|
2013-08-16 04:02:33 +02:00
|
|
|
# construct the path to the cheat directory
|
2013-08-13 12:42:58 +02:00
|
|
|
user_cheat_dir = os.path.join(os.path.expanduser('~'), '.cheat')
|
2013-08-11 21:37:11 +02:00
|
|
|
|
2013-08-13 12:01:06 +02:00
|
|
|
# list the files in the cheat directory
|
2013-08-13 12:42:58 +02:00
|
|
|
# add the user's cheat files if they have a ~/.cheat directory
|
|
|
|
if os.path.isdir(user_cheat_dir):
|
2013-08-16 04:02:33 +02:00
|
|
|
cheatsheets += [(f, user_cheat_dir) for f in os.listdir(user_cheat_dir)
|
|
|
|
if '.' not in f]
|
2013-08-17 03:38:31 +02:00
|
|
|
|
|
|
|
# add the cheat files from the directory specified in $CHEATPATH
|
|
|
|
if 'CHEATPATH' in os.environ and os.environ['CHEATPATH']:
|
|
|
|
path = os.environ['CHEATPATH'].split(os.pathsep)
|
|
|
|
if os.path.isdir(path):
|
|
|
|
cheatsheets += [(f, path) for f in os.listdir(path) if '.' not in f]
|
2013-08-11 21:37:11 +02:00
|
|
|
cheatsheets.sort()
|
2013-08-10 05:46:34 +02:00
|
|
|
|
2013-07-31 05:25:36 +02:00
|
|
|
# assemble a keyphrase out of all params passed to the script
|
2013-08-16 00:03:58 +02:00
|
|
|
keyphrase = ' '.join(sys.argv[1:])
|
2013-07-31 05:25:36 +02:00
|
|
|
|
2013-08-13 03:59:33 +02:00
|
|
|
# verify that we have at least one cheat directory
|
2013-08-17 03:38:31 +02:00
|
|
|
if not cheatsheets:
|
2013-08-16 00:03:58 +02:00
|
|
|
print >> sys.stderr, 'The ~/.cheat directory does not exist or the CHEATPATH variable is not set.'
|
|
|
|
exit()
|
2013-08-11 21:37:11 +02:00
|
|
|
|
2013-08-16 04:02:33 +02:00
|
|
|
# assemble a keyphrase out of all params passed to the script
|
|
|
|
keyphrase = ' '.join(sys.argv[1:])
|
|
|
|
|
2013-07-31 05:33:31 +02:00
|
|
|
# print help if requested
|
2013-08-16 00:19:02 +02:00
|
|
|
if keyphrase.lower() in ['', 'cheat', 'help', '-h', '-help', '--help']:
|
2013-08-16 10:47:43 +02:00
|
|
|
print "Usage: cheat [keyphrase]\n"
|
|
|
|
print "Available keyphrases:"
|
2013-08-17 03:38:31 +02:00
|
|
|
max_command = max([len(sheet[0]) for sheet in cheatsheets]) + 3
|
|
|
|
print '\n'.join(sorted([ '%s [%s]' % (sheet[0].ljust(max_command), sheet[1]) for sheet in cheatsheets]))
|
2013-08-16 10:47:43 +02:00
|
|
|
exit()
|
2013-07-31 05:25:36 +02:00
|
|
|
|
2013-08-13 12:42:58 +02:00
|
|
|
sheet_found = False
|
2013-07-31 05:33:31 +02:00
|
|
|
# print the cheatsheet if it exists
|
2013-08-13 12:42:58 +02:00
|
|
|
for sheet in cheatsheets:
|
2013-08-16 10:47:43 +02:00
|
|
|
if keyphrase == sheet[0]:
|
2013-08-17 03:38:31 +02:00
|
|
|
cheatsheet_filename = os.path.join(sheet[1], keyphrase)
|
|
|
|
with open(cheatsheet_filename, 'r') as cheatsheet:
|
|
|
|
print cheatsheet.read()
|
|
|
|
sheet_found = True
|
2013-08-11 21:37:11 +02:00
|
|
|
|
|
|
|
# if it does not, say so
|
|
|
|
else:
|
2013-08-16 00:03:58 +02:00
|
|
|
print 'No cheatsheet found for %s.' % keyphrase
|