1
0
mirror of https://github.com/openSUSE/osc.git synced 2025-09-07 21:58:41 +02:00

Move format_table to util.helper

This commit is contained in:
Anatoli Babenia
2022-07-29 22:32:54 +03:00
parent fed3de9c11
commit 0bc142df91
2 changed files with 20 additions and 20 deletions

View File

@@ -51,3 +51,22 @@ def raw_input(*args):
def _html_escape(data):
return html.escape(data, quote=False)
def format_table(rows, headers):
"""Format list of tuples into equal width table with headers"""
maxlens = [len(h) for h in headers]
for r in rows:
for i, c in enumerate(r):
maxlens[i] = max(maxlens[i], len(c))
tpltpl = []
for i, m in enumerate(maxlens):
tpltpl.append('{%s:<%s}' % (i, m))
# {0:12} {1:7} {2:10} {3:8}
templ = ' '.join(tpltpl) + '\n'
out = templ.format(*headers)
out += templ.format(*['-'*m for m in maxlens])
for r in rows:
out += templ.format(*r)
return out