diff --git a/docs/configuration.rst b/docs/configuration.rst index 4dc89eb3..720875f3 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -2948,6 +2948,17 @@ Description .. __: `output.mode`_ +output.ansi +----------- +Type + ``bool`` +Default + ``false`` +Description + | On Windows, enable ANSI escape sequences and colored output + | by setting the ``ENABLE_VIRTUAL_TERMINAL_PROCESSING`` flag for stdout and stderr. + + output.skip ----------- Type diff --git a/docs/gallery-dl-example.conf b/docs/gallery-dl-example.conf index 29b2507b..89bfd0c7 100644 --- a/docs/gallery-dl-example.conf +++ b/docs/gallery-dl-example.conf @@ -334,6 +334,9 @@ "#": "while also considering wider East-Asian characters", "shorten": "eaw", + "#": "enable ANSI escape sequences on Windows", + "ansi": true, + "#": "write logging messages to a separate file", "logfile": { "path": "~/gallery-dl/log.txt", diff --git a/docs/gallery-dl.conf b/docs/gallery-dl.conf index 6e71d426..af8bba6a 100644 --- a/docs/gallery-dl.conf +++ b/docs/gallery-dl.conf @@ -355,6 +355,7 @@ "mode": "auto", "progress": true, "shorten": true, + "ansi": false, "colors": { "success": "1;32", "skip" : "2" diff --git a/gallery_dl/__init__.py b/gallery_dl/__init__.py index d66e285c..9e274172 100644 --- a/gallery_dl/__init__.py +++ b/gallery_dl/__init__.py @@ -154,6 +154,21 @@ def main(): else: signal.signal(signal_num, signal.SIG_IGN) + # enable ANSI escape sequences on Windows + if util.WINDOWS and config.get(("output",), "ansi"): + from ctypes import windll, wintypes, byref + kernel32 = windll.kernel32 + mode = wintypes.DWORD() + + for handle_id in (-11, -12): # stdout and stderr + handle = kernel32.GetStdHandle(handle_id) + kernel32.GetConsoleMode(handle, byref(mode)) + if not mode.value & 0x4: + mode.value |= 0x4 + kernel32.SetConsoleMode(handle, mode) + + output.ANSI = True + # extractor modules modules = config.get(("extractor",), "modules") if modules is not None: