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

Set coloured output with a context manager #12134

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

chrisjsewell
Copy link
Member

@chrisjsewell chrisjsewell commented Mar 19, 2024

Rather than having singular nocolor and coloron functions (left for back-compatibility, introduce a colorize_output context manager, for temporarily enabling / disabling colorized output streams, on entering, then return to the initial state on exit.

Two outstandung questions:

  1. double-check working of colorama init/deinit (see https://github.com/tartley/colorama/blob/master/colorama/initialise.py#L23)
  2. currently make_app is now set to always have color deactivated (which helps for checking output strings), but maybe this should be configurable?

Comment on lines +94 to +98
if on:
coloron()
else:
nocolor()
yield
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the docstring: should there be toggle actions both before and after the yield statement?

Suggested change
if on:
coloron()
else:
nocolor()
yield
(coloron if on else nocolor)()
yield
(nocolor if on else coloron)()

...or something neater (example for illustration purposes, not necessarily good code).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe toggle is the wrong word then; it should just turn on/off colorization, on entering the context, then return it to the initial state on exiting the context

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little wary of thread-safety / potential timing issues related to the codes object, but thanks for resolving my concern about the content of this function. The code as you have it is OK with me 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I'm not a fan of any mutable global variables, I wish there wasn't so many in docutils/sphinx 😅

@chrisjsewell chrisjsewell changed the title 👌 Toggle colorized output with a context manager 👌 Set colorized output with a context manager Mar 19, 2024
Copy link
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hadn't looked at it yet but you might need to fix the ColorizeFormatter of the logging module as well. That was what I wanted to fix yesterday so that the environment variable is handled properly in loggers.

@picnixz
Copy link
Member

picnixz commented Mar 19, 2024

Didn't see your questions, sorry.

currently make_app is now set to always have color deactivated (which helps for checking output strings), but maybe this should be configurable?

This can be configured I think by changing the environment variable by using NO_COLOR or FORCE_COLOR. However, as I've said in #12089, you need to patch the logger formatter so that it knows whether to add colors or not.

@chrisjsewell
Copy link
Member Author

chrisjsewell commented Mar 19, 2024

Hadn't looked at it yet but you might need to fix the ColorizeFormatter of the logging module as well

this looks ok to me, basically it ends up calling:

escape = codes.get(name, '')

and codes will be empty if nocolor is set

@chrisjsewell
Copy link
Member Author

This can be configured I think by changing the environment variable by using NO_COLOR or FORCE_COLOR

I don't believe this to be the case for testing; these variables are only evaluated in sphinx.util.console.color_terminal, which is called as part of build_main (i.e. for the CLI), but nowhere in the the testing fixtures

@jayaddison jayaddison added api:cmdline type:proposal a feature suggestion python Pull requests that update Python code labels Mar 22, 2024
@AA-Turner AA-Turner changed the title 👌 Set colorized output with a context manager Set coloured output with a context manager Apr 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api:cmdline internals:refactoring python Pull requests that update Python code type:proposal a feature suggestion
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants