diff --git a/cheat b/cheat index af4e153..2f5062d 100755 --- a/cheat +++ b/cheat @@ -2,6 +2,11 @@ import os import sys +from pygments import highlight +from pygments.util import ClassNotFound +from pygments.lexers import get_lexer_for_filename, TextLexer +from pygments.formatters import TerminalFormatter + def cheat_directories(): "Assembles a list of directories containing cheatsheets." @@ -58,13 +63,30 @@ def main(): # print the cheatsheet if it exists if keyphrase in cheatsheets: filename = os.path.join(cheatsheets[keyphrase], keyphrase) - with open(filename, 'r') as cheatsheet: - print cheatsheet.read() + pretty_print(filename) # if it does not, say so else: print 'No cheatsheet found for %s.' % keyphrase +def pretty_print(filename): + try: + if os.path.splitext(filename)[1]: + lexer = get_lexer_for_filename(filename) + else: + # shell is a sensible default when there is no extension + lexer = get_lexer_for_filename(filename + '.sh') + + except ClassNotFound: + lexer = TextLexer() + + with open(filename) as istream: + code = istream.read() + + fmt = TerminalFormatter() + highlight(code, lexer, fmt, sys.stdout) + + if __name__ == '__main__': main() diff --git a/setup.py b/setup.py index af240f9..4b10199 100644 --- a/setup.py +++ b/setup.py @@ -5,12 +5,13 @@ import os setup(name='cheat', version='1.0', - description='Create and view interactive cheatsheets on the command-line', + description='Create and view interactive cheatsheets on the command-line', # nopep8 author='Chris Lane', author_email='chris@chris-allen-lane.com', url='https://github.com/chrisallenlane/cheat', packages=['cheatsheets'], package_data={'cheatsheets': [f for f in os.listdir('cheatsheets') if '.' not in f]}, + install_requires=['Pygments>=1.6'], scripts=['cheat'] )