mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-07-13 01:20:24 +02:00
simplify json_out function
This commit is contained in:
20
jc/cli.py
20
jc/cli.py
@ -381,22 +381,22 @@ def versiontext():
|
|||||||
def json_out(data, pretty=False, env_colors=None, mono=False, piped_out=False):
|
def json_out(data, pretty=False, env_colors=None, mono=False, piped_out=False):
|
||||||
"""Return a JSON formatted string. String may include color codes or be pretty printed."""
|
"""Return a JSON formatted string. String may include color codes or be pretty printed."""
|
||||||
separators = (',', ':')
|
separators = (',', ':')
|
||||||
|
indent = None
|
||||||
|
|
||||||
|
if pretty:
|
||||||
|
separators = None
|
||||||
|
indent = 2
|
||||||
|
|
||||||
if not mono and not piped_out:
|
if not mono and not piped_out:
|
||||||
# set colors
|
# set colors
|
||||||
class JcStyle(Style):
|
class JcStyle(Style):
|
||||||
styles = set_env_colors(env_colors)
|
styles = set_env_colors(env_colors)
|
||||||
|
|
||||||
if pretty:
|
return str(highlight(json.dumps(data, indent=indent, separators=separators, ensure_ascii=False),
|
||||||
return str(highlight(json.dumps(data, indent=2, ensure_ascii=False),
|
JsonLexer(), Terminal256Formatter(style=JcStyle))[0:-1])
|
||||||
JsonLexer(), Terminal256Formatter(style=JcStyle))[0:-1])
|
|
||||||
else:
|
|
||||||
return str(highlight(json.dumps(data, separators=separators, ensure_ascii=False),
|
|
||||||
JsonLexer(), Terminal256Formatter(style=JcStyle))[0:-1])
|
|
||||||
else:
|
else:
|
||||||
if pretty:
|
return json.dumps(data, indent=indent, separators=separators, ensure_ascii=False)
|
||||||
return json.dumps(data, indent=2, ensure_ascii=False)
|
|
||||||
else:
|
|
||||||
return json.dumps(data, separators=separators, ensure_ascii=False)
|
|
||||||
|
|
||||||
|
|
||||||
def magic_parser(args):
|
def magic_parser(args):
|
||||||
|
Reference in New Issue
Block a user