Support styling in print_table

This commit is contained in:
Ivan Habunek 2024-04-27 19:58:02 +02:00
parent 4fac6c11c5
commit f9e553c61f
No known key found for this signature in database
GPG Key ID: F5F0623FF5EBCB3D

View File

@ -25,15 +25,24 @@ def print_log(message: Any):
click.secho(message, err=True, dim=True) click.secho(message, err=True, dim=True)
def visual_len(text: str):
return len(click.unstyle(text))
def ljust(text: str, width: int):
diff = width - visual_len(text)
return text + (" " * diff) if diff > 0 else text
def print_table(headers: List[str], data: List[List[str]]): def print_table(headers: List[str], data: List[List[str]]):
widths = [[len(cell) for cell in row] for row in data + [headers]] widths = [[visual_len(cell) for cell in row] for row in data + [headers]]
widths = [max(width) for width in zip(*widths)] widths = [max(width) for width in zip(*widths)]
underlines = ["-" * width for width in widths] underlines = ["-" * width for width in widths]
def print_row(row: List[str]): def print_row(row: List[str]):
for idx, cell in enumerate(row): for idx, cell in enumerate(row):
width = widths[idx] width = widths[idx]
click.echo(cell.ljust(width), nl=False) click.echo(ljust(cell, width), nl=False)
click.echo(" ", nl=False) click.echo(" ", nl=False)
click.echo() click.echo()