Skip to content

Commit

Permalink
wip: better formatting for cli output
Browse files Browse the repository at this point in the history
TODO:
- currency col needs to be right aligned
- currency col needs thousands separator
- currency col needs optional rounding
  • Loading branch information
redstreet committed Oct 11, 2024
1 parent 86650a2 commit ed2e60c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
8 changes: 5 additions & 3 deletions fava_miler/common/clicommon.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
tabulate.PRESERVE_WHITESPACE = True


def pretty_print_table(rtypes, rrows):
def pretty_print_table(rtypes, rrows, **tabulate_options):
headers = [i[0] for i in rtypes]
tabulate_options.setdefault('tablefmt', 'simple')
print(tabulate.tabulate(rrows,
headers=headers[1:],
tablefmt='simple'))
headers=headers[1:], # Skip the first header as per your logic
**tabulate_options))



def pretty_print_table_bare(rrows):
Expand Down
6 changes: 5 additions & 1 deletion fava_miler/libmiler.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/env python3

from decimal import ROUND_HALF_UP
from beancount.core.number import Decimal
from beancount.core.amount import Amount
import collections
Expand Down Expand Up @@ -50,12 +51,15 @@ def get_miles_metadata(miles):
except (KeyError, AttributeError):
return {}

def rnd(n):
return n.quantize(Decimal('1'), rounding=ROUND_HALF_UP)

ret_rows = []
for row in rrows:
meta = get_miles_metadata(row.points)

value = meta.get('points-value', Amount(Decimal(0), 'NONE'))
converted_value = Amount(value.number * row.balance, value.currency)
converted_value = Amount(rnd(value.number * row.balance), value.currency)

expiry_months = meta.get('expiry-months', 0)
if expiry_months >= 0:
Expand Down
2 changes: 1 addition & 1 deletion fava_miler/miler.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def miler(beancount_file, accounts_pattern, exclude_currencies):
"""Beancount Miles and Rewards Manager"""
accapi = api.AccAPI(beancount_file, locals())
result = libmiler.get_miles_expirations(accapi, accapi.options)
pretty_print_table(*result)
pretty_print_table(*result, floatfmt=",.0f")


if __name__ == '__main__':
Expand Down

0 comments on commit ed2e60c

Please sign in to comment.