Skip to content

Instantly share code, notes, and snippets.

@simonLeary42
Created February 9, 2026 15:20
Show Gist options
  • Select an option

  • Save simonLeary42/dedc1887c445d7b98db9c2e7e9af9fc5 to your computer and use it in GitHub Desktop.

Select an option

Save simonLeary42/dedc1887c445d7b98db9c2e7e9af9fc5 to your computer and use it in GitHub Desktop.
def fmt_table(
table, between_column_padding_size=5, alternate_brightness=True, left_padding_size=0
):
output_lines = []
assert all(
len(row) <= len(table[0]) for row in table
), "no row can have more elements than the header row"
column_widths = [0] * len(table[0])
for row in table:
for i, element in enumerate(row):
if len(str(element)) > column_widths[i]:
column_widths[i] = len(str(element))
column_widths = [x + between_column_padding_size for x in column_widths]
header = ""
for i, column_header in enumerate(table[0]):
if i > 0:
header += "|"
header += str(column_header).center(column_widths[i] - 1) # minus one for the '|'
output_lines.append(header)
output_lines.append("".join(["="] * len(header)))
for row in table[1:]:
if left_padding_size > 0:
line = " " * left_padding_size
else:
line = ""
for i, value in enumerate(row):
line = line + str(value).ljust(column_widths[i])
output_lines.append(line)
if alternate_brightness:
bright = "\033[0;1m"
reset = "\033[0m"
for i, line in enumerate(output_lines):
if i <= 1:
continue # skip first 2 lines
if i % 2 == 0:
output_lines[i] = bright + line + reset
return output_lines
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment