Skip to content

Set coloured output with a context manager - #12134

Draft
chrisjsewell wants to merge 1 commit into
sphinx-doc:masterfrom
chrisjsewell:console-color-contextmanager
Draft

chrisjsewell wants to merge 1 commit into
sphinx-doc:masterfrom
chrisjsewell:console-color-contextmanager

Conversation

@chrisjsewell

@chrisjsewell chrisjsewell commented Mar 19, 2024 •

Copy link
Copy Markdown
Member

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 thread sphinx/util/console.py
Comment on lines +94 to +98
if on:
coloron()
else:
nocolor()
yield

Copy link
Copy Markdown
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).

@chrisjsewell chrisjsewell Mar 19, 2024 •

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

@picnixz picnixz left a comment

Copy link
Copy Markdown
Member

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

picnixz commented Mar 19, 2024

Copy link
Copy Markdown
Member

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

chrisjsewell commented Mar 19, 2024 •

Copy link
Copy Markdown
Member Author

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
Copy Markdown
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
@AA-Turner

Copy link
Copy Markdown
Member

I don't think this is needed now that we don't wrap stdout/stderr on Windows (#12493).

A

@AA-Turner AA-Turner closed this Oct 5, 2024
@chrisjsewell chrisjsewell reopened this Oct 5, 2024
@chrisjsewell

chrisjsewell commented Oct 5, 2024 •

Copy link
Copy Markdown
Member Author

I don't think this is needed now that we don't wrap stdout/stderr on Windows

@AA-Turner I would perhaps disagree, the point is that colorization should not be just globally set to "on forever" whenever sphinx is run,
it should only be on for the duration of that run.

More specifically, most unit tests, for sphinx core and for sphinx extensions, should not have colorisation enabled,
because it makes testing output logging/warnings unnecessarily complicated since you have to strip out all the colorisation.

#12493 is certainly an improvement, but I don't see that it achieves this?

This branch has not been deployed

No deployments
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.

4 participants