1
0
mirror of https://github.com/yt-dlp/yt-dlp.git synced 2025-10-03 23:43:21 -04:00

Option to present -F output to a more tabular form

This commit is contained in:
pukkandan
2020-12-13 19:59:09 +05:30
parent 2d30509fc8
commit 76d321f68f
5 changed files with 86 additions and 12 deletions

View File

@@ -4315,11 +4315,25 @@ def determine_protocol(info_dict):
return compat_urllib_parse_urlparse(url).scheme
def render_table(header_row, data):
def render_table(header_row, data, delim=False, extraGap=0, hideEmpty=False):
""" Render a list of rows, each as a list of values """
def get_max_lens(table):
return [max(len(compat_str(v)) for v in col) for col in zip(*table)]
def filter_using_list(row, filterArray):
return [col for (take, col) in zip(filterArray, row) if take]
if hideEmpty:
max_lens = get_max_lens(data)
header_row = filter_using_list(header_row, max_lens)
data = [filter_using_list(row, max_lens) for row in data]
table = [header_row] + data
max_lens = [max(len(compat_str(v)) for v in col) for col in zip(*table)]
format_str = ' '.join('%-' + compat_str(ml + 1) + 's' for ml in max_lens[:-1]) + '%s'
max_lens = get_max_lens(table)
if delim:
table = [header_row] + [['-' * ml for ml in max_lens]] + data
format_str = ' '.join('%-' + compat_str(ml + extraGap) + 's' for ml in max_lens[:-1]) + ' %s'
return '\n'.join(format_str % tuple(row) for row in table)
@@ -5795,3 +5809,9 @@ def to_high_limit_path(path):
return r'\\?\ '.rstrip() + os.path.abspath(path)
return path
def format_field(obj, field, template='%s', ignore=(None, ''), default='', func=None):
val = obj.get(field, default)
if func and val not in ignore:
val = func(val)
return template % val if val not in ignore else default