Skip to content
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

colorization in cli json tool #131952

Open
raztd opened this issue Mar 31, 2025 · 8 comments
Open

colorization in cli json tool #131952

raztd opened this issue Mar 31, 2025 · 8 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@raztd
Copy link

raztd commented Mar 31, 2025

Feature or enhancement

Proposal:

apologies if this is not the right place to ask for enhancement requests. since python3.13 added support for colorization in repl, i was wondering if some basic colorization could be added for the python's cli json tool? jq[0] does this, the keys are bolded and have a different color from the values.

if this gets implemented i think a lot of people will be happy to just use python and not have to install yet another library to fulfill their needs :-)

[0] https://static1.howtogeekimages.com/wordpress/wp-content/uploads/2020/01/5-7.png

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Linked PRs

@raztd raztd added the type-feature A feature request or enhancement label Mar 31, 2025
@picnixz picnixz added the stdlib Python modules in the Lib dir label Mar 31, 2025
@ZeroIntensity
Copy link
Member

cc @hugovk

@sobolevn
Copy link
Member

sobolevn commented Apr 1, 2025

I think that adding colors by default can possibly break piping on some shells / systems. But, I am not 100% sure.

Like get_json | python -m json.tool | expects_json_text

@hugovk
Copy link
Member

hugovk commented Apr 1, 2025

By default, the _colorize module does check if we're in a tty before enabling colour:

cpython/Lib/_colorize.py

Lines 109 to 112 in 3b3720f

try:
return os.isatty(file.fileno())
except io.UnsupportedOperation:
return hasattr(file, "isatty") and file.isatty()

Compare the output of python3.14 -m calendar and python3.14 -m calendar | cat

@ZeroIntensity
Copy link
Member

Compare the output of python3.14 -m calendar

Speaking of which, is that extra highlighted space before the 1 supposed to be there?

Image

The assymetry looks a little funky to me.

@Eclips4
Copy link
Member

Eclips4 commented Apr 1, 2025

Speaking of which, is that extra highlighted space before the 1 supposed to be there?

Image

The assymetry looks a little funky to me.

I guess so. At least so-called cal does the same:

Image

@srinivasreddy
Copy link
Contributor

srinivasreddy commented Apr 2, 2025

asymmetry happens on MacOS too.

Image

@hugovk
Copy link
Member

hugovk commented Apr 2, 2025

The highlight includes the unprinted 0 prefix.

@nineteendo
Copy link
Contributor

Like this?

Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
Status: No status
Development

No branches or pull requests

8 participants