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

Use sphinx._cli.util.colour in sphinx.util.console #13241

Merged
merged 2 commits into from
Jan 16, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions sphinx/_cli/util/colour.py
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@
import colorama


_COLOURING_DISABLED = True
_COLOURING_DISABLED = False


def terminal_supports_colour() -> bool:
@@ -19,6 +19,7 @@ def terminal_supports_colour() -> bool:
return False
if sys.platform == 'win32':
colorama.just_fix_windows_console()
return True
if 'FORCE_COLOUR' in os.environ or 'FORCE_COLOR' in os.environ:
return True
if os.environ.get('CI', '') in {'true', '1'}:
@@ -58,6 +59,7 @@ def inner(text: str) -> str:
return text
return f'\x1b[{escape_code}m{text}\x1b[39;49;00m'

inner.__escape_code = escape_code # type: ignore[attr-defined]
return inner


@@ -75,7 +77,7 @@ def _create_input_mode_colour_func(escape_code: str, /) -> Callable[[str], str]:
def inner(text: str) -> str:
if _COLOURING_DISABLED:
return text
return f'\x01\x1b[{escape_code}m\x02{text}\x01\x1b[39;49;00m\x02'
return f'\1\x1b[{escape_code}m\2{text}\1\x1b[39;49;00m\2'

return inner

2 changes: 1 addition & 1 deletion sphinx/application.py
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@

import sphinx
from sphinx import locale, package_dir
from sphinx._cli.util.colour import bold
from sphinx.config import Config
from sphinx.environment import BuildEnvironment
from sphinx.errors import ApplicationError, ConfigError, VersionRequirementError
@@ -27,7 +28,6 @@
from sphinx.util import docutils, logging
from sphinx.util._pathlib import _StrPath, _StrPathProperty
from sphinx.util.build_phase import BuildPhase
from sphinx.util.console import bold
from sphinx.util.display import progress_message
from sphinx.util.i18n import CatalogRepository
from sphinx.util.logging import prefixed_warnings
2 changes: 1 addition & 1 deletion sphinx/builders/__init__.py
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@
from docutils import nodes
from docutils.utils import DependencyList

from sphinx._cli.util.colour import bold
from sphinx.environment import (
CONFIG_CHANGED_REASON,
CONFIG_OK,
@@ -29,7 +30,6 @@
from sphinx.util._importer import import_object
from sphinx.util._pathlib import _StrPathProperty
from sphinx.util.build_phase import BuildPhase
from sphinx.util.console import bold
from sphinx.util.display import progress_message, status_iterator
from sphinx.util.docutils import sphinx_domains
from sphinx.util.i18n import CatalogRepository, docname_to_domain
2 changes: 1 addition & 1 deletion sphinx/builders/changes.py
Original file line number Diff line number Diff line change
@@ -7,11 +7,11 @@
from typing import TYPE_CHECKING

from sphinx import package_dir
from sphinx._cli.util.colour import bold
from sphinx.builders import Builder
from sphinx.locale import _, __
from sphinx.theming import HTMLThemeFactory
from sphinx.util import logging
from sphinx.util.console import bold
from sphinx.util.fileutil import copy_asset_file
from sphinx.util.osutil import ensuredir, os_path

2 changes: 1 addition & 1 deletion sphinx/builders/gettext.py
Original file line number Diff line number Diff line change
@@ -14,11 +14,11 @@
from docutils import nodes

from sphinx import addnodes, package_dir
from sphinx._cli.util.colour import bold
from sphinx.builders import Builder
from sphinx.errors import ThemeError
from sphinx.locale import __
from sphinx.util import logging
from sphinx.util.console import bold
from sphinx.util.display import status_iterator
from sphinx.util.i18n import docname_to_domain
from sphinx.util.index_entries import split_index_msg
2 changes: 1 addition & 1 deletion sphinx/builders/html/__init__.py
Original file line number Diff line number Diff line change
@@ -23,6 +23,7 @@

from sphinx import __display_version__, package_dir
from sphinx import version_info as sphinx_version
from sphinx._cli.util.colour import bold
from sphinx.builders import Builder
from sphinx.builders.html._assets import (
_CascadingStyleSheet,
@@ -45,7 +46,6 @@
from sphinx.util._pathlib import _StrPath
from sphinx.util._timestamps import _format_rfc3339_microseconds
from sphinx.util._uri import is_url
from sphinx.util.console import bold
from sphinx.util.display import progress_message, status_iterator
from sphinx.util.docutils import new_document
from sphinx.util.fileutil import copy_asset
2 changes: 1 addition & 1 deletion sphinx/builders/latex/__init__.py
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@

import sphinx.builders.latex.nodes # NoQA: F401 # Workaround: import this before writer to avoid ImportError
from sphinx import addnodes, highlighting, package_dir
from sphinx._cli.util.colour import darkgreen
from sphinx.builders import Builder
from sphinx.builders.latex.constants import (
ADDITIONAL_SETTINGS,
@@ -25,7 +26,6 @@
from sphinx.errors import NoUri, SphinxError
from sphinx.locale import _, __
from sphinx.util import logging, texescape
from sphinx.util.console import darkgreen
from sphinx.util.display import progress_message, status_iterator
from sphinx.util.docutils import SphinxFileOutput, new_document
from sphinx.util.fileutil import copy_asset_file
2 changes: 1 addition & 1 deletion sphinx/builders/linkcheck.py
Original file line number Diff line number Diff line change
@@ -24,12 +24,12 @@
)
from requests.exceptions import Timeout as RequestTimeout

from sphinx._cli.util.colour import darkgray, darkgreen, purple, red, turquoise
from sphinx.builders.dummy import DummyBuilder
from sphinx.locale import __
from sphinx.transforms.post_transforms import SphinxPostTransform
from sphinx.util import logging, requests
from sphinx.util._uri import encode_uri
from sphinx.util.console import darkgray, darkgreen, purple, red, turquoise
from sphinx.util.http_date import rfc1123_to_epoch
from sphinx.util.nodes import get_node_line

2 changes: 1 addition & 1 deletion sphinx/builders/manpage.py
Original file line number Diff line number Diff line change
@@ -10,10 +10,10 @@
from docutils.io import FileOutput

from sphinx import addnodes
from sphinx._cli.util.colour import darkgreen
from sphinx.builders import Builder
from sphinx.locale import __
from sphinx.util import logging
from sphinx.util.console import darkgreen
from sphinx.util.display import progress_message
from sphinx.util.nodes import inline_all_toctrees
from sphinx.util.osutil import ensuredir, make_filename_from_project
2 changes: 1 addition & 1 deletion sphinx/builders/singlehtml.py
Original file line number Diff line number Diff line change
@@ -7,12 +7,12 @@

from docutils import nodes

from sphinx._cli.util.colour import darkgreen
from sphinx.builders.html import StandaloneHTMLBuilder
from sphinx.deprecation import RemovedInSphinx10Warning
from sphinx.environment.adapters.toctree import global_toctree_for_doc
from sphinx.locale import __
from sphinx.util import logging
from sphinx.util.console import darkgreen
from sphinx.util.display import progress_message
from sphinx.util.nodes import inline_all_toctrees

2 changes: 1 addition & 1 deletion sphinx/builders/texinfo.py
Original file line number Diff line number Diff line change
@@ -12,12 +12,12 @@
from docutils.io import FileOutput

from sphinx import addnodes, package_dir
from sphinx._cli.util.colour import darkgreen
from sphinx.builders import Builder
from sphinx.environment.adapters.asset import ImageAdapter
from sphinx.errors import NoUri
from sphinx.locale import _, __
from sphinx.util import logging
from sphinx.util.console import darkgreen
from sphinx.util.display import progress_message, status_iterator
from sphinx.util.docutils import new_document
from sphinx.util.nodes import inline_all_toctrees
5 changes: 2 additions & 3 deletions sphinx/cmd/build.py
Original file line number Diff line number Diff line change
@@ -13,12 +13,11 @@
import sphinx._cli.util.errors
import sphinx.locale
from sphinx import __display_version__
from sphinx._cli.util.colour import terminal_supports_colour
from sphinx._cli.util.colour import disable_colour, 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 nocolor
from sphinx.util.docutils import docutils_namespace, patch_docutils
from sphinx.util.osutil import ensuredir

@@ -328,7 +327,7 @@ def _validate_filenames(

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


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

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

if TYPE_CHECKING:
@@ -93,7 +92,7 @@ def build_clean(self) -> int:

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

print(bold('Sphinx v%s' % sphinx.__display_version__))
print("Please use `make %s' where %s is one of" % ((blue('target'),) * 2))
11 changes: 8 additions & 3 deletions sphinx/cmd/quickstart.py
Original file line number Diff line number Diff line change
@@ -31,9 +31,14 @@

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

@@ -726,7 +731,7 @@ def main(argv: Sequence[str] = (), /) -> int:
sphinx.locale.init_console()

if not terminal_supports_colour():
nocolor()
disable_colour()

# parse options
parser = get_parser()
2 changes: 1 addition & 1 deletion sphinx/ext/coverage.py
Original file line number Diff line number Diff line change
@@ -17,10 +17,10 @@
from typing import TYPE_CHECKING

import sphinx
from sphinx._cli.util.colour import red
from sphinx.builders import Builder
from sphinx.locale import __
from sphinx.util import logging
from sphinx.util.console import red
from sphinx.util.inspect import safe_getattr

if TYPE_CHECKING:
2 changes: 1 addition & 1 deletion sphinx/ext/doctest.py
Original file line number Diff line number Diff line change
@@ -19,10 +19,10 @@
from packaging.version import Version

import sphinx
from sphinx._cli.util.colour import bold
from sphinx.builders import Builder
from sphinx.locale import __
from sphinx.util import logging
from sphinx.util.console import bold
from sphinx.util.docutils import SphinxDirective
from sphinx.util.osutil import relpath

8 changes: 6 additions & 2 deletions sphinx/testing/util.py
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@
import sphinx.application
import sphinx.locale
import sphinx.pycode
from sphinx.util.console import strip_colors
from sphinx._cli.util.errors import strip_escape_sequences
from sphinx.util.docutils import additional_nodes

if TYPE_CHECKING:
@@ -271,7 +271,11 @@ def _clean_up_global_state() -> None:

# deprecated name -> (object to return, canonical path or '', removal version)
_DEPRECATED_OBJECTS: dict[str, tuple[Any, str, tuple[int, int]]] = {
'strip_escseq': (strip_colors, 'sphinx.util.console.strip_colors', (9, 0)),
'strip_escseq': (
strip_escape_sequences,
'sphinx.util.console.strip_escape_sequences',
(9, 0),
),
}


6 changes: 3 additions & 3 deletions sphinx/util/__init__.py
Original file line number Diff line number Diff line change
@@ -127,10 +127,10 @@ def __getattr__(name: str) -> Any:

return patfilter

if name == 'strip_colors':
from sphinx.util.console import strip_colors
if name == 'strip_escape_sequences':
from sphinx._cli.util.errors import strip_escape_sequences

return strip_colors
return strip_escape_sequences

if name in {
'caption_ref_re',
2 changes: 1 addition & 1 deletion sphinx/util/_io.py
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@

from typing import TYPE_CHECKING

from sphinx.util.console import strip_escape_sequences
from sphinx._cli.util.errors import strip_escape_sequences

if TYPE_CHECKING:
from typing import Protocol
Loading
Loading