1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-19 00:17:51 +02:00

make pygments library optional

This commit is contained in:
Kelly Brazil
2021-03-26 15:54:45 -07:00
parent 5f00973e40
commit 3638298af8

View File

@ -11,14 +11,19 @@ import importlib
import textwrap import textwrap
import signal import signal
import json import json
import pygments
from pygments import highlight
from pygments.style import Style
from pygments.token import (Name, Number, String, Keyword)
from pygments.lexers import JsonLexer
from pygments.formatters import Terminal256Formatter
import jc import jc
import jc.appdirs as appdirs import jc.appdirs as appdirs
# make pygments import optional
try:
import pygments
from pygments import highlight
from pygments.style import Style
from pygments.token import (Name, Number, String, Keyword)
from pygments.lexers import JsonLexer
from pygments.formatters import Terminal256Formatter
pygments_installed = True
except Exception:
pygments_installed = False
class info(): class info():
@ -115,7 +120,8 @@ if os.path.isdir(local_parsers_dir):
# We only support 2.3.0+, pygments changed color names in 2.4.0. # We only support 2.3.0+, pygments changed color names in 2.4.0.
# startswith is sufficient and avoids potential exceptions from split and int. # startswith is sufficient and avoids potential exceptions from split and int.
if pygments.__version__.startswith('2.3.'): if pygments_installed:
if pygments.__version__.startswith('2.3.'):
PYGMENT_COLOR = { PYGMENT_COLOR = {
'black': '#ansiblack', 'black': '#ansiblack',
'red': '#ansidarkred', 'red': '#ansidarkred',
@ -134,7 +140,7 @@ if pygments.__version__.startswith('2.3.'):
'brightcyan': '#ansiturquoise', 'brightcyan': '#ansiturquoise',
'white': '#ansiwhite', 'white': '#ansiwhite',
} }
else: else:
PYGMENT_COLOR = { PYGMENT_COLOR = {
'black': 'ansiblack', 'black': 'ansiblack',
'red': 'ansired', 'red': 'ansired',
@ -438,6 +444,9 @@ def main():
quiet = 'q' in options quiet = 'q' in options
raw = 'r' in options raw = 'r' in options
if not pygments_installed:
mono = True
if help_me: if help_me:
print(helptext()) print(helptext())
sys.exit(0) sys.exit(0)