Only display a final balance total if necessary

In the case where only one top-level account is being reported, there is
no reason to duplicate the total balance shown.
This commit is contained in:
John Wiegley 2009-02-17 19:48:42 -04:00
parent 07f4aefdfd
commit 93d195f1d9
3 changed files with 38 additions and 31 deletions

View file

@ -118,12 +118,12 @@ class account_t : public scope_t
struct xdata_t : public supports_flags<>
{
#define ACCOUNT_EXT_DISPLAYED 0x01
#define ACCOUNT_EXT_SORT_CALC 0x02
#define ACCOUNT_EXT_HAS_NON_VIRTUALS 0x04
#define ACCOUNT_EXT_HAS_UNB_VIRTUALS 0x08
#define ACCOUNT_EXT_VISITED 0x10
#define ACCOUNT_EXT_MATCHING 0x20
#define ACCOUNT_EXT_SORT_CALC 0x01
#define ACCOUNT_EXT_HAS_NON_VIRTUALS 0x02
#define ACCOUNT_EXT_HAS_UNB_VIRTUALS 0x04
#define ACCOUNT_EXT_VISITED 0x08
#define ACCOUNT_EXT_MATCHING 0x10
#define ACCOUNT_EXT_DISPLAYED 0x20
value_t value;
value_t total;

View file

@ -210,8 +210,10 @@ void format_entries::operator()(xact_t& xact)
last_entry = xact.entry;
}
void format_accounts::post_accounts(account_t& account)
std::size_t format_accounts::post_accounts(account_t& account)
{
std::size_t displayed = 0;
// Don't ever print the top-most account
if (account.parent) {
bind_scope_t bound_scope(report, account);
@ -242,30 +244,36 @@ void format_accounts::post_accounts(account_t& account)
" No, neither it nor its children were eligible for display");
}
if (format_account)
if (format_account) {
account.xdata().add_flags(ACCOUNT_EXT_DISPLAYED);
displayed++;
format.format(report.output_stream, bound_scope);
}
}
foreach (accounts_map::value_type pair, account.accounts)
post_accounts(*pair.second);
foreach (accounts_map::value_type pair, account.accounts) {
if (post_accounts(*pair.second) > 0)
displayed++;
}
return displayed;
}
void format_accounts::flush()
{
std::ostream& out(report.output_stream);
post_accounts(*report.session.master.get());
std::size_t top_displayed = post_accounts(*report.session.master.get());
if (print_final_total) {
assert(report.session.master->has_xdata());
account_t::xdata_t& xdata(report.session.master->xdata());
assert(report.session.master->has_xdata());
account_t::xdata_t& xdata(report.session.master->xdata());
if (! report.HANDLED(collapse) && xdata.total) {
out << "--------------------\n";
xdata.value = xdata.total;
bind_scope_t bound_scope(report, *report.session.master);
format.format(out, bound_scope);
}
if (top_displayed > 1 && ! report.HANDLED(collapse) && xdata.total) {
out << "--------------------\n";
xdata.value = xdata.total;
bind_scope_t bound_scope(report, *report.session.master);
format.format(out, bound_scope);
}
out.flush();
@ -341,12 +349,12 @@ void format_equity::flush()
out.flush();
}
void format_equity::post_accounts(account_t& account)
std::size_t format_equity::post_accounts(account_t& account)
{
std::ostream& out(report.output_stream);
if (! account.has_flags(ACCOUNT_EXT_MATCHING))
return;
return 0;
value_t val = account.xdata().value;
@ -368,6 +376,8 @@ void format_equity::post_accounts(account_t& account)
next_lines_format.format(out, bound_scope);
}
total += val;
return 1;
}
} // namespace ledger

View file

@ -163,14 +163,11 @@ protected:
report_t& report;
format_t format;
item_predicate disp_pred;
bool print_final_total;
public:
format_accounts(report_t& _report,
const string& _format = "",
const bool _print_final_total = true)
: report(_report), format(_format), disp_pred(),
print_final_total(_print_final_total)
const string& _format = "")
: report(_report), format(_format), disp_pred()
{
TRACE_CTOR(format_accounts, "report&, const string&, const bool");
@ -184,8 +181,8 @@ public:
TRACE_DTOR(format_accounts);
}
virtual void post_accounts(account_t& account);
virtual void flush();
virtual std::size_t post_accounts(account_t& account);
virtual void flush();
virtual void operator()(account_t& account);
};
@ -209,8 +206,8 @@ class format_equity : public format_accounts
TRACE_DTOR(format_equity);
}
virtual void flush();
virtual void post_accounts(account_t& account);
virtual std::size_t post_accounts(account_t& account);
virtual void flush();
};
} // namespace ledger