diff options
author | pukkandan <pukkandan@gmail.com> | 2020-12-13 19:59:09 +0530 |
---|---|---|
committer | pukkandan <pukkandan@gmail.com> | 2021-01-04 23:03:04 +0530 |
commit | 76d321f68f412a5d07a7dfb9ad0c1c9f5513b13a (patch) | |
tree | fd688e5d827306d9fcf60d3b0bab5617cc095af2 /youtube_dlc/utils.py | |
parent | 2d30509fc893f58cac77c25134a246ed9d76e7ed (diff) |
Option to present -F output to a more tabular form
Diffstat (limited to 'youtube_dlc/utils.py')
-rw-r--r-- | youtube_dlc/utils.py | 26 |
1 files changed, 23 insertions, 3 deletions
diff --git a/youtube_dlc/utils.py b/youtube_dlc/utils.py index d814eb2ac..8c2c377af 100644 --- a/youtube_dlc/utils.py +++ b/youtube_dlc/utils.py @@ -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 |