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

add color and -m monochrome option

This commit is contained in:
Kelly Brazil
2020-04-02 17:29:25 -07:00
parent 6b925a16c8
commit d0bfddc3d9

View File

@ -9,6 +9,11 @@ import importlib
import textwrap
import signal
import json
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.utils
@ -75,6 +80,28 @@ parsers = [
]
class JcStyle(Style):
BLUE = '#2c5dcd'
GREY = '#4d4d4d'
PURPLE = '#5918bb'
GREEN = '#00cc00'
styles = {
Name.Tag: 'bold {}'.format(BLUE), # key names
Keyword: GREY, # true, false, null
Number: PURPLE, # int, float
String: GREEN # string
}
def piped_output():
"""returns False if stdout is a TTY. True if output is being piped to another program"""
if sys.stdout.isatty():
return False
else:
return True
def ctrlc(signum, frame):
"""exit with error on SIGINT"""
sys.exit(1)
@ -167,6 +194,7 @@ def helptext(message):
Options:
-a about jc
-d debug - show trace messages
-m monochrome output
-p pretty print output
-q quiet - suppress warnings
-r raw JSON output
@ -181,7 +209,13 @@ def helptext(message):
print(textwrap.dedent(helptext_string), file=sys.stderr)
def json_out(data, pretty=False):
def json_out(data, pretty=False, mono=False, piped_out=False):
if not mono and not piped_out:
if pretty:
print(highlight(json.dumps(data, indent=2), JsonLexer(), Terminal256Formatter(style=JcStyle))[0:-1])
else:
print(highlight(json.dumps(data), JsonLexer(), Terminal256Formatter(style=JcStyle))[0:-1])
else:
if pretty:
print(json.dumps(data, indent=2))
else:
@ -271,12 +305,13 @@ def main():
options.extend(opt[1:])
debug = 'd' in options
mono = 'm' in options
pretty = 'p' in options
quiet = 'q' in options
raw = 'r' in options
if 'a' in options:
json_out(about_jc(), pretty=pretty)
json_out(about_jc(), pretty=pretty, mono=mono, piped_out=piped_output())
exit()
if sys.stdin.isatty():
@ -317,7 +352,7 @@ def main():
helptext('missing or incorrect arguments')
sys.exit(1)
json_out(result, pretty=pretty)
json_out(result, pretty=pretty, mono=mono, piped_out=piped_output())
if __name__ == '__main__':