From e31918a949af009dc6f4915441bf5fe748e575a8 Mon Sep 17 00:00:00 2001 From: Cyberes Date: Fri, 21 Apr 2023 23:54:18 -0600 Subject: [PATCH] fix table --- check_bandwidth.py | 2 +- checker/markdown.py | 46 +++++++++++++++++++++++++++------------------ 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/check_bandwidth.py b/check_bandwidth.py index 7c15d9a..682cf22 100755 --- a/check_bandwidth.py +++ b/check_bandwidth.py @@ -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) diff --git a/checker/markdown.py b/checker/markdown.py index d86cb42..73d6d93 100644 --- a/checker/markdown.py +++ b/checker/markdown.py @@ -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