From f9e553c61f9f970b0008e335cddc0e96a581d0eb Mon Sep 17 00:00:00 2001 From: Ivan Habunek Date: Sat, 27 Apr 2024 19:58:02 +0200 Subject: [PATCH] Support styling in print_table --- twitchdl/output.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/twitchdl/output.py b/twitchdl/output.py index 6bce7c7..12b843c 100644 --- a/twitchdl/output.py +++ b/twitchdl/output.py @@ -25,15 +25,24 @@ def print_log(message: Any): 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]]): - 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)] underlines = ["-" * width for width in widths] def print_row(row: List[str]): for idx, cell in enumerate(row): width = widths[idx] - click.echo(cell.ljust(width), nl=False) + click.echo(ljust(cell, width), nl=False) click.echo(" ", nl=False) click.echo()