diff options
Diffstat (limited to 'gallery_dl/output.py')
| -rw-r--r-- | gallery_dl/output.py | 71 |
1 files changed, 48 insertions, 23 deletions
diff --git a/gallery_dl/output.py b/gallery_dl/output.py index 7e00e1a..3531304 100644 --- a/gallery_dl/output.py +++ b/gallery_dl/output.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright 2015-2021 Mike Fährmann +# Copyright 2015-2022 Mike Fährmann # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 as @@ -205,6 +205,30 @@ def setup_logging_handler(key, fmt=LOG_FORMAT, lvl=LOG_LEVEL): # -------------------------------------------------------------------- # Utility functions +def stdout_write_flush(s): + sys.stdout.write(s) + sys.stdout.flush() + + +def stderr_write_flush(s): + sys.stderr.write(s) + sys.stderr.flush() + + +if sys.stdout.line_buffering: + def stdout_write(s): + sys.stdout.write(s) +else: + stdout_write = stdout_write_flush + + +if sys.stderr.line_buffering: + def stderr_write(s): + sys.stderr.write(s) +else: + stderr_write = stderr_write_flush + + def replace_std_streams(errors="replace"): """Replace standard streams and set their error handlers to 'errors'""" for name in ("stdout", "stdin", "stderr"): @@ -255,7 +279,7 @@ class NullOutput(): def skip(self, path): """Print a message indicating that a download has been skipped""" - def success(self, path, tries): + def success(self, path): """Print a message indicating the completion of a download""" def progress(self, bytes_total, bytes_downloaded, bytes_per_second): @@ -265,14 +289,10 @@ class NullOutput(): class PipeOutput(NullOutput): def skip(self, path): - stdout = sys.stdout - stdout.write(CHAR_SKIP + path + "\n") - stdout.flush() + stdout_write(CHAR_SKIP + path + "\n") - def success(self, path, tries): - stdout = sys.stdout - stdout.write(path + "\n") - stdout.flush() + def success(self, path): + stdout_write(path + "\n") class TerminalOutput(NullOutput): @@ -288,38 +308,43 @@ class TerminalOutput(NullOutput): self.shorten = util.identity def start(self, path): - stdout = sys.stdout - stdout.write(self.shorten(" " + path)) - stdout.flush() + stdout_write_flush(self.shorten(" " + path)) def skip(self, path): - sys.stdout.write(self.shorten(CHAR_SKIP + path) + "\n") + stdout_write(self.shorten(CHAR_SKIP + path) + "\n") - def success(self, path, tries): - sys.stdout.write("\r" + self.shorten(CHAR_SUCCESS + path) + "\n") + def success(self, path): + stdout_write("\r" + self.shorten(CHAR_SUCCESS + path) + "\n") def progress(self, bytes_total, bytes_downloaded, bytes_per_second): bdl = util.format_value(bytes_downloaded) bps = util.format_value(bytes_per_second) if bytes_total is None: - sys.stderr.write("\r{:>7}B {:>7}B/s ".format(bdl, bps)) + stderr_write("\r{:>7}B {:>7}B/s ".format(bdl, bps)) else: - sys.stderr.write("\r{:>3}% {:>7}B {:>7}B/s ".format( + stderr_write("\r{:>3}% {:>7}B {:>7}B/s ".format( bytes_downloaded * 100 // bytes_total, bdl, bps)) class ColorOutput(TerminalOutput): + def __init__(self): + TerminalOutput.__init__(self) + + colors = config.get(("output",), "colors") or {} + self.color_skip = "\033[{}m".format( + colors.get("skip", "2")) + self.color_success = "\r\033[{}m".format( + colors.get("success", "1;32")) + def start(self, path): - stdout = sys.stdout - stdout.write(self.shorten(path)) - stdout.flush() + stdout_write_flush(self.shorten(path)) def skip(self, path): - sys.stdout.write("\033[2m" + self.shorten(path) + "\033[0m\n") + stdout_write(self.color_skip + self.shorten(path) + "\033[0m\n") - def success(self, path, tries): - sys.stdout.write("\r\033[1;32m" + self.shorten(path) + "\033[0m\n") + def success(self, path): + stdout_write(self.color_success + self.shorten(path) + "\033[0m\n") class EAWCache(dict): |
