diff --git a/src/huggingface_hub/cli/_output.py b/src/huggingface_hub/cli/_output.py index e1ac21c57c..46cedd4212 100644 --- a/src/huggingface_hub/cli/_output.py +++ b/src/huggingface_hub/cli/_output.py @@ -26,8 +26,9 @@ import click +from huggingface_hub import constants from huggingface_hub.errors import ConfirmationError -from huggingface_hub.utils import ANSI, StatusLine, disable_progress_bars, is_agent, tabulate +from huggingface_hub.utils import ANSI, StatusLine, disable_progress_bars, enable_progress_bars, is_agent, tabulate class OutputFormat(str, Enum): @@ -65,8 +66,13 @@ def set_mode(self, mode: OutputFormat = OutputFormat.auto) -> None: if mode == OutputFormat.auto: mode = OutputFormat.agent if is_agent() else OutputFormat.human self.mode = mode - if mode != OutputFormat.human: - disable_progress_bars() + is_human = mode == OutputFormat.human + ANSI.set_enabled(is_human) + if constants.HF_HUB_DISABLE_PROGRESS_BARS is None: # env var has priority + if is_human: + enable_progress_bars() + else: + disable_progress_bars() def set_no_truncate(self, no_truncate: bool) -> None: """Toggle off cell truncation for human table output.""" diff --git a/src/huggingface_hub/utils/_terminal.py b/src/huggingface_hub/utils/_terminal.py index 051713fb8b..c12285489a 100644 --- a/src/huggingface_hub/utils/_terminal.py +++ b/src/huggingface_hub/utils/_terminal.py @@ -68,6 +68,11 @@ class ANSI: _reset = "\u001b[0m" _underline = "\u001b[4m" _yellow = "\u001b[33m" + _enabled: bool | None = None # None: fall back to agent detection (callers outside the `hf` CLI) + + @classmethod + def set_enabled(cls, enabled: bool) -> None: + cls._enabled = enabled @classmethod def blue(cls, s: str) -> str: @@ -99,10 +104,11 @@ def yellow(cls, s: str) -> str: @classmethod def _format(cls, s: str, code: str) -> str: - if os.environ.get("NO_COLOR") or is_agent(): + if os.environ.get("NO_COLOR"): # See https://no-color.org/ return s - return f"{code}{s}{cls._reset}" + enabled = cls._enabled if cls._enabled is not None else not is_agent() + return f"{code}{s}{cls._reset}" if enabled else s def select_choice(prompt: str, choices: list[str]) -> int: