Skip to content

Commit db1ba7f

Browse files
committed
Handle (PIP_)NO_COLOR
1 parent 588c84a commit db1ba7f

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/pip/_internal/cli/parser.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,23 @@
44

55
import logging
66
import optparse
7+
import os
78
import shutil
89
import sys
910
import textwrap
1011
from collections.abc import Generator
1112
from contextlib import suppress
1213
from typing import Any, NoReturn
1314

14-
from pip._vendor.rich.console import Console, RenderableType
15+
from pip._vendor.rich.console import RenderableType
1516
from pip._vendor.rich.markup import escape
1617
from pip._vendor.rich.style import StyleType
1718
from pip._vendor.rich.text import Text
1819
from pip._vendor.rich.theme import Theme
1920

2021
from pip._internal.cli.status_codes import UNKNOWN_ERROR
2122
from pip._internal.configuration import Configuration, ConfigurationError
23+
from pip._internal.utils.logging import PipConsole
2224
from pip._internal.utils.misc import redact_auth_from_url, strtobool
2325

2426
logger = logging.getLogger(__name__)
@@ -46,7 +48,14 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
4648
kwargs["indent_increment"] = 1
4749
kwargs["width"] = shutil.get_terminal_size()[0] - 2
4850
super().__init__(*args, **kwargs)
49-
self.console: Console = Console(theme=Theme(self.styles))
51+
# This is unfortunate but necessary since arguments may have not been
52+
# parsed yet at this point, so detect --no-color manually.
53+
no_color = (
54+
"--no-color" in sys.argv
55+
or bool(strtobool(os.environ.get("PIP_NO_COLOR", "no") or "no"))
56+
or "NO_COLOR" in os.environ
57+
)
58+
self.console = PipConsole(theme=Theme(self.styles), no_color=no_color)
5059
self.rich_option_strings: dict[optparse.Option, Text] = {}
5160

5261
def stringify(self, text: RenderableType) -> str:

0 commit comments

Comments
 (0)