From 9d9254b69a62e86f0a4da4fc13f271e9bfe45393 Mon Sep 17 00:00:00 2001 From: Brett Smith Date: Sun, 17 Dec 2017 21:05:22 -0500 Subject: [PATCH] template: Nicer formatting of account split lines. This provides nicer output when the whole line can fit in the desired width, even if the account name or amount are unusually long. --- import2ledger/template.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/import2ledger/template.py b/import2ledger/template.py index 97271dd..90b7595 100644 --- a/import2ledger/template.py +++ b/import2ledger/template.py @@ -88,6 +88,10 @@ class AmountTokenTransformer(TokenTransformer): class AccountSplitter: + TARGET_LINE_LEN = 78 + # -4 because that's how many spaces prefix an account line. + TARGET_ACCTLINE_LEN = TARGET_LINE_LEN - 4 + def __init__(self, signed_currencies, signed_currency_fmt, unsigned_currency_fmt, template_name): self.splits = collections.OrderedDict() @@ -158,12 +162,13 @@ class AccountSplitter: if amount == 0: yield '' else: - account_s = ' {:45} {:>19}\n'.format( - account.format_map(template_vars), - babel.numbers.format_currency(amount, template_vars['currency'], amt_fmt), + account_s = account.format_map(template_vars) + amount_s = babel.numbers.format_currency(amount, template_vars['currency'], amt_fmt) + sep_len = max(2, self.TARGET_ACCTLINE_LEN - len(account_s) - len(amount_s)) + yield ' {}{}{}\n{}'.format( + account_s, ' ' * sep_len, amount_s, + metadata.format_map(template_vars), ) - metadata_s = metadata.format_map(template_vars) - yield account_s + metadata_s def render_next(self, template_vars): if template_vars is not self._last_template_vars: