aboutsummaryrefslogtreecommitdiffstats
path: root/youtube_dlc/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'youtube_dlc/utils.py')
-rw-r--r--youtube_dlc/utils.py26
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