Skip to content

Commit

Permalink
Enable colour by default in when running on CI
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner committed Jan 14, 2025
1 parent b064ef3 commit 2cc0a2c
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 27 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ Features added
Patch by Chris Barrick.
* #13227: Implement the :rst:role:`kbd` role as a ``SphinxRole``.
Patch by Adam Turner.
* #13065: Enable colour by default in when running on CI.
Patch by Adam Turner.

Bugs fixed
----------
Expand Down
2 changes: 2 additions & 0 deletions sphinx/_cli/util/colour.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ def terminal_supports_colour() -> bool:
colorama.just_fix_windows_console()
if 'FORCE_COLOUR' in os.environ or 'FORCE_COLOR' in os.environ:
return True
if os.environ.get('CI', '') in {'true', '1'}:
return True

try:
if not sys.stdout.isatty():
Expand Down
5 changes: 3 additions & 2 deletions sphinx/cmd/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
import sphinx._cli.util.errors
import sphinx.locale
from sphinx import __display_version__
from sphinx._cli.util.colour import terminal_supports_colour
from sphinx.application import Sphinx
from sphinx.locale import __
from sphinx.util._io import TeeStripANSI
from sphinx.util._pathlib import _StrPath
from sphinx.util.console import color_terminal, nocolor
from sphinx.util.console import nocolor
from sphinx.util.docutils import docutils_namespace, patch_docutils
from sphinx.util.osutil import ensuredir

Expand Down Expand Up @@ -326,7 +327,7 @@ def _validate_filenames(


def _validate_colour_support(colour: str) -> None:
if colour == 'no' or (colour == 'auto' and not color_terminal()):
if colour == 'no' or (colour == 'auto' and not terminal_supports_colour()):
nocolor()


Expand Down
5 changes: 3 additions & 2 deletions sphinx/cmd/make_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
from typing import TYPE_CHECKING

import sphinx
from sphinx._cli.util.colour import terminal_supports_colour
from sphinx.cmd.build import build_main
from sphinx.util._pathlib import _StrPath
from sphinx.util.console import blue, bold, color_terminal, nocolor
from sphinx.util.console import blue, bold, nocolor
from sphinx.util.osutil import rmtree

if TYPE_CHECKING:
Expand Down Expand Up @@ -91,7 +92,7 @@ def build_clean(self) -> int:
return 0

def build_help(self) -> None:
if not color_terminal():
if not terminal_supports_colour():
nocolor()

print(bold('Sphinx v%s' % sphinx.__display_version__))
Expand Down
5 changes: 3 additions & 2 deletions sphinx/cmd/quickstart.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@

import sphinx.locale
from sphinx import __display_version__, package_dir
from sphinx._cli.util.colour import terminal_supports_colour
from sphinx.locale import __
from sphinx.util.console import bold, color_terminal, colorize, nocolor, red
from sphinx.util.console import bold, colorize, nocolor, red
from sphinx.util.osutil import ensuredir
from sphinx.util.template import SphinxRenderer

Expand Down Expand Up @@ -724,7 +725,7 @@ def main(argv: Sequence[str] = (), /) -> int:
locale.setlocale(locale.LC_ALL, '')
sphinx.locale.init_console()

if not color_terminal():
if not terminal_supports_colour():
nocolor()

# parse options
Expand Down
23 changes: 4 additions & 19 deletions sphinx/util/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

from __future__ import annotations

import os
import re
import shutil
import sys
from typing import TYPE_CHECKING

from sphinx._cli.util.colour import (
terminal_supports_colour as color_terminal, # NoQA: F401
)

if TYPE_CHECKING:
from typing import Final

Expand Down Expand Up @@ -91,24 +94,6 @@ def term_width_line(text: str) -> str:
return text.ljust(_tw + len(text) - len(strip_escape_sequences(text))) + '\r'


def color_terminal() -> bool:
if 'NO_COLOR' in os.environ:
return False
if sys.platform == 'win32' and COLORAMA_AVAILABLE:
colorama.just_fix_windows_console()
return True
if 'FORCE_COLOR' in os.environ:
return True
if not hasattr(sys.stdout, 'isatty'):
return False
if not sys.stdout.isatty():
return False
if 'COLORTERM' in os.environ:
return True
term = os.environ.get('TERM', 'dumb').lower()
return term in {'xterm', 'linux'} or 'color' in term


def nocolor() -> None:
if sys.platform == 'win32' and COLORAMA_AVAILABLE:
colorama.deinit()
Expand Down
5 changes: 3 additions & 2 deletions sphinx/util/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import functools

from sphinx._cli.util.colour import terminal_supports_colour
from sphinx.locale import __
from sphinx.util import logging
from sphinx.util.console import bold, color_terminal
from sphinx.util.console import bold

if False:
from collections.abc import Callable, Iterable, Iterator
Expand Down Expand Up @@ -35,7 +36,7 @@ def status_iterator(
stringify_func: Callable[[Any], str] = display_chunk,
) -> Iterator[T]:
# printing on a single line requires ANSI control sequences
single_line = verbosity < 1 and color_terminal()
single_line = verbosity < 1 and terminal_supports_colour()
bold_summary = bold(summary)
if length == 0:
logger.info(bold_summary, nonl=True)
Expand Down

0 comments on commit 2cc0a2c

Please sign in to comment.