Skip to content

Commit

Permalink
[output] enable colors by default
Browse files Browse the repository at this point in the history
  • Loading branch information
mikf committed Apr 20, 2024
1 parent 14b3826 commit 20e2c00
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
14 changes: 12 additions & 2 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797#colors--graphics-mode>`__
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion gallery_dl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
27 changes: 22 additions & 5 deletions gallery_dl/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@
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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 20e2c00

Please sign in to comment.