-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
pygments themes are customizable #8752
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
pygments themes are customizable #8752
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i like this as even tho its a workaround ,it will give people with conditions like color blindness options to work with
i believe we should have some sort of test + a UsageError on case bg/style are unknown
do you want to add such a test/error handler?
I can. What do you expect? Catching the pygments exception and raise a custom exception with a nicer error message when the environment variables have invalid values? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot @azmeuk!
Please also add the new variables to the reference.rst
file, in the "Environment Variables" section.
I added some tests and applied your suggestions. |
@@ -195,16 +195,34 @@ def _write_source(self, lines: Sequence[str], indents: Sequence[str] = ()) -> No | |||
|
|||
def _highlight(self, source: str) -> str: | |||
"""Highlight the given source code if we have markup support.""" | |||
from _pytest.config.exceptions import UsageError |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I let this import inside the function to avoid a circular import error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @azmeuk! Looks great!
Left two other suggestions, otherwise LGTM. 👍
Done :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm 👍 on configuring this as env vars... my thinking is that this is something you might want change just for yourself because of preference/color blindness issues, and this is not easily done with config vars (you can of course use command-line vars indirectly with |
I suggested an environment variable because of this comment, and because each contributor of a given project may have its own terminal configuration, and need its own color customization. I am open to add/replace a command line argument though. |
unless pytest gets someething nice to unify config by env var, config file and cli argument im of the oppinion that this particular one is best placed in a env var, accompanied with some google search firendly docs for people that need it |
It looks like the failing tests are unrelated with this patch. |
@The-Compiler do you agree with the reasoning for the environment variables? If so could you merge it then? Thanks! 🙇 |
Does this really work? I don't see a change in output when I set |
@azmeuk did you test this yourself? Can you post some screenshots please? |
I think you are right, I did not tested with other pygments themes. This piece of documentation indeed tells that |
https://github.com/pygments/pygments/blob/faf69c028f0efa7798a938163e8432d838acfda2/pygments/formatters/terminal256.py#L100 will use the color style, https://github.com/pygments/pygments/blob/faf69c028f0efa7798a938163e8432d838acfda2/pygments/formatters/terminal.py#L25-L58 will use a color scheme argument i believe its very safe to usually pick the 256 formatter, lets figure how to correctly pick |
pygments itself does a explicit choice - https://github.com/pygments/pygments/blob/faf69c028f0efa7798a938163e8432d838acfda2/pygments/cmdline.py#L390-L395 i wonder if the color sceme can be worked into the details |
What do you mean? Use the same trick as pygments to guess if a TerminalFormatter or a Terminal256Formatter should be used? |
@azmeuk no, i meant trying to figure of color schemes could be sensibly mapped to the low color terminal (or alternatively at least having color scemes for the visually imparied |
Now rereading my PR I guess it does not really have sense anymore unless we indeed use |
i'm going to be happy with formatter change i believe for full compat we should do the same check pygments does (and print a warning if a color style is passed but not supported due to lack of 256 color terminals) a followup then could be to provide a color sceme for the color-bind either by pygments directly or by addding one and upstreaming later |
We're not really in "easy and unintrusive" terrain at that point anymore, IMHO... Even pygmentize gets this wrong, checking if 256 is in Case in point, here's the pygments default output with my terminal and and here when it switches to 256 color mode: So this would really make things less readable (and uniform) for many setups, rather than more so. FWIW I still think the right solution to this is the user configuring the colors in their terminal (and sticking with the current 16-color formatter), rather than adding 256-color support just so that we can use pygment themes. Right now, the output looks like other colorized output (say, |
I have a custom set of terminal colors (solarized), every terminal utility displays well except pytest.
What do you think @The-Compiler? |
I guess that'd work indeed! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a little typo.
@RonnyPfannschmidt @hugovk Thank you for your feedbacks, they are fixed. |
@The-Compiler Is this PR acceptable to you now? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the update! Seems fine now, other than the linting issues. I added a couple of comments for the mypy ones.
@The-Compiler Here it is! |
@The-Compiler Ping! Would you consider approving this PR now? |
While test failures are unrelated, the linters in the CI are still failing. |
Ok. This should be fixed now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Looks good now, but I'm still confused about the test failures. I thought they were only something unrelated being flaky, but I can see them consistently happen in your branch, but apparently not on the main branch. However, I'm unable to reproduce locally... anyone has an idea what's going on there?
I rebased against the main branch, just in case. |
Different tests are failing each time, but the common thing is
Not sure where it comes from. |
I've been trying to reproduce with Python 3.9 in a Ubuntu 20.04 Docker container, but still no luck, unfortunately... Maybe someone else has an idea what could be going on. |
@nicoddemus @RonnyPfannschmidt do you maybe have any clue? |
I just tried -but failed- to reproduce the errors with act. |
Sorry no clue :( Have you tried rebasing on the latest |
Yep. I found that the errors are raised randomly on different other tests due to Here I am looking for unclosed file descriptors and warnings... |
If this is happening in a few tests, we can use |
❤️ ❤️ ❤️ Tests are green now, this patch should be mergeable! |
Woot! Thanks for the patience and following through with the PR! ❤️ |
@nicoddemus @RonnyPfannschmidt @The-Compiler anyone interested in pressing the merge button? |
Thanks! |
This patchs allows users to set two environment variables (PYTEST_THEME and PYTEST_THEME_MODE) to customize the pygments theme to use. It fixes #7132
I know some people were interested in rich and suggested to wait and see, but this solution is so easy and unintrusive I think it might be worth right now.
What do you think?