Commit 94242be2 authored by Ludovic LANGE's avatar Ludovic LANGE

Fix cosmetic issue in account listing

When you have long id names, or long account names, or both, you may
have some formatting issue where balance and coming values are not properly
aligned.
This patch fixes this alignment issue, and allows the possibility to
easily change the column width in the future.
(If there is interest in it, it's certainly possible to fetch column width
either from cli parameters, configuration or environment variables)
parent 7531c00a
Pipeline #2084 passed with stages
in 20 minutes and 1 second
......@@ -384,28 +384,37 @@ class AccountListFormatter(IFormatter):
MANDATORY_FIELDS = ('id', 'label', 'balance', 'coming', 'type')
totals = {}
col_id_width = 30
col_alias_width = 15
col_label_width = 26
col_balance_width = 10
col_coming_width = 10
def start_format(self, **kwargs):
self.output(' %s Account Balance Coming ' % ((' ' * 15) if not self.interactive else ''))
self.output('------------------------------------------%s+----------+----------' % (('-' * 15) if not self.interactive else ''))
if self.interactive:
first_col_width = 1 + self.col_alias_width + self.col_label_width
else:
first_col_width = 1 + self.col_id_width + self.col_label_width
self.output('%s %s %s' % ('Account'.center(first_col_width), 'Balance'.center(self.col_balance_width), 'Coming'.center(self.col_coming_width)))
self.output('%s+%s+%s' % (('-' * first_col_width), ('-' * self.col_balance_width), ('-' * self.col_coming_width)))
def format_obj(self, obj, alias):
if alias is not None:
# Magic number 6 is the size of : id (3 chars) + ' ' + '(' + ')'
id = '%s (%s)' % (self.colored('%3s' % ('#' + alias), 'red', 'bold'),
self.colored(obj.backend, 'blue', 'bold'))
clean = '#%s (%s)' % (alias, obj.backend)
if len(clean) < 15:
id += (' ' * (15 - len(clean)))
self.colored(obj.backend[:(self.col_alias_width-6)], 'blue', 'bold'))
clean = '#%3s (%s)' % (alias, obj.backend[:(self.col_alias_width-6)])
if len(clean) < self.col_alias_width:
id += (' ' * (self.col_alias_width - len(clean) + 1))
else:
id = self.colored('%30s' % obj.fullid, 'red', 'bold')
id = self.colored('%*s' % (self.col_id_width, obj.fullid[:self.col_id_width]), 'red', 'bold')
balance = obj.balance or Decimal('0')
coming = obj.coming or Decimal('0')
currency = obj.currency or 'EUR'
result = u'%s %s %s %s' % (id,
self.colored('%-25s' % obj.label[:25], 'yellow' if obj.type != Account.TYPE_LOAN else 'blue'),
self.colored('%9.2f' % obj.balance, 'green' if balance >= 0 else 'red') if not empty(obj.balance) else ' ' * 9,
self.colored('%9.2f' % obj.coming, 'green' if coming >= 0 else 'red') if not empty(obj.coming) else '')
result = u'%s %s %s %s' % (id,
self.colored('%-*s' % (self.col_label_width, obj.label[:self.col_label_width]), 'yellow' if obj.type != Account.TYPE_LOAN else 'blue'),
self.colored('%*.2f' % (self.col_balance_width, obj.balance), 'green' if balance >= 0 else 'red') if not empty(obj.balance) else ' ' * 9,
self.colored('%*.2f' % (self.col_coming_width, obj.coming), 'green' if coming >= 0 else 'red') if not empty(obj.coming) else '')
currency_totals = self.totals.setdefault(currency, {})
currency_totals.setdefault('balance', Decimal(0))
......@@ -417,16 +426,21 @@ class AccountListFormatter(IFormatter):
return result
def flush(self):
self.output(u'------------------------------------------%s+----------+----------' % (('-' * 15) if not self.interactive else ''))
if self.interactive:
first_col_width = 1 + self.col_alias_width + self.col_label_width
else:
first_col_width = 1 + self.col_id_width + self.col_label_width
self.output('%s+%s+%s' % (('-' * first_col_width), ('-' * self.col_balance_width), ('-' * self.col_coming_width)))
for currency, currency_totals in sorted(self.totals.items(), key=lambda k_v: (k_v[1]['balance'], k_v[1]['coming'], k_v[0])):
balance = currency_totals['balance']
coming = currency_totals['coming']
self.output(u'%s Total (%s) %s %s' % (
(' ' * 15) if not self.interactive else '',
currency,
self.colored('%8.2f' % balance, 'green' if balance >= 0 else 'red'),
self.colored('%8.2f' % coming, 'green' if coming >= 0 else 'red'))
# Magic number 12 is the size of 'Total (xxx) '
self.output(u'%sTotal (%s) %s %s' % (
(' ' * (first_col_width - 12)),
currency[:3],
self.colored('%*.2f' % (self.col_balance_width, balance), 'green' if balance >= 0 else 'red'),
self.colored('%*.2f' % (self.col_coming_width, coming), 'green' if coming >= 0 else 'red'))
)
self.totals.clear()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment