Added new option --prepend-width

This is useful for making sure that the column containing the results of
--prepend-format is a consistent width throughout the report (including
those lines where it is not applied).

Fixes 64F9D913-75E1-4830-A3D9-29B72442E68B
This commit is contained in:
John Wiegley 2010-05-22 19:17:13 -04:00
parent db5418c838
commit a7c28aa200
4 changed files with 56 additions and 28 deletions

View file

@ -42,8 +42,10 @@ namespace ledger {
format_posts::format_posts(report_t& _report,
const string& format,
const optional<string>& _prepend_format)
: report(_report), last_xact(NULL), last_post(NULL)
const optional<string>& _prepend_format,
std::size_t _prepend_width)
: report(_report), prepend_width(_prepend_width),
last_xact(NULL), last_post(NULL)
{
TRACE_CTOR(format_posts, "report&, const string&, bool");
@ -82,8 +84,10 @@ void format_posts::operator()(post_t& post)
! post.xdata().has_flags(POST_EXT_DISPLAYED)) {
bind_scope_t bound_scope(report, post);
if (prepend_format)
if (prepend_format) {
out.width(prepend_width);
out << prepend_format(bound_scope);
}
if (last_xact != post.xact) {
if (last_xact) {
@ -107,8 +111,9 @@ void format_posts::operator()(post_t& post)
format_accounts::format_accounts(report_t& _report,
const string& format,
const optional<string>& _prepend_format)
: report(_report), disp_pred()
const optional<string>& _prepend_format,
std::size_t _prepend_width)
: report(_report), prepend_width(_prepend_width), disp_pred()
{
TRACE_CTOR(format_accounts, "report&, const string&");
@ -144,9 +149,11 @@ std::size_t format_accounts::post_account(account_t& account, const bool flat)
bind_scope_t bound_scope(report, account);
if (prepend_format)
if (prepend_format) {
static_cast<std::ostream&>(report.output_stream).width(prepend_width);
static_cast<std::ostream&>(report.output_stream)
<< prepend_format(bound_scope);
}
static_cast<std::ostream&>(report.output_stream)
<< account_line_format(bound_scope);
@ -216,9 +223,11 @@ void format_accounts::flush()
bind_scope_t bound_scope(report, *report.session.journal->master);
out << separator_format(bound_scope);
if (prepend_format)
if (prepend_format) {
static_cast<std::ostream&>(report.output_stream).width(prepend_width);
static_cast<std::ostream&>(report.output_stream)
<< prepend_format(bound_scope);
}
out << total_line_format(bound_scope);
}

View file

@ -56,17 +56,19 @@ class report_t;
class format_posts : public item_handler<post_t>
{
protected:
report_t& report;
format_t first_line_format;
format_t next_lines_format;
format_t between_format;
format_t prepend_format;
xact_t * last_xact;
post_t * last_post;
report_t& report;
format_t first_line_format;
format_t next_lines_format;
format_t between_format;
format_t prepend_format;
std::size_t prepend_width;
xact_t * last_xact;
post_t * last_post;
public:
format_posts(report_t& _report, const string& format,
const optional<string>& _prepend_format = none);
const optional<string>& _prepend_format = none,
std::size_t _prepend_width = 0);
virtual ~format_posts() {
TRACE_DTOR(format_posts);
}
@ -83,13 +85,15 @@ protected:
format_t total_line_format;
format_t separator_format;
format_t prepend_format;
std::size_t prepend_width;
predicate_t disp_pred;
std::list<account_t *> posted_accounts;
public:
format_accounts(report_t& _report, const string& _format,
const optional<string>& _prepend_format = none);
const optional<string>& _prepend_format = none,
std::size_t _prepend_width = 0);
virtual ~format_accounts() {
TRACE_DTOR(format_accounts);
}

View file

@ -119,6 +119,8 @@ void report_t::normalize_options(const string& verb)
HANDLER(meta_).str() + "\"))");
}
}
if (! HANDLED(prepend_width_))
HANDLER(prepend_width_).on_with(string("?normalize"), static_cast<long>(0));
if (verb == "print" || verb == "xact" || verb == "dump") {
HANDLER(related).on_only(string("?normalize"));
@ -937,6 +939,7 @@ option_t<report_t> * report_t::lookup_option(const char * p)
else OPT(pricedb_format_);
else OPT(payee_width_);
else OPT(prepend_format_);
else OPT(prepend_width_);
else OPT(print_virtual);
break;
case 'q':
@ -1234,7 +1237,8 @@ expr_t::ptr_op_t report_t::lookup(const symbol_t::kind_t kind,
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_)),
maybe_format(HANDLER(prepend_format_))),
maybe_format(HANDLER(prepend_format_)),
HANDLER(prepend_width_).value.to_long()),
*this, "#balance"));
}
else if (is_eq(p, "budget")) {
@ -1247,7 +1251,8 @@ expr_t::ptr_op_t report_t::lookup(const symbol_t::kind_t kind,
return expr_t::op_t::wrap_functor
(reporter<account_t, acct_handler_ptr, &report_t::accounts_report>
(new format_accounts(*this, report_format(HANDLER(budget_format_)),
maybe_format(HANDLER(prepend_format_))),
maybe_format(HANDLER(prepend_format_)),
HANDLER(prepend_width_).value.to_long()),
*this, "#budget"));
}
break;
@ -1257,7 +1262,8 @@ expr_t::ptr_op_t report_t::lookup(const symbol_t::kind_t kind,
return WRAP_FUNCTOR
(reporter<>
(new format_posts(*this, report_format(HANDLER(csv_format_)),
maybe_format(HANDLER(prepend_format_))),
maybe_format(HANDLER(prepend_format_)),
HANDLER(prepend_width_).value.to_long()),
*this, "#csv"));
}
else if (is_eq(p, "cleared")) {
@ -1266,7 +1272,8 @@ expr_t::ptr_op_t report_t::lookup(const symbol_t::kind_t kind,
return expr_t::op_t::wrap_functor
(reporter<account_t, acct_handler_ptr, &report_t::accounts_report>
(new format_accounts(*this, report_format(HANDLER(cleared_format_)),
maybe_format(HANDLER(prepend_format_))),
maybe_format(HANDLER(prepend_format_)),
HANDLER(prepend_width_).value.to_long()),
*this, "#cleared"));
}
else if (is_eq(p, "convert")) {
@ -1300,13 +1307,15 @@ expr_t::ptr_op_t report_t::lookup(const symbol_t::kind_t kind,
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_)),
maybe_format(HANDLER(prepend_format_))),
maybe_format(HANDLER(prepend_format_)),
HANDLER(prepend_width_).value.to_long()),
*this, "#prices"));
else if (is_eq(p, "pricedb"))
return expr_t::op_t::wrap_functor
(reporter<post_t, post_handler_ptr, &report_t::commodities_report>
(new format_posts(*this, report_format(HANDLER(pricedb_format_)),
maybe_format(HANDLER(prepend_format_))),
maybe_format(HANDLER(prepend_format_)),
HANDLER(prepend_width_).value.to_long()),
*this, "#pricedb"));
else if (is_eq(p, "payees"))
return WRAP_FUNCTOR(reporter<>(new report_payees(*this), *this,
@ -1318,7 +1327,8 @@ expr_t::ptr_op_t report_t::lookup(const symbol_t::kind_t kind,
return WRAP_FUNCTOR
(reporter<>
(new format_posts(*this, report_format(HANDLER(register_format_)),
maybe_format(HANDLER(prepend_format_))),
maybe_format(HANDLER(prepend_format_)),
HANDLER(prepend_width_).value.to_long()),
*this, "#register"));
else if (is_eq(p, "reload"))
return MAKE_FUNCTOR(report_t::reload_command);

View file

@ -280,6 +280,7 @@ public:
HANDLER(plot_amount_format_).report(out);
HANDLER(plot_total_format_).report(out);
HANDLER(prepend_format_).report(out);
HANDLER(prepend_width_).report(out);
HANDLER(price).report(out);
HANDLER(prices_format_).report(out);
HANDLER(pricedb_format_).report(out);
@ -372,7 +373,7 @@ public:
OPTION__(report_t, balance_format_, CTOR(report_t, balance_format_) {
on(none,
"%(justify(scrub(display_total), 20, -1, true, color))"
"%(justify(scrub(display_total), 20, 20 + prepend_width, true, color))"
" %(!options.flat ? depth_spacer : \"\")"
"%-(ansify_if(partial_account(options.flat), blue if color))\n%/"
"%$1\n%/"
@ -432,8 +433,9 @@ public:
OPTION__(report_t, cleared_format_, CTOR(report_t, cleared_format_) {
on(none,
"%(justify(scrub(get_at(total_expr, 0)), 16, -1, true, color))"
" %(justify(scrub(get_at(total_expr, 1)), 16, -1, true, color))"
"%(justify(scrub(get_at(total_expr, 0)), 16, 16 + prepend_width, "
" true, color)) %(justify(scrub(get_at(total_expr, 1)), 18, "
" 36 + prepend_width, true, color))"
" %(latest_cleared ? format_date(latest_cleared) : \" \")"
" %(!options.flat ? depth_spacer : \"\")"
"%-(ansify_if(partial_account(options.flat), blue if color))\n%/"
@ -737,6 +739,9 @@ public:
});
OPTION(report_t, prepend_format_);
OPTION_(report_t, prepend_width_, DO_(args) {
value = args[1].to_long();
});
OPTION_(report_t, price, DO() { // -I
parent->HANDLER(display_amount_)
@ -784,10 +789,10 @@ public:
" account_width), blue if color))"
" %(justify(scrub(display_amount), amount_width, "
" 3 + meta_width + date_width + payee_width + account_width"
" + amount_width, true, color))"
" + amount_width + prepend_width, true, color))"
" %(justify(scrub(display_total), total_width, "
" 4 + meta_width + date_width + payee_width + account_width"
" + amount_width + total_width, true, color))\n%/"
" + amount_width + total_width + prepend_width, true, color))\n%/"
"%(justify(\" \", 2 + date_width + payee_width))"
"%$3 %$4 %$5\n");
});