Moved around some code blocks for the sake of readabilty.

This commit is contained in:
Chris Lane 2013-09-14 08:56:36 -04:00
parent 2a9220bb6d
commit 48cdbdd836

67
cheat
View File

@ -18,6 +18,7 @@ if os.name == 'posix' and 'CHEATCOLORS' in os.environ:
except ImportError: except ImportError:
pass pass
def cheat_directories(): def cheat_directories():
"Assembles a list of directories containing cheatsheets." "Assembles a list of directories containing cheatsheets."
default_directories = [DEFAULT_CHEAT_DIR] default_directories = [DEFAULT_CHEAT_DIR]
@ -47,19 +48,6 @@ def cheat_files(cheat_directories):
and not cheat.startswith('__')])) and not cheat.startswith('__')]))
return cheats 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): def create_cheatsheet(cheat, cheatsheets):
"Create a new cheatsheet." "Create a new cheatsheet."
@ -83,6 +71,41 @@ def create_cheatsheet(cheat, cheatsheets):
exit() 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(): def main():
# 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:])
@ -161,23 +184,5 @@ def main():
print ('No cheatsheet found for %s.' % keyphrase) 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__': if __name__ == '__main__':
main() main()