From dc9d83e64bc15c65100449a48c7390f194cab71e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Fri, 3 May 2024 02:03:59 +0200 Subject: [PATCH] [output] support 'NO_COLOR' environment variable --- gallery_dl/__init__.py | 2 +- gallery_dl/output.py | 34 +++++++++++++++++++--------------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/gallery_dl/__init__.py b/gallery_dl/__init__.py index c9439f43..7ca405aa 100644 --- a/gallery_dl/__init__.py +++ b/gallery_dl/__init__.py @@ -91,7 +91,7 @@ def main(): signal.signal(signal_num, signal.SIG_IGN) # enable ANSI escape sequences on Windows - if util.WINDOWS and config.get(("output",), "ansi", True): + if util.WINDOWS and config.get(("output",), "ansi", output.COLORS): from ctypes import windll, wintypes, byref kernel32 = windll.kernel32 mode = wintypes.DWORD() diff --git a/gallery_dl/output.py b/gallery_dl/output.py index 5882d142..35185452 100644 --- a/gallery_dl/output.py +++ b/gallery_dl/output.py @@ -14,6 +14,11 @@ import functools import unicodedata from . import config, util, formatter + +# -------------------------------------------------------------------- +# Globals + +COLORS = not os.environ.get("NO_COLOR") COLORS_DEFAULT = { "success": "1;32", "skip" : "2", @@ -21,7 +26,20 @@ COLORS_DEFAULT = { "info" : "1;37", "warning": "1;33", "error" : "1;31", -} +} if COLORS else {} + +if util.WINDOWS: + ANSI = COLORS and os.environ.get("TERM") == "ANSI" + OFFSET = 1 + CHAR_SKIP = "# " + CHAR_SUCCESS = "* " + CHAR_ELLIPSIES = "..." +else: + ANSI = COLORS + OFFSET = 0 + CHAR_SKIP = "# " + CHAR_SUCCESS = "✔ " + CHAR_ELLIPSIES = "…" # -------------------------------------------------------------------- @@ -550,17 +568,3 @@ def shorten_string_eaw(txt, limit, sep="…", cache=EAWCache()): right -= 1 return txt[:left] + sep + txt[right+1:] - - -if util.WINDOWS: - ANSI = os.environ.get("TERM") == "ANSI" - OFFSET = 1 - CHAR_SKIP = "# " - CHAR_SUCCESS = "* " - CHAR_ELLIPSIES = "..." -else: - ANSI = True - OFFSET = 0 - CHAR_SKIP = "# " - CHAR_SUCCESS = "✔ " - CHAR_ELLIPSIES = "…"