Replace support for user's .cheat directories

This commit is contained in:
Louis Taylor 2013-08-13 11:42:58 +01:00
parent 53b93c00e1
commit 3c9136b476

25
cheat
View File

@ -6,28 +6,31 @@ from cheatsheets import cheat_dir
# assemble a keyphrase out of all params passed to the script # assemble a keyphrase out of all params passed to the script
keyphrase = ' '.join(sys.argv[1:]) keyphrase = ' '.join(sys.argv[1:])
# verify that the cheat directory exists user_cheat_dir = os.path.join(os.path.expanduser('~'), '.cheat')
if not os.path.isdir(cheat_dir):
print >> sys.stderr, 'The cheat directory does not exist.'
exit()
# list the files in the cheat directory # list the files in the cheat directory
cheatsheets = [f for f in os.listdir(cheat_dir) if '.' not in f] cheatsheets = [(f, cheat_dir) for f in os.listdir(cheat_dir) if '.' not in f]
# 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)]
cheatsheets.sort() cheatsheets.sort()
# print help if requested # print help if requested
if keyphrase in ['', 'help', '--help', '-h']: if keyphrase in ['', 'help', '--help', '-h']:
print "Usage: cheat [keyphrase]\n" print "Usage: cheat [keyphrase]\n"
print "Available keyphrases:" print "Available keyphrases:"
print "\n".join(cheatsheets) print "\n".join([name[0] for name in cheatsheets])
exit() exit()
sheet_found = False
# print the cheatsheet if it exists # print the cheatsheet if it exists
if keyphrase in cheatsheets: for sheet in cheatsheets:
cheatsheet_filename = os.path.join(cheat_dir, keyphrase) if keyphrase == sheet[0]:
with open(cheatsheet_filename, 'r') as cheatsheet: cheatsheet_filename = os.path.join(sheet[1], keyphrase)
print cheatsheet.read() with open(cheatsheet_filename, 'r') as cheatsheet:
print cheatsheet.read()
sheet_found = True
# if it does not, say so # if it does not, say so
else: if not sheet_found:
print 'No cheatsheet found.' print 'No cheatsheet found.'