cheat-fork-echo/cheat
2013-08-16 03:02:33 +01:00

44 lines
1.3 KiB
Python
Executable file

#!/usr/bin/env python
import os
import sys
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 = []
# construct the path to the cheat directory
user_cheat_dir = os.path.join(os.path.expanduser('~'), '.cheat')
# list the files in the cheat directory
# add the user's cheat files if they have a ~/.cheat directory
if os.path.isdir(user_cheat_dir):
cheatsheets += [(f, user_cheat_dir) for f in os.listdir(user_cheat_dir)
if '.' not in f]
cheatsheets.sort()
# assemble a keyphrase out of all params passed to the script
keyphrase = ' '.join(sys.argv[1:])
# print help if requested
if keyphrase in ['', 'help', '--help', '-h']:
print "Usage: cheat [keyphrase]\n"
print "Available keyphrases:"
print "\n".join([name[0] for name in cheatsheets])
exit()
sheet_found = False
# print the cheatsheet if it exists
for sheet in cheatsheets:
if keyphrase == sheet[0]:
cheatsheet_filename = os.path.join(sheet[1], keyphrase)
with open(cheatsheet_filename, 'r') as cheatsheet:
print cheatsheet.read()
sheet_found = True
# if it does not, say so
if not sheet_found:
print 'No cheatsheet found.'