Skip to content

Commit 55b691f

Browse files
authored
Merge pull request #29 from switchbox-data/13-add-heading-separator-to-utility-table-output
Add heading separator to utility table output and imporve padding (Cl…
2 parents 8c98e70 + 75a2fe4 commit 55b691f

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

tariff_fetch/cli.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,37 @@ def fmt_number(value: float | int | None) -> str:
7171
return "-"
7272
return f"{value:,.0f}"
7373

74+
utility_name_header = "Utility Name"
75+
entity_type_header = "Entity Type"
76+
sales_header = "Sales (MWh)"
77+
revenue_header = "Revenue ($)"
78+
customers_header = "Customers"
79+
80+
largest_utility_name = max(len(utility_name_header), *(len(row["utility_name"]) for row in rows))
81+
largest_entity_type = max(len(entity_type_header), *(len(row["entity_type"][:18]) for row in rows))
82+
largest_sales_col = max(len(sales_header), *(len(fmt_number(row["sales_mwh"])) for row in rows))
83+
largest_revenue_col = max(len(revenue_header), *(len(fmt_number(row["sales_revenue"])) for row in rows))
84+
largest_customers_col = max(len(customers_header), *(len(fmt_number(row["customers"])) for row in rows))
85+
86+
header_str_utility_name = utility_name_header.ljust(largest_utility_name)
87+
header_str_entity_type = entity_type_header.ljust(largest_entity_type)
88+
header_str_sales = sales_header.ljust(largest_sales_col)
89+
header_str_revenue = revenue_header.ljust(largest_revenue_col)
90+
header_str_customers = customers_header.ljust(largest_customers_col)
91+
header_str = f"{header_str_utility_name} | {header_str_entity_type} | {header_str_sales} | {header_str_revenue} | {header_str_customers}"
92+
separator = questionary.Separator(line="-" * len(header_str))
93+
7494
header = questionary.Choice(
75-
"Utility Name | Entity Type | Sales (MWh) | Revenue ($) | Customers",
95+
title=header_str,
7696
value=0,
7797
)
7898

7999
def build_choice(row: dict) -> questionary.Choice:
80-
name_col = f"{row['utility_name']:<44}"
81-
entity_type = f"{(row['entity_type'] or '-')[:18]:<18}"
82-
sales_col = f"{fmt_number(row.get('sales_mwh')):>12}"
83-
revenue_col = f"{fmt_number(row.get('sales_revenue')):>14}"
84-
customers_col = f"{fmt_number(row.get('customers')):>9}"
100+
name_col = row["utility_name"].ljust(largest_utility_name)
101+
entity_type = (row["entity_type"] or "-")[:18].ljust(largest_entity_type)
102+
sales_col = fmt_number(row["sales_mwh"]).ljust(largest_sales_col)
103+
revenue_col = fmt_number(row["sales_revenue"]).ljust(largest_revenue_col)
104+
customers_col = fmt_number(row["customers"]).ljust(largest_customers_col)
85105
title = f"{name_col} | {entity_type} | {sales_col} | {revenue_col} | {customers_col}"
86106
return questionary.Choice(
87107
title=title,
@@ -92,7 +112,7 @@ def build_choice(row: dict) -> questionary.Choice:
92112
while result == 0:
93113
result = questionary.select(
94114
message="Select a utility",
95-
choices=[header, *[build_choice(row) for row in rows]],
115+
choices=[header, separator, *[build_choice(row) for row in rows]],
96116
use_search_filter=True,
97117
use_jk_keys=False,
98118
use_shortcuts=False,

0 commit comments

Comments
 (0)