From 8e530351ec76ee626ac6207c41f944939ffce9b6 Mon Sep 17 00:00:00 2001 From: Taylor Monacelli Date: Wed, 20 Nov 2024 18:55:37 -0800 Subject: [PATCH] adjust column widths based off header widths --- src/americansouth/display.py | 71 ++++++++++++++++++++++++++++-------- 1 file changed, 55 insertions(+), 16 deletions(-) diff --git a/src/americansouth/display.py b/src/americansouth/display.py index 6815738..f733ff5 100644 --- a/src/americansouth/display.py +++ b/src/americansouth/display.py @@ -6,26 +6,67 @@ class DisplayManager: def __init__(self) -> None: + self.formatter: DataFormatter = DataFormatter() + self.headers: typing.Dict[str, str] = { + "daily": "DAILY USED", + "budget": "DAILY BUDGET", + "total": "BANDWIDTH USED", + "monthly": "REMAINING BUDGET", + "total_avail": "MONTHLY BUDGET", + "fetch": "DATA FETCH TIME", + } self.col_width: typing.Dict[str, int] = { - "daily": 12, - "budget": 12, - "total": 12, - "monthly": 14, - "fetch": 22, - "total_avail": 10, + "daily": len(self.headers["daily"]), + "budget": len(self.headers["budget"]), + "total": len(self.headers["total"]), + "monthly": len(self.headers["monthly"]), + "total_avail": len(self.headers["total_avail"]), + "fetch": len(self.headers["fetch"]), } - self.formatter: DataFormatter = DataFormatter() + + def update_widths( + self, + records: typing.List[ + typing.Tuple[float, float, datetime.datetime, float, float] + ], + ) -> None: + prev_amount = 0 + for record in records: + _, amount, scraped_at, daily_limit, total = record + daily_used = amount - prev_amount + remaining = total - amount + + daily_str = f"{int(daily_used)}GB" + budget_str = self.formatter.format_gb(daily_limit) + total_str = f"{int(amount)}GB" + remaining_str = f"{int(remaining)}GB" + total_avail_str = f"{int(total)}GB" + fetch_time = self.formatter.format_time(scraped_at) + + self.col_width["daily"] = max(self.col_width["daily"], len(daily_str)) + self.col_width["budget"] = max(self.col_width["budget"], len(budget_str)) + self.col_width["total"] = max(self.col_width["total"], len(total_str)) + self.col_width["monthly"] = max( + self.col_width["monthly"], len(remaining_str) + ) + self.col_width["total_avail"] = max( + self.col_width["total_avail"], len(total_avail_str) + ) + self.col_width["fetch"] = max(self.col_width["fetch"], len(fetch_time)) + + prev_amount = amount def print_headers(self) -> None: print( - f"{'DAILY USED':>{self.col_width['daily']}} " - f"{'DAILY BUDGET':>{self.col_width['budget']}} " - f"{'TOTAL USED':>{self.col_width['total']}} " - f"{'MONTHLY BUDGET':>{self.col_width['monthly']}} " - f"{'TOTAL':>{self.col_width['total_avail']}} " - f"{'DATA FETCH TIME':<{self.col_width['fetch']}}" + f"{self.headers['daily']:>{self.col_width['daily']}} " + f"{self.headers['budget']:>{self.col_width['budget']}} " + f"{self.headers['total']:>{self.col_width['total']}} " + f"{self.headers['monthly']:>{self.col_width['monthly']}} " + f"{self.headers['total_avail']:>{self.col_width['total_avail']}} " + f"{self.headers['fetch']:<{self.col_width['fetch']}}" ) - print("-" * 92) + total_width = sum(self.col_width.values()) + len(self.col_width) - 1 + print("-" * total_width) def print_record( self, @@ -35,13 +76,11 @@ def print_record( _, amount, scraped_at, daily_limit, total = record daily_used: float = amount - prev_amount remaining: float = total - amount - daily_str: str = f"{int(daily_used)}GB" total_str: str = f"{int(amount)}GB" remaining_str: str = f"{int(remaining)}GB" total_avail_str: str = f"{int(total)}GB" fetch_time: str = self.formatter.format_time(scraped_at) - print( f"{daily_str:>{self.col_width['daily']}} " f"{self.formatter.format_gb(daily_limit):>{self.col_width['budget']}} "