-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
gh-131952: Add color to the json
CLI
#132126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
cae293f
1854ae5
ee39cb2
5e726ee
848a7be
8d90ccb
93e4306
f8d697a
429e350
0abe76a
1acd35d
691aecd
5afac97
911f75f
4c27be7
18ce6fa
18f7ae5
b5157af
d7287e2
177107b
b7589be
e744290
fac39c7
4f399be
8bd0a36
37d4c08
1e6f4ea
cc92518
4fc5e27
a489649
789ca88
ce75f86
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,9 @@ | |
""" | ||
import argparse | ||
import json | ||
import re | ||
AA-Turner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import sys | ||
from _colorize import ANSIColors, can_colorize | ||
|
||
|
||
def main(): | ||
|
@@ -48,6 +50,8 @@ def main(): | |
dump_args['indent'] = None | ||
dump_args['separators'] = ',', ':' | ||
|
||
with_colors = can_colorize() | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This variable isn't doing much, shall we just call the function directly? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I may have been too cautious here. Without the variable, the function would be called in a loop and I was worried the result could change in between invocations, though that is really unlikely. If you prefer I can remove it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, I applied your other suggestion which already removed it so never mind 😄 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, that's a good point: there could be a lot of |
||
try: | ||
if options.infile == '-': | ||
infile = sys.stdin | ||
|
@@ -68,12 +72,42 @@ def main(): | |
outfile = open(options.outfile, 'w', encoding='utf-8') | ||
with outfile: | ||
for obj in objs: | ||
json.dump(obj, outfile, **dump_args) | ||
if with_colors: | ||
tomasr8 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
json_str = json.dumps(obj, **dump_args) | ||
outfile.write(colorize_json(json_str)) | ||
else: | ||
json.dump(obj, outfile, **dump_args) | ||
outfile.write('\n') | ||
except ValueError as e: | ||
raise SystemExit(e) | ||
|
||
|
||
color_pattern = re.compile(r''' | ||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(?P<string>"(\\.|[^"\\])*?") | # String | ||
(?P<number>[\d\-+.Ee]+) | # Number | ||
tomasr8 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(?P<boolean>true|false) | # Boolean | ||
(?P<null>null) # Null | ||
''', re.VERBOSE) | ||
|
||
|
||
def colorize_json(json_str): | ||
colors = { | ||
'string': ANSIColors.GREEN, | ||
'number': ANSIColors.YELLOW, | ||
'boolean': ANSIColors.CYAN, | ||
'null': ANSIColors.CYAN, | ||
tomasr8 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
def replace(match): | ||
for key in colors: | ||
if match.group(key): | ||
color = colors[key] | ||
return f"{color}{match.group(key)}{ANSIColors.RESET}" | ||
tomasr8 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return match.group() | ||
|
||
return re.sub(color_pattern, replace, json_str) | ||
|
||
|
||
if __name__ == '__main__': | ||
try: | ||
main() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add color output to the :program:`json.tool` CLI. | ||
tomasr8 marked this conversation as resolved.
Show resolved
Hide resolved
|
Uh oh!
There was an error while loading. Please reload this page.