From 48cdbdd836c5db0359ea1fa1a6f54b7ac7356531 Mon Sep 17 00:00:00 2001 From: Chris Lane Date: Sat, 14 Sep 2013 08:56:36 -0400 Subject: [PATCH] Moved around some code blocks for the sake of readabilty. --- cheat | 67 ++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 36 insertions(+), 31 deletions(-) diff --git a/cheat b/cheat index 6d1bddc..cbebbca 100755 --- a/cheat +++ b/cheat @@ -18,6 +18,7 @@ if os.name == 'posix' and 'CHEATCOLORS' in os.environ: except ImportError: pass + def cheat_directories(): "Assembles a list of directories containing cheatsheets." default_directories = [DEFAULT_CHEAT_DIR] @@ -47,19 +48,6 @@ 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." @@ -83,6 +71,41 @@ def create_cheatsheet(cheat, cheatsheets): exit() + +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 pretty_print(filename): + "Applies syntax highlighting to a cheatsheet and writes it to stdout" + 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) + + def main(): # assemble a keyphrase out of all params passed to the script keyphrase = ' '.join(sys.argv[1:]) @@ -161,23 +184,5 @@ def main(): 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()