From 20e2c0042b590ec243fc1fe18ce34c010739db9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Sat, 20 Apr 2024 20:49:28 +0200 Subject: [PATCH] [output] enable colors by default --- docs/configuration.rst | 14 ++++++++++++-- gallery_dl/__init__.py | 2 +- gallery_dl/output.py | 27 ++++++++++++++++++++++----- 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/docs/configuration.rst b/docs/configuration.rst index 1f069259..ec32e3b9 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -4921,7 +4921,17 @@ output.colors Type ``object`` (`key` -> `ANSI color`) Default - ``{"success": "1;32", "skip": "2"}`` + .. code:: json + + { + "success": "1;32", + "skip" : "2", + "debug" : "0;37", + "info" : "1;37", + "warning": "1;33", + "error" : "1;31" + } + Description Controls the `ANSI colors `__ @@ -4947,7 +4957,7 @@ output.ansi Type ``bool`` Default - ``false`` + ``true`` Description | On Windows, enable ANSI escape sequences and colored output | by setting the ``ENABLE_VIRTUAL_TERMINAL_PROCESSING`` flag for stdout and stderr. diff --git a/gallery_dl/__init__.py b/gallery_dl/__init__.py index fb3c5cb7..01a51904 100644 --- a/gallery_dl/__init__.py +++ b/gallery_dl/__init__.py @@ -86,7 +86,7 @@ def main(): signal.signal(signal_num, signal.SIG_IGN) # enable ANSI escape sequences on Windows - if util.WINDOWS and config.get(("output",), "ansi"): + if util.WINDOWS and config.get(("output",), "ansi", True): 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 4ef71db0..5882d142 100644 --- a/gallery_dl/output.py +++ b/gallery_dl/output.py @@ -14,6 +14,15 @@ import functools import unicodedata from . import config, util, formatter +COLORS_DEFAULT = { + "success": "1;32", + "skip" : "2", + "debug" : "0;37", + "info" : "1;37", + "warning": "1;33", + "error" : "1;31", +} + # -------------------------------------------------------------------- # Logging @@ -189,7 +198,9 @@ def configure_logging(loglevel): handler = root.handlers[0] opts = config.interpolate(("output",), "log") - colors = config.interpolate(("output",), "colors") or {} + colors = config.interpolate(("output",), "colors") + if colors is None: + colors = COLORS_DEFAULT if colors and not opts: opts = LOG_FORMAT @@ -326,9 +337,12 @@ def select(): mode = config.get(("output",), "mode") if mode is None or mode == "auto": - if hasattr(sys.stdout, "isatty") and sys.stdout.isatty(): - output = ColorOutput() if ANSI else TerminalOutput() - else: + try: + if sys.stdout.isatty(): + output = ColorOutput() if ANSI else TerminalOutput() + else: + output = PipeOutput() + except Exception: output = PipeOutput() elif isinstance(mode, dict): output = CustomOutput(mode) @@ -407,7 +421,10 @@ class ColorOutput(TerminalOutput): def __init__(self): TerminalOutput.__init__(self) - colors = config.get(("output",), "colors") or {} + colors = config.interpolate(("output",), "colors") + if colors is None: + colors = COLORS_DEFAULT + self.color_skip = "\033[{}m".format( colors.get("skip", "2")) self.color_success = "\r\033[{}m".format(