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

Add text color capability for Windows #612

Merged
merged 7 commits into from
Feb 2, 2021
Merged

Conversation

itsayellow
Copy link
Contributor

  • I have added an entry to docs/changelog.md

Summary of changes

With colorama it is ridiculously easy to support text color output on Windows, so I added it with this PR. It adds polish and brings the Windows experience in line with other platforms.

Test plan

Tested by running

pipx list --verbose

On Windows both the bold list formatting and the green of the verbose output show up. (They don't with existing pipx.)

@uranusjr
Copy link
Member

colorama also handles tty detection so init() can be called unconditionally. But I guess there's no harm to check either.

@itsayellow
Copy link
Contributor Author

There's also this: https://stackoverflow.com/questions/12492810/python-how-can-i-make-the-ansi-escape-codes-to-work-also-in-windows/60194390#60194390
Which seems to indicate with a few lines of code using ctypes we don't need colorama. It seems great but I wonder if colorama is providing us with something else valuable?

@itsayellow
Copy link
Contributor Author

To follow up on my last comment, this https://stackoverflow.com/a/36760881 gives some more information. It seems that it's only turning on Windows' native ANSI-interpreter.

So the simple non-colorama code might not help people whose Windows doesn't have ANSI capabilities.

@itsayellow
Copy link
Contributor Author

colorama also handles tty detection so init() can be called unconditionally. But I guess there's no harm to check either.

That's a good point. I think I will leave it in. If I read colorama's docs right, it's still trying to filter out all the color codes in a non-tty environment. So since we don't add the codes in the first place in the that case, I like not having to filter sys.stdout and sys.stderr at all.

@CarliJoy
Copy link

Current Result on Windows:

2021-01-27_14h51_03

@itsayellow
Copy link
Contributor Author

Thanks for the independent test, @CarliJoy , that seems to work as expected, the codes still visible are fixed in the other PR.

@itsayellow
Copy link
Contributor Author

colorama also handles tty detection so init() can be called unconditionally. But I guess there's no harm to check either.

Another possible option would just be to use colorama for everything--so the "color functions" would always add the color escape codes, and we rely on colorama to remove them if we're not in a tty.

I didn't go this route just out of conservatism (original code works) and because I have no idea how much overhead this would add.

@itsayellow itsayellow merged commit 655403f into pypa:master Feb 2, 2021
@itsayellow itsayellow deleted the win-color branch February 2, 2021 05:38
itsayellow added a commit to itsayellow/pipx that referenced this pull request Feb 16, 2021
Add app_name "pipx" to ensurepath comments, don't duplicate bin_paths.

Apply typing.NoReturn to code base (pypa#605)

Fix post-release development versioning (pypa#601)

Make constants.py single source for WINDOWS boolean. (pypa#607)

Add section on where pipx keeps its logs. (pypa#608)

Upgrade all pre-commit plugins except black. (pypa#611)

Fix show/hide cursor for older Windows (pypa#610)

Drop .exe appending on Windows (pypa#606)

This makes Venv.run_app() and code path along it accept an additional
argument, so we can distinguish between the "real" name (filename) of an
app, and the name supplied by the user.

This makes the code simpler, espacially when dealing with the proposed
[pipx.run] entry points.

Add text color capability for Windows (pypa#612)

Better Unicode / emoji capability detection (pypa#614)

Use python 3.9 as default python for CI. (pypa#622)

Don't show / hide cursor if not in tty. (pypa#620)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants