Added --flat option, to flatten the balance report

This commit is contained in:
John Wiegley 2009-02-17 21:19:31 -04:00
parent 93d195f1d9
commit 4ec2dfeef1
7 changed files with 42 additions and 18 deletions

View file

@ -125,17 +125,19 @@ string account_t::fullname() const
} }
} }
string account_t::partial_name() const string account_t::partial_name(bool flat) const
{ {
string pname = name; string pname = name;
for (const account_t * acct = parent; for (const account_t * acct = parent;
acct && acct->parent; acct && acct->parent;
acct = acct->parent) { acct = acct->parent) {
std::size_t count = acct->children_with_flags(ACCOUNT_EXT_MATCHING); if (! flat) {
assert(count > 0); std::size_t count = acct->children_with_flags(ACCOUNT_EXT_MATCHING);
if (count > 1) assert(count > 0);
break; if (count > 1)
break;
}
pname = acct->name + ":" + pname; pname = acct->name + ":" + pname;
} }
return pname; return pname;
@ -148,8 +150,13 @@ std::ostream& operator<<(std::ostream& out, const account_t& account)
} }
namespace { namespace {
value_t get_partial_name(account_t& account) { value_t get_partial_name(call_scope_t& scope)
return string_value(account.partial_name()); {
account_t& account(find_scope<account_t>(scope));
var_t<bool> flatten(scope, 0);
return string_value(account.partial_name(flatten ? *flatten : false));
} }
value_t get_account(account_t& account) { // this gets the name value_t get_account(account_t& account) { // this gets the name
@ -238,7 +245,7 @@ expr_t::ptr_op_t account_t::lookup(const string& name)
case 'p': case 'p':
if (name == "partial_account") if (name == "partial_account")
return WRAP_FUNCTOR(get_wrapper<&get_partial_name>); return WRAP_FUNCTOR(get_partial_name);
break; break;
case 's': case 's':

View file

@ -97,7 +97,7 @@ class account_t : public scope_t
return fullname(); return fullname();
} }
string fullname() const; string fullname() const;
string partial_name() const; string partial_name(bool flat = false) const;
void add_account(account_t * acct) { void add_account(account_t * acct) {
accounts.insert(accounts_map::value_type(acct->name, acct)); accounts.insert(accounts_map::value_type(acct->name, acct));

View file

@ -222,11 +222,13 @@ std::size_t format_accounts::post_accounts(account_t& account)
DEBUG("account.display", "Should we display " << account.fullname()); DEBUG("account.display", "Should we display " << account.fullname());
if (account.has_flags(ACCOUNT_EXT_MATCHING) || if (account.has_flags(ACCOUNT_EXT_MATCHING) ||
account.children_with_flags(ACCOUNT_EXT_MATCHING) > 1) { (! flatten_list &&
account.children_with_flags(ACCOUNT_EXT_MATCHING) > 1)) {
DEBUG("account.display", " Yes, because it matched"); DEBUG("account.display", " Yes, because it matched");
format_account = true; format_account = true;
} }
else if (account.children_with_flags(ACCOUNT_EXT_VISITED) && else if (! flatten_list &&
account.children_with_flags(ACCOUNT_EXT_VISITED) &&
! account.children_with_flags(ACCOUNT_EXT_MATCHING)) { ! account.children_with_flags(ACCOUNT_EXT_MATCHING)) {
DEBUG("account.display", DEBUG("account.display",
" Maybe, because it has visited, but no matching, children"); " Maybe, because it has visited, but no matching, children");

View file

@ -163,13 +163,16 @@ protected:
report_t& report; report_t& report;
format_t format; format_t format;
item_predicate disp_pred; item_predicate disp_pred;
bool flatten_list;
public: public:
format_accounts(report_t& _report, format_accounts(report_t& _report,
const string& _format = "") const string& _format = "",
: report(_report), format(_format), disp_pred() bool _flatten_list = false)
: report(_report), format(_format), disp_pred(),
flatten_list(_flatten_list)
{ {
TRACE_CTOR(format_accounts, "report&, const string&, const bool"); TRACE_CTOR(format_accounts, "report&, const string&, bool");
if (report.HANDLED(display_)) { if (report.HANDLED(display_)) {
DEBUG("account.display", DEBUG("account.display",

View file

@ -73,7 +73,8 @@ report_t::report_t(session_t& _session)
HANDLER(balance_format_).on( HANDLER(balance_format_).on(
"%20(print_balance(strip(display_total), 20))" "%20(print_balance(strip(display_total), 20))"
" %(depth_spacer)%-(partial_account)\n"); " %(!options.flat ? depth_spacer : \"\")"
"%-(partial_account(options.flat))\n");
HANDLER(equity_format_).on("\n%D %Y%C%P\n%/ %-34W %12t\n"); HANDLER(equity_format_).on("\n%D %Y%C%P\n%/ %-34W %12t\n");
@ -404,7 +405,8 @@ option_t<report_t> * report_t::lookup_option(const char * p)
else OPT(equity_format_); else OPT(equity_format_);
break; break;
case 'f': case 'f':
OPT(forecast_); OPT(flat);
else OPT(forecast_);
else OPT(format_); else OPT(format_);
else OPT_ALT(head_, first_); else OPT_ALT(head_, first_);
break; break;
@ -524,8 +526,8 @@ expr_t::ptr_op_t report_t::lookup(const string& name)
if (*(p + 1) == '\0' || is_eq(p, "bal") || is_eq(p, "balance")) if (*(p + 1) == '\0' || is_eq(p, "bal") || is_eq(p, "balance"))
return expr_t::op_t::wrap_functor return expr_t::op_t::wrap_functor
(reporter<account_t, acct_handler_ptr, &report_t::accounts_report> (reporter<account_t, acct_handler_ptr, &report_t::accounts_report>
(new format_accounts(*this, report_format(HANDLER(balance_format_))), (new format_accounts(*this, report_format(HANDLER(balance_format_)),
*this)); HANDLED(flat)), *this));
break; break;
case 'c': case 'c':

View file

@ -333,6 +333,7 @@ public:
}); });
OPTION(report_t, equity_format_); OPTION(report_t, equity_format_);
OPTION(report_t, flat);
OPTION(report_t, forecast_); OPTION(report_t, forecast_);
OPTION(report_t, format_); // -F OPTION(report_t, format_); // -F
OPTION(report_t, gain); // -G OPTION(report_t, gain); // -G

View file

@ -344,6 +344,15 @@ public:
} }
}; };
template <>
inline bool var_t<bool>::operator *() {
return value->to_boolean();
}
template <>
inline bool var_t<bool>::operator *() const {
return value->to_boolean();
}
template <> template <>
inline long var_t<long>::operator *() { inline long var_t<long>::operator *() {
return value->to_long(); return value->to_long();