Skip to content

Commit 0ba7a39

Browse files
committed
Fix lukas-blecher#184, add highlight
1 parent 30c519d commit 0ba7a39

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

pix2tex/cli.py

+23-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from PIL import ImageGrab
44
from PIL import Image
55
import os
6+
import sys
67
from typing import Tuple
78
import logging
89
import yaml
@@ -133,7 +134,28 @@ def __call__(self, img=None, resize=True) -> str:
133134

134135

135136
def output_prediction(pred, args):
136-
print(pred, '\n')
137+
TERM = os.getenv('TERM', 'xterm')
138+
if not sys.stdout.isatty():
139+
TERM = 'dumb'
140+
try:
141+
from pygments import highlight
142+
from pygments.lexers import get_lexer_by_name
143+
from pygments.formatters import get_formatter_by_name
144+
145+
if TERM.split('-')[-1] == '256color':
146+
formatter_name = 'terminal256'
147+
elif TERM != 'dumb':
148+
formatter_name = 'terminal'
149+
else:
150+
formatter_name = None
151+
if formatter_name:
152+
formatter = get_formatter_by_name(formatter_name)
153+
lexer = get_lexer_by_name('tex')
154+
print(highlight(pred, lexer, formatter), end='')
155+
except ImportError:
156+
TERM = 'dumb'
157+
if TERM == 'dumb':
158+
print(pred)
137159
if args.show or args.katex:
138160
try:
139161
if args.katex:

setup.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
'torchtext>=0.6.0',
2525
'imagesize>=1.2.0',
2626
]
27+
highlight = ['pygments']
2728

2829
setuptools.setup(
2930
name='pix2tex',
@@ -66,10 +67,11 @@
6667
'albumentations>=0.5.2',
6768
],
6869
extras_require={
69-
'all': gui+api+train,
70+
'all': gui+api+train+highlight,
7071
'gui': gui,
7172
'api': api,
72-
'train': train
73+
'train': train,
74+
'highlight': highlight,
7375
},
7476
entry_points={
7577
'console_scripts': [

0 commit comments

Comments
 (0)