Create utils.py
Browse files
utils.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pygments import highlight
|
| 2 |
+
from pygments.lexers import guess_lexer, PythonLexer
|
| 3 |
+
from pygments.formatters import TerminalFormatter
|
| 4 |
+
|
| 5 |
+
def pretty_print_code(code):
|
| 6 |
+
"""
|
| 7 |
+
Print code in terminal with syntax highlighting.
|
| 8 |
+
"""
|
| 9 |
+
try:
|
| 10 |
+
lexer = guess_lexer(code)
|
| 11 |
+
except:
|
| 12 |
+
lexer = PythonLexer()
|
| 13 |
+
print(highlight(code, lexer, TerminalFormatter()))
|