File size: 373 Bytes
de4a3df
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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()))