diff options
author | John Wiegley <johnw@newartisans.com> | 2010-05-22 23:22:26 -0600 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2010-05-22 23:34:12 -0600 |
commit | 7bd0170d51dd3cf20764b6748a9ea4884f9ee7bc (patch) | |
tree | 5785de2e6338653d934e25409517719ea91440e6 /src/print.cc | |
parent | 0f3e7e3e7fcba2b9f99dcc101b1c937ffa0fd56e (diff) | |
download | fork-ledger-7bd0170d51dd3cf20764b6748a9ea4884f9ee7bc.tar.gz fork-ledger-7bd0170d51dd3cf20764b6748a9ea4884f9ee7bc.tar.bz2 fork-ledger-7bd0170d51dd3cf20764b6748a9ea4884f9ee7bc.zip |
print honors --columns, --account,amount-width
Diffstat (limited to 'src/print.cc')
-rw-r--r-- | src/print.cc | 81 |
1 files changed, 59 insertions, 22 deletions
diff --git a/src/print.cc b/src/print.cc index 7964b001..5c46f4e7 100644 --- a/src/print.cc +++ b/src/print.cc @@ -48,9 +48,14 @@ print_xacts::print_xacts(report_t& _report, } namespace { - void print_note(std::ostream& out, const string& note) + void print_note(std::ostream& out, + const string& note, + const std::size_t columns, + const std::size_t prior_width) { - if (note.length() > 15) + // The 4 is for four leading spaces at the beginning of the posting, and + // the 3 is for two spaces and a semi-colon before the note. + if (columns > 0 && note.length() > columns - (prior_width + 3)) out << "\n ;"; else out << " ;"; @@ -79,22 +84,32 @@ namespace { format = report.HANDLER(date_format_).str().c_str(); } - out << format_date(item_t::use_effective_date ? - xact.date() : xact.actual_date(), format_type, format); + std::ostringstream buf; + + buf << format_date(item_t::use_effective_date ? + xact.date() : xact.actual_date(), + format_type, format); if (! item_t::use_effective_date && xact.effective_date()) - out << '=' << format_date(*xact.effective_date(), format_type, format); - out << ' '; + buf << '=' << format_date(*xact.effective_date(), + format_type, format); + buf << ' '; - out << (xact.state() == item_t::CLEARED ? "* " : + buf << (xact.state() == item_t::CLEARED ? "* " : (xact.state() == item_t::PENDING ? "! " : "")); if (xact.code) - out << '(' << *xact.code << ") "; + buf << '(' << *xact.code << ") "; + + buf << xact.payee; + + string leader = buf.str(); + out << leader; - out << xact.payee; + std::size_t columns = (report.HANDLED(columns_) ? + report.HANDLER(columns_).value.to_long() : 80); if (xact.note) - print_note(out, *xact.note); + print_note(out, *xact.note, columns, unistring(leader).length()); out << '\n'; if (xact.metadata) { @@ -139,20 +154,37 @@ namespace { buf << ')'; } - if (! post->has_flags(POST_CALCULATED) || report.HANDLED(print_virtual)) { - unistring name(buf.str()); + unistring name(buf.str()); + + std::size_t account_width = + (report.HANDLER(account_width_).specified ? + report.HANDLER(account_width_).value.to_long() : 36); + + if (account_width < name.length()) + account_width = name.length(); + if (! post->has_flags(POST_CALCULATED) || report.HANDLED(print_virtual)) { out << name.extract(); - int slip = 36 - static_cast<int>(name.length()); - if (slip > 0) - out << string(slip, ' '); + int slip = (static_cast<int>(account_width) - + static_cast<int>(name.length())); + if (slip > 0) { + out.width(slip); + out << ' '; + } + + std::ostringstream amtbuf; string amt; if (post->amount_expr) { amt = post->amount_expr->text(); } else { + std::size_t amount_width = + (report.HANDLER(amount_width_).specified ? + report.HANDLER(amount_width_).value.to_long() : 12); + std::ostringstream amt_str; - report.scrub(post->amount).print(amt_str, 12, -1, true); + report.scrub(post->amount) + .print(amt_str, static_cast<int>(amount_width), -1, true); amt = amt_str.str(); } @@ -161,24 +193,29 @@ namespace { int amt_slip = (static_cast<int>(amt.length()) - static_cast<int>(trimmed_amt.length())); if (slip + amt_slip < 2) - out << string(2 - (slip + amt_slip), ' '); - out << amt; + amtbuf << string(2 - (slip + amt_slip), ' '); + amtbuf << amt; if (post->cost && ! post->has_flags(POST_CALCULATED)) { if (post->has_flags(POST_COST_IN_FULL)) - out << " @@ " << report.scrub(post->cost->abs()); + amtbuf << " @@ " << report.scrub(post->cost->abs()); else - out << " @ " << report.scrub((*post->cost / post->amount).abs()); + amtbuf << " @ " << report.scrub((*post->cost / post->amount).abs()); } if (post->assigned_amount) - out << " = " << report.scrub(*post->assigned_amount); + amtbuf << " = " << report.scrub(*post->assigned_amount); + + string trailer = amtbuf.str(); + out << trailer; + + account_width += unistring(trailer).length(); } else { out << buf.str(); } if (post->note) - print_note(out, *post->note); + print_note(out, *post->note, columns, 4 + account_width); out << '\n'; } } |