Skip to content

Commit 4e9c101

Browse files
authored
Merge pull request #10260 from hugovk/colour-env-vars
Enable FORCE_COLOR and NO_COLOR for terminal colouring
2 parents b12b39d + b172dc1 commit 4e9c101

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ Other contributors, listed alphabetically, are:
7373
* Pauli Virtanen -- autodoc improvements, autosummary extension
7474
* Eric N. Vander Weele -- autodoc improvements
7575
* Stefan van der Walt -- autosummary extension
76+
* Hugo van Kemenade -- support FORCE_COLOR and NO_COLOR
7677
* Thomas Waldmann -- apidoc module fixes
7778
* John Waltman -- Texinfo builder
7879
* Barry Warsaw -- setup command improvements

CHANGES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Deprecated
1515
Features added
1616
--------------
1717

18+
* #10260: Enable ``FORCE_COLOR`` and ``NO_COLOR`` for terminal colouring
1819
* #10234: autosummary: Add "autosummary" CSS class to summary tables
1920
* #10125: extlinks: Improve suggestion message for a reference having title
2021
* #9494, #9456: html search: Add a config variable

doc/man/sphinx-build.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,22 @@ variables to customize behavior:
304304
Additional options for :program:`sphinx-build`. These options can
305305
also be set via the shortcut variable **O** (capital 'o').
306306

307+
.. describe:: NO_COLOR
308+
309+
When set (regardless of value), :program:`sphinx-build` will not use color
310+
in terminal output. ``NO_COLOR`` takes precedence over ``FORCE_COLOR``. See
311+
`no-color.org <https://no-color.org/>`__ for other libraries supporting this
312+
community standard.
313+
314+
.. versionadded:: 4.5.0
315+
316+
.. describe:: FORCE_COLOR
317+
318+
When set (regardless of value), :program:`sphinx-build` will use color in
319+
terminal output. ``NO_COLOR`` takes precedence over ``FORCE_COLOR``.
320+
321+
.. versionadded:: 4.5.0
322+
307323
.. _when-deprecation-warnings-are-displayed:
308324

309325
Deprecation Warnings

sphinx/util/console.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,13 @@ def term_width_line(text: str) -> str:
4949

5050

5151
def color_terminal() -> bool:
52+
if 'NO_COLOR' in os.environ:
53+
return False
5254
if sys.platform == 'win32' and colorama is not None:
5355
colorama.init()
5456
return True
57+
if 'FORCE_COLOR' in os.environ:
58+
return True
5559
if not hasattr(sys.stdout, 'isatty'):
5660
return False
5761
if not sys.stdout.isatty():

0 commit comments

Comments
 (0)