> # this one function looks simple but it took so fucking long
Heh. I can't count how many times I've written a column-aligning functions in various programming languages, and each time it is a pain. And it sounds simple in my head - just get max-length of each column, and add spaces up to the next multiple of tab-size.
But even in Python with f-strings and all the fancy padding stuff it has, it is ends up a convoluted, unreadable mess:
# randomwordgenerator.com
table = [
['agony', 'kick', 'pump'],
['frown', 'lonely', 'mutation'],
['sail', 'tasty', 'want'],
]
tab_width = 4
n_rows, n_cols = len(table), len(table[0])
max_width = [
max(len(table[r][c]) for r in range(n_rows))
for c in range(n_cols)
]
for r in range(n_rows):
for c in range(n_cols):
item = table[r][c]
if c == n_cols - 1:
# do not print space after last item
print(item)
else:
# only print newline after last item
# EDIT: found a bug here after commenting...
# width = int(((max_width[c] + 1) / tab_width) * tab_width)
width = ((max_width[c] // tab_width) + 1) * tab_width
print(f'{item:<{width}}', end='')
Even while writing this code for the comment for the hundredth time, I've had to fix at least 5 bugs! Truly horrible.