| from pygments import highlight | |
| from pygments.lexers import guess_lexer, PythonLexer | |
| from pygments.formatters import TerminalFormatter | |
| def pretty_print_code(code): | |
| """ | |
| Print code in terminal with syntax highlighting. | |
| """ | |
| try: | |
| lexer = guess_lexer(code) | |
| except: | |
| lexer = PythonLexer() | |
| print(highlight(code, lexer, TerminalFormatter())) | |