Restored --percent option, added baseline test

This commit is contained in:
John Wiegley 2009-06-21 18:07:27 +01:00
parent 7287aad336
commit 521b935aa8
9 changed files with 157 additions and 9 deletions

View file

@ -194,6 +194,10 @@ namespace {
return false;
}
value_t get_true(account_t&) {
return true;
}
value_t get_depth_spacer(account_t& account)
{
std::size_t depth = 0;
@ -217,6 +221,10 @@ namespace {
value_t get_wrapper(call_scope_t& scope) {
return (*Func)(find_scope<account_t>(scope));
}
value_t get_parent(account_t& account) {
return value_t(static_cast<scope_t *>(account.parent));
}
}
expr_t::ptr_op_t account_t::lookup(const string& name)
@ -243,9 +251,16 @@ expr_t::ptr_op_t account_t::lookup(const string& name)
return WRAP_FUNCTOR(get_wrapper<&get_depth_spacer>);
break;
case 'i':
if (name == "is_account")
return WRAP_FUNCTOR(get_wrapper<&get_true>);
break;
case 'p':
if (name == "partial_account")
return WRAP_FUNCTOR(get_partial_name);
else if (name == "parent")
return WRAP_FUNCTOR(get_wrapper<&get_parent>);
break;
case 's':

View file

@ -247,6 +247,10 @@ namespace {
return 0L;
}
value_t ignore(item_t&) {
return false;
}
template <value_t (*Func)(item_t&)>
value_t get_wrapper(call_scope_t& scope) {
return (*Func)(find_scope<item_t>(scope));
@ -330,6 +334,11 @@ expr_t::ptr_op_t item_t::lookup(const string& name)
return WRAP_FUNCTOR(ledger::has_tag);
break;
case 'i':
if (name == "is_account")
return WRAP_FUNCTOR(get_wrapper<&ignore>);
break;
case 'm':
if (name == "meta")
return WRAP_FUNCTOR(ledger::get_tag);
@ -343,6 +352,8 @@ expr_t::ptr_op_t item_t::lookup(const string& name)
case 'p':
if (name == "pending")
return WRAP_FUNCTOR(get_wrapper<&get_pending>);
else if (name == "parent")
return WRAP_FUNCTOR(get_wrapper<&ignore>);
break;
case 's':

View file

@ -166,6 +166,10 @@ value_t expr_t::op_t::calc(scope_t& scope, ptr_op_t * locus)
call_scope_t call_args(scope);
if (value_t obj = left()->left()->as_function()(call_args)) {
if (obj.is_pointer()) {
if (obj.as_pointer_lval<scope_t>() == NULL) {
throw_(calc_error,
_("Left operand of . operator is NULL"));
} else {
scope_t& objscope(obj.as_ref_lval<scope_t>());
if (ptr_op_t member = objscope.lookup(right()->as_ident())) {
result = member->calc(objscope);
@ -174,6 +178,7 @@ value_t expr_t::op_t::calc(scope_t& scope, ptr_op_t * locus)
}
}
}
}
if (right()->kind != IDENT)
throw_(calc_error,
_("Right operand of . operator must be an identifier"));

View file

@ -209,7 +209,8 @@ void format_accounts::flush()
foreach (account_t * account, posted_accounts)
displayed += post_account(*account);
if (! report.HANDLED(no_total) && displayed > 1) {
if (displayed > 1 &&
! report.HANDLED(no_total) && ! report.HANDLED(percent)) {
bind_scope_t bound_scope(report, *report.session.master);
separator_format.format(out, bound_scope);
total_line_format.format(out, bound_scope);

View file

@ -302,6 +302,8 @@ expr_t::ptr_op_t post_t::lookup(const string& name)
return WRAP_FUNCTOR(get_wrapper<&get_payee>);
else if (name == "primary")
return WRAP_FUNCTOR(get_wrapper<&get_commodity_is_primary>);
else if (name == "parent")
return WRAP_FUNCTOR(get_wrapper<&get_xact>);
break;
case 'r':

View file

@ -291,6 +291,13 @@ value_t report_t::fn_ansify_if(call_scope_t& scope)
}
}
value_t report_t::fn_percent(call_scope_t& scope)
{
interactive_t args(scope, "aa");
return (amount_t("100.00%") *
(args.get<amount_t>(0) / args.get<amount_t>(1)).number());
}
namespace {
value_t fn_black(call_scope_t&) {
return string_value("black");
@ -402,7 +409,7 @@ option_t<report_t> * report_t::lookup_option(const char * p)
{
switch (*p) {
case '%':
OPT_CH(percentage);
OPT_CH(percent);
break;
case 'A':
OPT_CH(average);
@ -568,7 +575,7 @@ option_t<report_t> * report_t::lookup_option(const char * p)
OPT(pager_);
else OPT(payee_as_account);
else OPT(pending);
else OPT(percentage);
else OPT(percent);
else OPT_(period_);
else OPT(period_sort_);
else OPT(plot_amount_format_);
@ -842,6 +849,8 @@ expr_t::ptr_op_t report_t::lookup(const string& name)
}
else if (is_eq(p, "post"))
return WRAP_FUNCTOR(fn_false);
else if (is_eq(p, "percent"))
return MAKE_FUNCTOR(report_t::fn_percent);
break;
case 'q':

View file

@ -155,6 +155,7 @@ public:
value_t fn_join(call_scope_t& scope);
value_t fn_format_date(call_scope_t& scope);
value_t fn_ansify_if(call_scope_t& scope);
value_t fn_percent(call_scope_t& scope);
#if 0
value_t fn_now(call_scope_t&) {
@ -248,7 +249,7 @@ public:
HANDLER(pager_).report(out);
HANDLER(payee_as_account).report(out);
HANDLER(pending).report(out);
HANDLER(percentage).report(out);
HANDLER(percent).report(out);
HANDLER(period_).report(out);
HANDLER(period_sort_).report(out);
HANDLER(plot_amount_format_).report(out);
@ -582,7 +583,11 @@ public:
parent->HANDLER(limit_).on(string("--pending"), "pending");
});
OPTION(report_t, percentage); // -%
OPTION_(report_t, percent, DO() { // -%
parent->HANDLER(total_)
.set_expr(string("--percent"),
"is_account&parent&parent.total&percent(total, parent.total)");
});
OPTION__
(report_t, period_, // -p

View file

@ -81,6 +81,12 @@ session_t::session_t()
commodity->add_flags(COMMODITY_BUILTIN | COMMODITY_NOMARKET);
else
assert(false);
// Add a "percentile" commodity
if (commodity_t * commodity = commodity_pool->create("%"))
commodity->add_flags(COMMODITY_BUILTIN | COMMODITY_NOMARKET);
else
assert(false);
}
std::size_t session_t::read_journal(std::istream& in,

View file

@ -0,0 +1,94 @@
bal --percent
<<<
2008/01/11 LIAT
Expenses:Travel:Airfare $40.00
Liabilities:MasterCard
2008/01/14 cheaptickets.com
Expenses:Travel:Airfare $182.19
Liabilities:MasterCard
2008/02/05 CTX
Expenses:Travel:Auto $240.38
Liabilities:MasterCard
2008/02/05 UNITED
Expenses:Travel:Airfare $238.80
Liabilities:MasterCard
2008/02/05 UNITED
Expenses:Travel:Airfare $238.80
Liabilities:MasterCard
2008/02/22 BUDGET RENT-A-CAR
Expenses:Travel:Auto $40.59
Liabilities:MasterCard
2008/03/16 IBERIA
Expenses:Travel:Airfare $1,231.60
Liabilities:MasterCard
2008/03/16 IBERIA
Expenses:Travel:Airfare $1,231.60
Liabilities:MasterCard
2008/04/03 AMERICAN
Expenses:Travel:Airfare $155.86
Liabilities:MasterCard
2008/04/03 AMERICAN
Expenses:Travel:Airfare $155.86
Liabilities:MasterCard
2008/04/30 UNITED
Expenses:Travel:Airfare $437.21
Liabilities:MasterCard
2008/04/30 UNITED
Expenses:Travel:Airfare $437.21
Liabilities:MasterCard
2008/08/08 BCIS I-131 FILING FEE-
Expenses:Travel:Passport $170.00
Liabilities:MasterCard
2008/09/06 AMERICAN
Expenses:Travel:Airfare $912.60
Liabilities:MasterCard
2008/09/06 AMERICAN
Expenses:Travel:Airfare $912.60
Liabilities:MasterCard
2008/09/22 AGNT FEE
Expenses:Travel:Airfare $70.00
Liabilities:MasterCard
2008/09/22 DELTA
Expenses:Travel:Airfare $806.20
Liabilities:MasterCard
2008/09/22 DELTA
Expenses:Travel:Airfare $806.20
Liabilities:MasterCard
2008/09/22 LIAT 1974 LIMITED
Expenses:Travel:Airfare $418.34
Liabilities:MasterCard
2008/12/26 U.S. Department of State
Expenses:Travel:Passport $127.00
Assets:Checking
2008/12/26 U.S. Department of State
Expenses:Travel:Passport $127.00
Assets:Checking
>>>1
100.00% Assets:Checking
100.00% Expenses:Travel
92.15% Airfare
3.13% Auto
4.72% Passport
100.00% Liabilities:MasterCard
>>>2
=== 0