summaryrefslogtreecommitdiff
path: root/src/report.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/report.cc')
-rw-r--r--src/report.cc140
1 files changed, 82 insertions, 58 deletions
diff --git a/src/report.cc b/src/report.cc
index be1e47ee..4c8a40e6 100644
--- a/src/report.cc
+++ b/src/report.cc
@@ -55,12 +55,15 @@ void report_t::posts_report(post_handler_ptr handler)
void report_t::generate_report(post_handler_ptr handler)
{
- HANDLER(limit_).on("actual"); // jww (2009-02-27): make this more general
+ // jww (2009-02-27): make this more general
+ HANDLER(limit_).on(string("#generate"), "actual");
+
generate_posts_iterator walker
(session, HANDLED(seed_) ?
static_cast<unsigned int>(HANDLER(seed_).value.to_long()) : 0,
HANDLED(head_) ?
static_cast<unsigned int>(HANDLER(head_).value.to_long()) : 50);
+
pass_down_posts(chain_post_handlers(*this, handler), walker);
}
@@ -126,33 +129,20 @@ value_t report_t::fn_market(call_scope_t& scope)
{
interactive_t args(scope, "a&ts");
- if (args.has(2)) {
- scoped_array<char> buf(new char[args.get<string>(2).length() + 1]);
- std::strcpy(buf.get(), args.get<string>(2).c_str());
-
- for (char * p = std::strtok(buf.get(), ",");
- p;
- p = std::strtok(NULL, ",")) {
- if (commodity_t * commodity = amount_t::current_pool->find(trim_ws(p))) {
- DEBUG("report.market", "Searching for value of " << args.value_at(0)
- << " in terms of commodity " << commodity->symbol());
- value_t result =
- args.value_at(0).value(false, args.has(1) ?
+ value_t result;
+ optional<datetime_t> moment = (args.has(1) ?
args.get<datetime_t>(1) :
- optional<datetime_t>(), *commodity);
- if (! result.is_null()) {
- DEBUG("report.market", "Market value is = " << result);
- return result;
- }
- }
- }
- } else {
- value_t result =
- args.value_at(0).value(true, args.has(1) ?
- args.get<datetime_t>(1) : optional<datetime_t>());
- if (! result.is_null())
- return result;
- }
+ optional<datetime_t>());
+ if (args.has(2))
+ result = args.value_at(0).exchange_commodities(args.get<string>(2),
+ /* add_prices= */ false,
+ moment);
+ else
+ result = args.value_at(0).value(true, moment);
+
+ if (! result.is_null())
+ return result;
+
return args.value_at(0);
}
@@ -227,13 +217,14 @@ value_t report_t::fn_truncated(call_scope_t& scope)
value_t report_t::fn_justify(call_scope_t& scope)
{
- interactive_t args(scope, "vl&lbs");
+ interactive_t args(scope, "vl&lbbs");
std::ostringstream out;
args.value_at(0)
.print(out, args.get<long>(1),
args.has(2) ? args.get<long>(2) : -1,
- args.has(3),
- args.has(4) ? args.get<string>(4) :
+ args.has(3) ? args.get<bool>(3) : false,
+ args.has(4) ? args.get<bool>(4) : false,
+ args.has(5) ? args.get<string>(5) :
(HANDLED(date_format_) ?
HANDLER(date_format_).str() : optional<string>()));
return string_value(out.str());
@@ -255,22 +246,26 @@ value_t report_t::fn_quoted(call_scope_t& args)
return string_value(out.str());
}
-value_t report_t::fn_join(call_scope_t& args)
+value_t report_t::fn_join(call_scope_t& scope)
{
+ interactive_t args(scope, "s");
+
std::ostringstream out;
- foreach (const char ch, args[0].to_string())
+ foreach (const char ch, args.get<string>(0)) {
if (ch != '\n')
out << ch;
else
out << "\\n";
-
+ }
return string_value(out.str());
}
-value_t report_t::fn_format_date(call_scope_t& args)
+value_t report_t::fn_format_date(call_scope_t& scope)
{
- return string_value(format_date(args[0].to_date(), args[1].to_string()));
+ interactive_t args(scope, "ds");
+ return string_value(format_date(args.get<date_t>(0),
+ args.get<string>(1)));
}
value_t report_t::fn_ansify_if(call_scope_t& scope)
@@ -299,6 +294,19 @@ 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());
+}
+
+value_t report_t::fn_price(call_scope_t& scope)
+{
+ interactive_t args(scope, "v");
+ return args.value_at(0).price();
+}
+
namespace {
value_t fn_black(call_scope_t&) {
return string_value("black");
@@ -349,10 +357,12 @@ namespace {
shared_ptr<item_handler<Type> > handler;
report_t& report;
+ string whence;
public:
- reporter(item_handler<Type> * _handler, report_t& _report)
- : handler(_handler), report(_report) {}
+ reporter(item_handler<Type> * _handler, report_t& _report,
+ const string& _whence)
+ : handler(_handler), report(_report), whence(_whence) {}
value_t operator()(call_scope_t& args)
{
@@ -365,7 +375,7 @@ namespace {
string limit = args_to_predicate_expr(begin, end);
if (! limit.empty())
- report.HANDLER(limit_).on(limit);
+ report.HANDLER(limit_).on(whence, limit);
DEBUG("report.predicate",
"Predicate = " << report.HANDLER(limit_).str());
@@ -375,7 +385,7 @@ namespace {
display = args_to_predicate_expr(begin, end);
if (! display.empty())
- report.HANDLER(display_).on(display);
+ report.HANDLER(display_).on(whence, display);
DEBUG("report.predicate",
"Display predicate = " << report.HANDLER(display_).str());
@@ -408,7 +418,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);
@@ -473,9 +483,6 @@ option_t<report_t> * report_t::lookup_option(const char * p)
case 'Y':
OPT_CH(yearly);
break;
- case 'Z':
- OPT_CH(price_exp_);
- break;
case 'a':
OPT(abbrev_len_);
else OPT(account_);
@@ -556,7 +563,6 @@ option_t<report_t> * report_t::lookup_option(const char * p)
else OPT(lots);
else OPT(lots_actual);
else OPT_ALT(tail_, last_);
- else OPT_ALT(price_exp_, leeway_);
break;
case 'm':
OPT(market);
@@ -564,6 +570,7 @@ option_t<report_t> * report_t::lookup_option(const char * p)
break;
case 'n':
OPT_CH(collapse);
+ else OPT(no_color);
else OPT(no_total);
break;
case 'o':
@@ -574,13 +581,12 @@ 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_ALT(sort_xacts_, period_sort_);
else OPT(plot_amount_format_);
else OPT(plot_total_format_);
else OPT(price);
- else OPT(price_exp_);
else OPT(prices_format_);
else OPT(pricesdb_format_);
else OPT(print_format_);
@@ -598,11 +604,14 @@ option_t<report_t> * report_t::lookup_option(const char * p)
else OPT(related_all);
else OPT(revalued);
else OPT(revalued_only);
+#if 0
+ // This option is not available to users
+ else OPT(revalued_total_);
+#endif
break;
case 's':
OPT(set_account_);
else OPT(set_payee_);
- else OPT(set_price_);
else OPT(sort_);
else OPT(sort_all_);
else OPT(sort_xacts_);
@@ -675,7 +684,7 @@ expr_t::ptr_op_t report_t::lookup(const string& name)
return expr_t::op_t::wrap_functor
(reporter<account_t, acct_handler_ptr, &report_t::accounts_report>
(new format_accounts(*this, report_format(HANDLER(balance_format_))),
- *this));
+ *this, "#balance"));
break;
case 'c':
@@ -683,7 +692,7 @@ expr_t::ptr_op_t report_t::lookup(const string& name)
return WRAP_FUNCTOR
(reporter<>
(new format_posts(*this, report_format(HANDLER(csv_format_))),
- *this));
+ *this, "#csv"));
break;
case 'e':
@@ -691,12 +700,12 @@ expr_t::ptr_op_t report_t::lookup(const string& name)
return WRAP_FUNCTOR
(reporter<>
(new format_posts(*this, report_format(HANDLER(print_format_))),
- *this));
- else if (is_eq(q, "xact") || is_eq(q, "entry"))
+ *this, "#equity"));
+ else if (is_eq(q, "entry"))
return WRAP_FUNCTOR(xact_command);
else if (is_eq(q, "emacs"))
return WRAP_FUNCTOR
- (reporter<>(new format_emacs_posts(output_stream), *this));
+ (reporter<>(new format_emacs_posts(output_stream), *this, "#emacs"));
break;
case 'p':
@@ -704,17 +713,17 @@ expr_t::ptr_op_t report_t::lookup(const string& name)
return WRAP_FUNCTOR
(reporter<>
(new format_posts(*this, report_format(HANDLER(print_format_)),
- HANDLED(raw)), *this));
+ HANDLED(raw)), *this, "#print"));
else if (is_eq(q, "prices"))
return expr_t::op_t::wrap_functor
(reporter<post_t, post_handler_ptr, &report_t::commodities_report>
(new format_posts(*this, report_format(HANDLER(prices_format_))),
- *this));
+ *this, "#prices"));
else if (is_eq(q, "pricesdb"))
return expr_t::op_t::wrap_functor
(reporter<post_t, post_handler_ptr, &report_t::commodities_report>
(new format_posts(*this, report_format(HANDLER(pricesdb_format_))),
- *this));
+ *this, "#pricesdb"));
else if (is_eq(q, "python") && maybe_import("ledger.interp"))
return session.lookup(string(CMD_PREFIX) + "python");
break;
@@ -724,7 +733,7 @@ expr_t::ptr_op_t report_t::lookup(const string& name)
return WRAP_FUNCTOR
(reporter<>
(new format_posts(*this, report_format(HANDLER(register_format_))),
- *this));
+ *this, "#register"));
else if (is_eq(q, "reload"))
return MAKE_FUNCTOR(report_t::reload_command);
break;
@@ -736,6 +745,11 @@ expr_t::ptr_op_t report_t::lookup(const string& name)
if (is_eq(q, "server") && maybe_import("ledger.server"))
return session.lookup(string(CMD_PREFIX) + "server");
break;
+
+ case 'x':
+ if (is_eq(q, "xact"))
+ return WRAP_FUNCTOR(xact_command);
+ break;
}
}
else if (is_eq(p, "cyan"))
@@ -747,6 +761,8 @@ expr_t::ptr_op_t report_t::lookup(const string& name)
return MAKE_FUNCTOR(report_t::fn_display_amount);
else if (is_eq(p, "display_total"))
return MAKE_FUNCTOR(report_t::fn_display_total);
+ else if (is_eq(p, "date"))
+ return MAKE_FUNCTOR(report_t::fn_now);
break;
case 'f':
@@ -783,6 +799,8 @@ expr_t::ptr_op_t report_t::lookup(const string& name)
case 'n':
if (is_eq(p, "null"))
return WRAP_FUNCTOR(fn_null);
+ else if (is_eq(p, "now"))
+ return MAKE_FUNCTOR(report_t::fn_now);
break;
case 'o':
@@ -815,7 +833,7 @@ expr_t::ptr_op_t report_t::lookup(const string& name)
return expr_t::op_t::wrap_functor
(reporter<post_t, post_handler_ptr, &report_t::generate_report>
(new format_posts(*this, report_format(HANDLER(print_format_)),
- false), *this));
+ false), *this, "#generate"));
case 'h':
if (is_eq(q, "hello") && maybe_import("ledger.hello"))
return session.lookup(string(PRECMD_PREFIX) + "hello");
@@ -834,6 +852,10 @@ 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);
+ else if (is_eq(p, "price"))
+ return MAKE_FUNCTOR(report_t::fn_price);
break;
case 'q':
@@ -862,6 +884,8 @@ expr_t::ptr_op_t report_t::lookup(const string& name)
return MAKE_FUNCTOR(report_t::fn_truncated);
else if (is_eq(p, "total_expr"))
return MAKE_FUNCTOR(report_t::fn_total_expr);
+ else if (is_eq(p, "today"))
+ return MAKE_FUNCTOR(report_t::fn_today);
break;
case 'u':