diff options
author | John Wiegley <johnw@newartisans.com> | 2009-02-22 03:14:58 -0400 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2009-02-22 04:51:11 -0400 |
commit | 04fd1ae24c3be6f9d3400c55bce90c3fd743e96b (patch) | |
tree | 25d1ad1adfd755b41ee1f24b3177a2d11219e714 /src | |
parent | 640279c65d638d777852a291d674c342a9b8115e (diff) | |
download | fork-ledger-04fd1ae24c3be6f9d3400c55bce90c3fd743e96b.tar.gz fork-ledger-04fd1ae24c3be6f9d3400c55bce90c3fd743e96b.tar.bz2 fork-ledger-04fd1ae24c3be6f9d3400c55bce90c3fd743e96b.zip |
Fixed the way values are justified for printing
Diffstat (limited to 'src')
-rw-r--r-- | src/balance.cc | 10 | ||||
-rw-r--r-- | src/balance.h | 6 | ||||
-rw-r--r-- | src/report.cc | 19 | ||||
-rw-r--r-- | src/report.h | 36 | ||||
-rw-r--r-- | src/value.cc | 18 | ||||
-rw-r--r-- | src/value.h | 7 |
6 files changed, 54 insertions, 42 deletions
diff --git a/src/balance.cc b/src/balance.cc index b8009607..b8e4e07d 100644 --- a/src/balance.cc +++ b/src/balance.cc @@ -215,7 +215,8 @@ balance_t::strip_annotations(const keep_details_t& what_to_keep) const void balance_t::print(std::ostream& out, const int first_width, - const int latter_width) const + const int latter_width, + const bool right_justify) const { bool first = true; int lwidth = latter_width; @@ -244,13 +245,12 @@ void balance_t::print(std::ostream& out, std::ostringstream buf; buf << *amount; - justify(out, buf.str(), width, true); + justify(out, buf.str(), width, right_justify); } if (first) { - std::ostringstream buf; - buf << amount_t(0L); - justify(out, buf.str(), first_width, true); + out.width(first_width); + out << (right_justify ? std::right : std::left) << 0; } } diff --git a/src/balance.h b/src/balance.h index 49435767..996132e6 100644 --- a/src/balance.h +++ b/src/balance.h @@ -469,8 +469,10 @@ public: * relative amounts of those commodities. There is no option to * change this behavior. */ - void print(std::ostream& out, const int first_width, - const int latter_width = -1) const; + void print(std::ostream& out, + const int first_width = -1, + const int latter_width = -1, + const bool right_justify = true) const; /** * Debugging methods. There are two methods defined to help with diff --git a/src/report.cc b/src/report.cc index ea2c2bae..d498cc85 100644 --- a/src/report.cc +++ b/src/report.cc @@ -174,15 +174,16 @@ value_t report_t::fn_truncate(call_scope_t& args) env.has(2) ? env.get<long>(2) : -1)); } -value_t report_t::fn_print(call_scope_t& args) +value_t report_t::fn_justify(call_scope_t& scope) { - interactive_t env(args, "vl&ls"); + interactive_t args(scope, "vl&lbs"); std::ostringstream out; - env.value_at(0) + args.value_at(0) .strip_annotations(what_to_keep()) - .print(out, env.get<long>(1), - env.has(2) ? env.get<long>(2) : -1, - env.has(3) ? env.get<string>(3) : + .print(out, args.get<long>(1), + args.has(2) ? args.get<long>(2) : -1, + args.has(3), + args.has(4) ? args.get<string>(4) : (HANDLED(date_format_) ? HANDLER(date_format_).str() : optional<string>())); return string_value(out.str()); @@ -602,7 +603,9 @@ expr_t::ptr_op_t report_t::lookup(const string& name) break; case 'j': - if (is_eq(p, "join")) + if (is_eq(p, "justify")) + return MAKE_FUNCTOR(report_t::fn_justify); + else if (is_eq(p, "join")) return MAKE_FUNCTOR(report_t::fn_join); break; @@ -648,8 +651,6 @@ expr_t::ptr_op_t report_t::lookup(const string& name) break; } } - else if (is_eq(p, "print")) - return MAKE_FUNCTOR(report_t::fn_print); break; case 'q': diff --git a/src/report.h b/src/report.h index ffa39b90..723100f4 100644 --- a/src/report.h +++ b/src/report.h @@ -138,7 +138,7 @@ public: value_t fn_quantity(call_scope_t& scope); value_t fn_rounded(call_scope_t& scope); value_t fn_truncate(call_scope_t& scope); - value_t fn_print(call_scope_t& scope); + value_t fn_justify(call_scope_t& scope); value_t fn_quoted(call_scope_t& scope); value_t fn_join(call_scope_t& scope); value_t fn_format_date(call_scope_t& scope); @@ -233,7 +233,7 @@ public: }); OPTION__(report_t, balance_format_, CTOR(report_t, balance_format_) { - on("%(ansify_if(print(scrub(display_total), 20), \"red\", " + on("%(ansify_if(justify(scrub(display_total), 20, -1, true), \"red\", " " color & scrub(display_total) < 0))" " %(!options.flat ? depth_spacer : \"\")" "%-(ansify_if(partial_account(options.flat), \"blue\", color))\n"); @@ -475,10 +475,12 @@ public: "%(code ? \" (\" + code + \")\" : \"\") %(payee)%(entry.comment | \"\")\n" " %(entry.uncleared ? (cleared ? \"* \" : (pending ? \"! \" : \"\")) : \"\")" "%-34(account)" - " %12(calculated ? \"\" : scrub(amount))%(comment | \"\")\n%/" + " %12(calculated ? \"\" : justify(scrub(amount), 12, -1, true))" + "%(comment | \"\")\n%/" " %(entry.uncleared ? (cleared ? \"* \" : (pending ? \"! \" : \"\")) : \"\")" "%-34(account)" - " %12(calculated ? \"\" : scrub(amount))%(comment | \"\")\n%/\n"); + " %12(calculated ? \"\" : justify(scrub(amount), 12, -1, true))" + "%(comment | \"\")\n%/\n"); }); OPTION_(report_t, quantity, DO() { // -O @@ -498,26 +500,26 @@ public: }); OPTION__(report_t, register_format_, CTOR(report_t, register_format_) { - on("%(ansify_if(print(date, date_width), \"green\", color & date > today))" - " %(ansify_if(print(truncate(payee, payee_width), payee_width), " + on("%(ansify_if(justify(date, date_width), \"green\", color & date > today))" + " %(ansify_if(justify(truncate(payee, payee_width), payee_width), " " \"bold\", color & !cleared))" - " %(ansify_if(print(truncate(account, account_width, abbrev_len), " + " %(ansify_if(justify(truncate(account, account_width, abbrev_len), " " account_width), \"blue\", color))" - " %(ansify_if(print(scrub(display_amount), amount_width, " - " 3 + date_width + payee_width + account_width + amount_width), " + " %(ansify_if(justify(scrub(display_amount), amount_width, " + " 3 + date_width + payee_width + account_width + amount_width, true), " " \"red\", color & scrub(display_amount) < 0))" - " %(ansify_if(print(scrub(display_total), total_width, " + " %(ansify_if(justify(scrub(display_total), total_width, " " 4 + date_width + payee_width + account_width + amount_width " - " + total_width), \"red\", color & scrub(display_amount) < 0))\n%/" - "%(print(\" \", 2 + date_width + payee_width))" - "%(ansify_if(print(truncate(account, account_width, abbrev_len), " + " + total_width, true), \"red\", color & scrub(display_amount) < 0))\n%/" + "%(justify(\" \", 2 + date_width + payee_width))" + "%(ansify_if(justify(truncate(account, account_width, abbrev_len), " " account_width), \"blue\", color))" - " %(ansify_if(print(scrub(display_amount), amount_width, " - " 3 + date_width + payee_width + account_width + amount_width), " + " %(ansify_if(justify(scrub(display_amount), amount_width, " + " 3 + date_width + payee_width + account_width + amount_width, true), " " \"red\", color & scrub(display_amount) < 0))" - " %(ansify_if(print(scrub(display_total), total_width, " + " %(ansify_if(justify(scrub(display_total), total_width, " " 4 + date_width + payee_width + account_width + amount_width " - " + total_width), \"red\", color & scrub(display_amount) < 0))\n"); + " + total_width, true), \"red\", color & scrub(display_amount) < 0))\n"); }); OPTION(report_t, related); // -r diff --git a/src/value.cc b/src/value.cc index 7162a610..52760535 100644 --- a/src/value.cc +++ b/src/value.cc @@ -1317,12 +1317,17 @@ value_t value_t::strip_annotations(const keep_details_t& what_to_keep) const void value_t::print(std::ostream& out, const int first_width, const int latter_width, + const bool right_justify, const optional<string>& date_format) const { if (first_width > 0 && ! is_amount() && ! is_balance() && ! is_string()) { out.width(first_width); - out << std::left; + + if (right_justify) + out << std::right; + else + out << std::left; } switch (type()) { @@ -1355,17 +1360,17 @@ void value_t::print(std::ostream& out, case AMOUNT: { if (as_amount().is_zero()) { out.width(first_width); - out << std::right << 0L; + out << (right_justify ? std::right : std::left) << 0; } else { std::ostringstream buf; buf << as_amount(); - justify(out, buf.str(), first_width, true); + justify(out, buf.str(), first_width, right_justify); } break; } case STRING: - justify(out, as_string(), first_width); + justify(out, as_string(), first_width, right_justify); break; case MASK: @@ -1381,14 +1386,15 @@ void value_t::print(std::ostream& out, else out << ", "; - value.print(out, first_width, latter_width, date_format); + value.print(out, first_width, latter_width, right_justify, + date_format); } out << ')'; break; } case BALANCE: - as_balance().print(out, first_width, latter_width); + as_balance().print(out, first_width, latter_width, right_justify); break; case POINTER: diff --git a/src/value.h b/src/value.h index 5fb78cb4..546707f0 100644 --- a/src/value.h +++ b/src/value.h @@ -890,9 +890,10 @@ public: * Printing methods. */ void print(std::ostream& out, - const int first_width = - 1, - const int latter_width = -1, - const optional<string>& date_format = none) const; + const int first_width = -1, + const int latter_width = -1, + const bool right_justify = false, + const optional<string>& date_format = none) const; void dump(std::ostream& out, const bool relaxed = true) const; /** |