A feature-rich command-line audio/video downloader
[core] Support `auto-tty` and `no_color-tty` for `--color` (#10453)
Simon Sawickid9cbced493ca
diff --git a/yt_dlp/YoutubeDL.py b/yt_dlp/YoutubeDL.pyindex 503dcb1124..9691a1ea7c 100644--- a/yt_dlp/YoutubeDL.py+++ b/yt_dlp/YoutubeDL.py@@ -452,7 +452,8 @@ class YoutubeDL:Can also just be a single color policy,in which case it applies to all outputs.Valid stream names are 'stdout' and 'stderr'.- Valid color policies are one of 'always', 'auto', 'no_color' or 'never'.+ Valid color policies are one of 'always', 'auto',+ 'no_color', 'never', 'auto-tty' or 'no_color-tty'.geo_bypass: Bypass geographic restriction via faking X-Forwarded-ForHTTP headergeo_bypass_country:@@ -659,12 +660,15 @@ class YoutubeDL:self.params['color'] = 'no_color'term_allow_color = os.getenv('TERM', '').lower() != 'dumb'- no_color = bool(os.getenv('NO_COLOR'))+ base_no_color = bool(os.getenv('NO_COLOR'))def process_color_policy(stream):stream_name = {sys.stdout: 'stdout', sys.stderr: 'stderr'}[stream]- policy = traverse_obj(self.params, ('color', (stream_name, None), {str}), get_all=False)- if policy in ('auto', None):+ policy = traverse_obj(self.params, ('color', (stream_name, None), {str}, any)) or 'auto'+ if policy in ('auto', 'auto-tty', 'no_color-tty'):+ no_color = base_no_color+ if policy.endswith('tty'):+ no_color = policy.startswith('no_color')if term_allow_color and supports_terminal_sequences(stream):return 'no_color' if no_color else Truereturn Falsediff --git a/yt_dlp/__init__.py b/yt_dlp/__init__.pyindex 0e48569e33..c0b8e3b507 100644--- a/yt_dlp/__init__.py+++ b/yt_dlp/__init__.py@@ -468,7 +468,7 @@ def validate_options(opts):default_downloader = ed.get_basename()for policy in opts.color.values():- if policy not in ('always', 'auto', 'no_color', 'never'):+ if policy not in ('always', 'auto', 'auto-tty', 'no_color', 'no_color-tty', 'never'):raise ValueError(f'"{policy}" is not a valid color policy')warnings, deprecation_warnings = [], []diff --git a/yt_dlp/options.py b/yt_dlp/options.pyindex 76db06c854..ffe2463fe2 100644--- a/yt_dlp/options.py+++ b/yt_dlp/options.py@@ -462,6 +462,7 @@ def create_parser():'the STREAM (stdout or stderr) to apply the setting to. ''Can be one of "always", "auto" (default), "never", or ''"no_color" (use non color terminal sequences). '+ 'Use "auto-tty" or "no_color-tty" to decide based on terminal support only. ''Can be used multiple times'))general.add_option('--compat-options',