|
4 | 4 |
|
5 | 5 | import logging |
6 | 6 | import optparse |
| 7 | +import os |
7 | 8 | import shutil |
8 | 9 | import sys |
9 | 10 | import textwrap |
10 | 11 | from collections.abc import Generator |
11 | 12 | from contextlib import suppress |
12 | 13 | from typing import Any, NoReturn |
13 | 14 |
|
14 | | -from pip._vendor.rich.console import Console, RenderableType |
| 15 | +from pip._vendor.rich.console import RenderableType |
15 | 16 | from pip._vendor.rich.markup import escape |
16 | 17 | from pip._vendor.rich.style import StyleType |
17 | 18 | from pip._vendor.rich.text import Text |
18 | 19 | from pip._vendor.rich.theme import Theme |
19 | 20 |
|
20 | 21 | from pip._internal.cli.status_codes import UNKNOWN_ERROR |
21 | 22 | from pip._internal.configuration import Configuration, ConfigurationError |
| 23 | +from pip._internal.utils.logging import PipConsole |
22 | 24 | from pip._internal.utils.misc import redact_auth_from_url, strtobool |
23 | 25 |
|
24 | 26 | logger = logging.getLogger(__name__) |
@@ -46,7 +48,14 @@ def __init__(self, *args: Any, **kwargs: Any) -> None: |
46 | 48 | kwargs["indent_increment"] = 1 |
47 | 49 | kwargs["width"] = shutil.get_terminal_size()[0] - 2 |
48 | 50 | 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) |
50 | 59 | self.rich_option_strings: dict[optparse.Option, Text] = {} |
51 | 60 |
|
52 | 61 | def stringify(self, text: RenderableType) -> str: |
|
0 commit comments