fix table
This commit is contained in:
parent
8232fc2472
commit
e31918a949
|
@ -72,7 +72,7 @@ def main():
|
|||
|
||||
data = [(x[0], f'{round(x[1], 2)} Mbps', x[2]) for x in data]
|
||||
data.insert(0, ('Interface', 'Bandwidth', 'State'))
|
||||
print(list_to_markdown_table(data, align='left'))
|
||||
print(list_to_markdown_table(data, align='left', seperator='!', borders=False))
|
||||
|
||||
print(f' |{"".join(perf_data)}')
|
||||
sys.exit(exit_code)
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
def list_to_markdown_table(array, align: str = None):
|
||||
def list_to_markdown_table(array, align: str = None, seperator: str = '|', borders: bool = True):
|
||||
"""
|
||||
https://gist.github.com/OsKaR31415/955b166f4a286ed427f667cb21d57bfd
|
||||
Args:
|
||||
array: The array to make into a table. Mush be a rectangular array
|
||||
(constant width and height).
|
||||
align: The alignment of the cells : 'left', 'center' or 'right'.
|
||||
seperator:
|
||||
borders:
|
||||
"""
|
||||
# make sure every elements are strings
|
||||
array = [[str(elt) for elt in line] for line in array]
|
||||
|
@ -18,38 +20,46 @@ def list_to_markdown_table(array, align: str = None):
|
|||
# separate the header and the body
|
||||
array_head, *array_body = array
|
||||
|
||||
header = '| ' + ' | '.join(array_head) + ' |'
|
||||
if borders:
|
||||
edge_seperator = seperator
|
||||
else:
|
||||
edge_seperator = ''
|
||||
|
||||
header = ((edge_seperator + ' ') if borders else '') + f' {seperator} '.join(array_head) + ((' ' + edge_seperator) if borders else '')
|
||||
|
||||
# alignment of the cells
|
||||
align = str(align).lower() # make sure `align` is a lowercase string
|
||||
if align == 'none':
|
||||
# we are just setting the position of the : in the table.
|
||||
# here there are none
|
||||
border_left = '| '
|
||||
border_center = ' | '
|
||||
border_right = ' |'
|
||||
border_left = (edge_seperator + ' ') if borders else ''
|
||||
border_center = f' {seperator} '
|
||||
border_right = (' ' + edge_seperator) if borders else ''
|
||||
elif align == 'center':
|
||||
border_left = '| '
|
||||
border_center = ' | '
|
||||
border_right = ' |'
|
||||
border_left = (edge_seperator + ' ') if borders else ''
|
||||
border_center = f' {seperator} '
|
||||
border_right = (' ' + edge_seperator) if borders else ''
|
||||
elif align == 'left':
|
||||
border_left = '| '
|
||||
border_center = ' | '
|
||||
border_right = ' |'
|
||||
border_left = (edge_seperator + ' ') if borders else ''
|
||||
border_center = f' {seperator} '
|
||||
border_right = (' ' + edge_seperator) if borders else ''
|
||||
elif align == 'right':
|
||||
border_left = '| '
|
||||
border_center = ' | '
|
||||
border_right = ' |'
|
||||
border_left = (edge_seperator + ' ') if borders else ''
|
||||
border_center = f' {seperator} '
|
||||
border_right = (' ' + edge_seperator) if borders else ''
|
||||
else:
|
||||
raise ValueError("align must be 'left', 'right' or 'center'.")
|
||||
separator = border_left + border_center.join(['-' * w for w in widths]) + border_right
|
||||
|
||||
breaker = border_left + border_center.join(['-' * w for w in widths]) + border_right
|
||||
|
||||
# body of the table
|
||||
del array[0]
|
||||
body = [''] * len(array) # empty string list that we fill after
|
||||
for idx, line in enumerate(array):
|
||||
# for each line, change the body at the correct index
|
||||
body[idx] = '| ' + ' | '.join(line) + ' |'
|
||||
if borders:
|
||||
body[idx] = f'{seperator} ' + f' {seperator} '.join(line) + f' {seperator}'
|
||||
else:
|
||||
body[idx] = f' {seperator} '.join(line)
|
||||
body = '\n'.join(body)
|
||||
|
||||
return header + '\n' + separator + '\n' + body
|
||||
return header + '\n' + breaker + '\n' + body
|
||||
|
|
Loading…
Reference in New Issue