gh-131952: Add color to the `json` CLI by tomasr8 · Pull Request #132126 · python/cpython
Example:
I'm open to changing the colors. For now it's green for strings, yellow for numbers and cyan for true/false/null.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How much of a performance impact does this have?
How much of a performance impact does this have?
It doesn't matter here. json.tool is more of a visualization tool. If users don't want to colorize the output they can also disable it using the NO_COLOR env. var IIRC. OTOH, we could add an explicit --pretty flag to enable colorization instead of making the default.
OTOH, we could add an explicit --pretty flag to enable colorization instead of making the default.
I think we might not need that, can_colorize can already detect most situations where color is not desirable (e.g. not a TTY). You can also set NO_COLOR to turn off colors unconditionally.
AA-Turner
changed the title
gh-131952: Add color to the
gh-131952: Add color to the json.tool CLI.json.tool CLI
What about CLIs? they would be broken if data is piped?
AFAIU they wouldn't (and local testing confirms that), see this comment from Hugo: #131952 (comment)
@tomasr8 thank you for taking the time to look into it. couple of notes regarding the keys 1) could you make them a different color from the string values? 2) could you make them bold?
this is what jq does and i think it provides a good ux https://static1.howtogeekimages.com/wordpress/wp-content/uploads/2020/01/5-7.png
That should be in theory possible, we can distinguish keys from string literals since keys are always followed by :. I will make a couple different color versions and then we can decide which one looks the best.
Comment on lines +53 to +54
| with_colors = can_colorize() | ||
|
|
Choose a reason for hiding this comment
The 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?
Choose a reason for hiding this comment
The 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?
Choose a reason for hiding this comment
The 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 😄
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, that's a good point: there could be a lot of objs as JSONL files can be quite long. Let's add with_colors back, but just outside the loop.
The green and cyan colours here look bad on the white background, which is the default for Windows Terminal, PyCharm's in-built terminal, etc. Could you darken them?
Thanks for your feedback! I think it looks much better now :) I added the bold colors and updated tests. Would you mind taking another look?
could you bold the keys and leave the values regular?
I think I prefer the bold colors. In my opinion, they are more legible when bold, especially with the light theme.
could you bold the keys and leave the values regular?
I think I prefer the bold colors. In my opinion, they are more legible when bold, especially with the light theme.
in the last pair of screenshots, i see only the values as bold. the keys look regular. my proposal was to invert those. or do you want to make the keys bold, too? i think that could work
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please could you also add this to What's New, and mention the colour env vars, like the other similar entries?
in the last pair of screenshots, i see only the values as bold. the keys look regular. my proposal was to invert those. or do you want to make the keys bold, too? i think that could work
I find the color contrast for the intense blue variant better compared to just bold blue:
One suggestion, jq uses default colour/uncoloured for numbers:
Which is more readable with light background than yellow?
Looks good:
Please could you also add this to What's New, and mention the colour env vars, like the other similar entries?
Added - I reused your entry from the calendar module ;)
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! 🎨








