INNER CODE UNIT · Python
__print_tabular
dhondta/python-codext · src/codext/__init__.py:52
def __print_tabular(lst, space=4):
try:
cols, _ = os.get_terminal_size()
# first, convert the list to a table that fits into the terminal
i, line, w = 0, "", []
while i < len(lst):
x = lst[i]
l = len(x)
col = "%-{}s".format(l + space) % x
i += 1
w.append(l)
if len(line) + len(col) > cols:
break
line += col
while True:
t = [lst[j:j+i] for j in range(0, len(lst), i)]
w = [max(0 if j+k*i >= len(lst) else len(lst[j+k*i]) for k in range(len(t))) for j, _ in enumerate(w)]
if sum(w) + space * len(w) >= cols: