[downloader] Add colors to download progress

This commit is contained in:
pukkandan 2021-11-29 02:55:37 +05:30
parent b29165267f
commit 7578d77d8c
No known key found for this signature in database
GPG key ID: 0F00D95A001F4698
3 changed files with 31 additions and 11 deletions

View file

@ -849,24 +849,24 @@ class Styles(Enum):
WARNING = 'yellow' WARNING = 'yellow'
SUPPRESS = 'light black' SUPPRESS = 'light black'
def __format_text(self, out, text, f, fallback=None, *, test_encoding=False): def _format_text(self, handle, allow_colors, text, f, fallback=None, *, test_encoding=False):
assert out in ('screen', 'err')
if test_encoding: if test_encoding:
original_text = text original_text = text
handle = self._screen_file if out == 'screen' else self._err_file
encoding = self.params.get('encoding') or getattr(handle, 'encoding', 'ascii') encoding = self.params.get('encoding') or getattr(handle, 'encoding', 'ascii')
text = text.encode(encoding, 'ignore').decode(encoding) text = text.encode(encoding, 'ignore').decode(encoding)
if fallback is not None and text != original_text: if fallback is not None and text != original_text:
text = fallback text = fallback
if isinstance(f, self.Styles): if isinstance(f, self.Styles):
f = f._value_ f = f._value_
return format_text(text, f) if self._allow_colors[out] else text if fallback is None else fallback return format_text(text, f) if allow_colors else text if fallback is None else fallback
def _format_screen(self, *args, **kwargs): def _format_screen(self, *args, **kwargs):
return self.__format_text('screen', *args, **kwargs) return self._format_text(
self._screen_file, self._allow_colors['screen'], *args, **kwargs)
def _format_err(self, *args, **kwargs): def _format_err(self, *args, **kwargs):
return self.__format_text('err', *args, **kwargs) return self._format_text(
self._err_file, self._allow_colors['err'], *args, **kwargs)
def report_warning(self, message, only_once=False): def report_warning(self, message, only_once=False):
''' '''

View file

@ -247,11 +247,29 @@ def _prepare_multiline_status(self, lines=1):
self._multiline = BreaklineStatusPrinter(self.ydl._screen_file, lines) self._multiline = BreaklineStatusPrinter(self.ydl._screen_file, lines)
else: else:
self._multiline = MultilinePrinter(self.ydl._screen_file, lines, not self.params.get('quiet')) self._multiline = MultilinePrinter(self.ydl._screen_file, lines, not self.params.get('quiet'))
self._multiline.allow_colors = self._multiline._HAVE_FULLCAP and not self.params.get('no_color')
def _finish_multiline_status(self): def _finish_multiline_status(self):
self._multiline.end() self._multiline.end()
def _report_progress_status(self, s): _progress_styles = {
'downloaded_bytes': 'light blue',
'percent': 'light blue',
'eta': 'yellow',
'speed': 'green',
'elapsed': 'bold white',
'total_bytes': '',
'total_bytes_estimate': '',
}
def _report_progress_status(self, s, default_template):
for name, style in self._progress_styles.items():
name = f'_{name}_str'
if name not in s:
continue
s[name] = self._format_progress(s[name], style)
s['_default_template'] = default_template % s
progress_dict = s.copy() progress_dict = s.copy()
progress_dict.pop('info_dict') progress_dict.pop('info_dict')
progress_dict = {'info': s['info_dict'], 'progress': progress_dict} progress_dict = {'info': s['info_dict'], 'progress': progress_dict}
@ -264,6 +282,10 @@ def _report_progress_status(self, s):
progress_template.get('download-title') or 'yt-dlp %(progress._default_template)s', progress_template.get('download-title') or 'yt-dlp %(progress._default_template)s',
progress_dict)) progress_dict))
def _format_progress(self, *args, **kwargs):
return self.ydl._format_text(
self._multiline.stream, self._multiline.allow_colors, *args, **kwargs)
def report_progress(self, s): def report_progress(self, s):
if s['status'] == 'finished': if s['status'] == 'finished':
if self.params.get('noprogress'): if self.params.get('noprogress'):
@ -276,7 +298,6 @@ def report_progress(self, s):
s['_elapsed_str'] = self.format_seconds(s['elapsed']) s['_elapsed_str'] = self.format_seconds(s['elapsed'])
msg_template += ' in %(_elapsed_str)s' msg_template += ' in %(_elapsed_str)s'
s['_percent_str'] = self.format_percent(100) s['_percent_str'] = self.format_percent(100)
s['_default_template'] = msg_template % s
self._report_progress_status(s) self._report_progress_status(s)
return return
@ -323,8 +344,7 @@ def report_progress(self, s):
msg_template += ' (frag %(fragment_index)s/%(fragment_count)s)' msg_template += ' (frag %(fragment_index)s/%(fragment_count)s)'
elif s.get('fragment_index'): elif s.get('fragment_index'):
msg_template += ' (frag %(fragment_index)s)' msg_template += ' (frag %(fragment_index)s)'
s['_default_template'] = msg_template % s self._report_progress_status(s, msg_template)
self._report_progress_status(s)
def report_resuming_byte(self, resume_len): def report_resuming_byte(self, resume_len):
"""Report attempt to resume at given byte.""" """Report attempt to resume at given byte."""

View file

@ -78,6 +78,7 @@ class MultilinePrinterBase:
def __init__(self, stream=None, lines=1): def __init__(self, stream=None, lines=1):
self.stream = stream self.stream = stream
self.maximum = lines - 1 self.maximum = lines - 1
self._HAVE_FULLCAP = supports_terminal_sequences(stream)
def __enter__(self): def __enter__(self):
return self return self
@ -124,7 +125,6 @@ def __init__(self, stream=None, lines=1, preserve_output=True):
self.preserve_output = preserve_output self.preserve_output = preserve_output
self._lastline = self._lastlength = 0 self._lastline = self._lastlength = 0
self._movelock = Lock() self._movelock = Lock()
self._HAVE_FULLCAP = supports_terminal_sequences(self.stream)
def lock(func): def lock(func):
@functools.wraps(func) @functools.wraps(func)