diff options
author | John Wiegley <johnw@newartisans.com> | 2009-06-16 16:57:10 +0100 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2009-06-16 16:57:10 +0100 |
commit | 298a4faef371cae8f308c0b729a1a34ea7aa927c (patch) | |
tree | 277f3125c4e03f7c80c8187c6165a66286131a9b | |
parent | 995c94ef178d143c2458ebf66ddb084aa782e975 (diff) | |
download | fork-ledger-298a4faef371cae8f308c0b729a1a34ea7aa927c.tar.gz fork-ledger-298a4faef371cae8f308c0b729a1a34ea7aa927c.tar.bz2 fork-ledger-298a4faef371cae8f308c0b729a1a34ea7aa927c.zip |
Move amount colorization deeper into the core
This is necessary in order to redden negative amounts correctly under
all circumstances, such as component amounts of a multi-commodity
balance.
Fixes 727B2DF8-A2A1-4716-9C15-547F20D5F933
-rw-r--r-- | src/balance.cc | 6 | ||||
-rw-r--r-- | src/balance.h | 3 | ||||
-rw-r--r-- | src/report.cc | 7 | ||||
-rw-r--r-- | src/report.h | 26 | ||||
-rw-r--r-- | src/unistring.h | 13 | ||||
-rw-r--r-- | src/value.cc | 20 | ||||
-rw-r--r-- | src/value.h | 1 | ||||
-rw-r--r-- | test/regress/727B2DF8.test | 81 |
8 files changed, 127 insertions, 30 deletions
diff --git a/src/balance.cc b/src/balance.cc index 864926cf..8acb6378 100644 --- a/src/balance.cc +++ b/src/balance.cc @@ -243,7 +243,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 bool right_justify) const + const bool right_justify, + const bool colorize) const { bool first = true; int lwidth = latter_width; @@ -272,7 +273,8 @@ void balance_t::print(std::ostream& out, std::ostringstream buf; buf << *amount; - justify(out, buf.str(), width, right_justify); + justify(out, buf.str(), width, right_justify, + colorize && amount->sign() < 0); } if (first) { diff --git a/src/balance.h b/src/balance.h index 85c6b1ff..142621f8 100644 --- a/src/balance.h +++ b/src/balance.h @@ -483,7 +483,8 @@ public: void print(std::ostream& out, const int first_width = -1, const int latter_width = -1, - const bool right_justify = true) const; + const bool right_justify = true, + const bool colorize = true) const; /** * Debugging methods. There are two methods defined to help with diff --git a/src/report.cc b/src/report.cc index f2d028ba..de67708b 100644 --- a/src/report.cc +++ b/src/report.cc @@ -230,13 +230,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()); diff --git a/src/report.h b/src/report.h index fa9318ce..a1c93c39 100644 --- a/src/report.h +++ b/src/report.h @@ -333,12 +333,10 @@ public: OPTION__(report_t, balance_format_, CTOR(report_t, balance_format_) { on(none, - "%(ansify_if(justify(scrub(display_total), 20, -1, true), " - " red if color & scrub(display_total) < 0))" + "%(justify(scrub(display_total), 20, -1, true, color))" " %(!options.flat ? depth_spacer : \"\")" "%-(ansify_if(partial_account(options.flat), blue if color))\n%/" - "%(ansify_if(justify(scrub(display_total), 20, -1, true), " - " red if color & scrub(display_total) < 0))\n%/" + "%(justify(scrub(display_total), 20, -1, true, color))\n%/" "--------------------\n"); }); @@ -668,21 +666,21 @@ public: " bold if color & !cleared))" " %(ansify_if(justify(truncated(account, account_width, abbrev_len), " " account_width), blue if color))" - " %(ansify_if(justify(scrub(display_amount), amount_width, " - " 3 + date_width + payee_width + account_width + amount_width, true), " - " red if color & scrub(display_amount) < 0))" - " %(ansify_if(justify(scrub(display_total), total_width, " + " %(justify(scrub(display_amount), amount_width, " + " 3 + date_width + payee_width + account_width + amount_width, " + " true, color))" + " %(justify(scrub(display_total), total_width, " " 4 + date_width + payee_width + account_width + amount_width " - " + total_width, true), red if color & scrub(display_amount) < 0))\n%/" + " + total_width, true, color))\n%/" "%(justify(\" \", 2 + date_width + payee_width))" "%(ansify_if(justify(truncated(account, account_width, abbrev_len), " " account_width), blue if color))" - " %(ansify_if(justify(scrub(display_amount), amount_width, " - " 3 + date_width + payee_width + account_width + amount_width, true), " - " red if color & scrub(display_amount) < 0))" - " %(ansify_if(justify(scrub(display_total), total_width, " + " %(justify(scrub(display_amount), amount_width, " + " 3 + date_width + payee_width + account_width + amount_width, " + " true, color))" + " %(justify(scrub(display_total), total_width, " " 4 + date_width + payee_width + account_width + amount_width " - " + total_width, true), red if color & scrub(display_amount) < 0))\n"); + " + total_width, true, color))\n"); }); OPTION(report_t, related); // -r diff --git a/src/unistring.h b/src/unistring.h index b3086c39..5467a151 100644 --- a/src/unistring.h +++ b/src/unistring.h @@ -99,10 +99,14 @@ public: inline void justify(std::ostream& out, const std::string& str, int width, - bool right = false) + bool right = false, + bool redden = false) { - if (! right) + if (! right) { + if (redden) out << "\e[31m"; out << str; + if (redden) out << "\e[0m"; + } unistring temp(str); @@ -110,8 +114,11 @@ inline void justify(std::ostream& out, while (spacing-- > 0) out << ' '; - if (right) + if (right) { + if (redden) out << "\e[31m"; out << str; + if (redden) out << "\e[0m"; + } } } // namespace ledger diff --git a/src/value.cc b/src/value.cc index f34d0e48..6f6e665d 100644 --- a/src/value.cc +++ b/src/value.cc @@ -1424,10 +1424,12 @@ void value_t::print(std::ostream& out, const int first_width, const int latter_width, const bool right_justify, + const bool colorize, const optional<string>& date_format) const { if (first_width > 0 && - ! is_amount() && ! is_balance() && ! is_string()) { + (! is_amount() || as_amount().is_zero()) && + ! is_balance() && ! is_string()) { out.width(first_width); if (right_justify) @@ -1460,17 +1462,20 @@ void value_t::print(std::ostream& out, break; case INTEGER: - out << std::right << as_long(); + if (colorize && as_long() < 0) + justify(out, to_string(), first_width, right_justify, true); + else + out << as_long(); break; case AMOUNT: { if (as_amount().is_zero()) { - out.width(first_width); - out << (right_justify ? std::right : std::left) << 0; + out << 0; } else { std::ostringstream buf; buf << as_amount(); - justify(out, buf.str(), first_width, right_justify); + justify(out, buf.str(), first_width, right_justify, + colorize && as_amount().sign() < 0); } break; } @@ -1493,14 +1498,15 @@ void value_t::print(std::ostream& out, out << ", "; value.print(out, first_width, latter_width, right_justify, - date_format); + colorize, date_format); } out << ')'; break; } case BALANCE: - as_balance().print(out, first_width, latter_width, right_justify); + as_balance().print(out, first_width, latter_width, right_justify, + colorize); break; case POINTER: diff --git a/src/value.h b/src/value.h index 1e972558..a2b9c07f 100644 --- a/src/value.h +++ b/src/value.h @@ -915,6 +915,7 @@ public: const int first_width = -1, const int latter_width = -1, const bool right_justify = false, + const bool colorize = false, const optional<string>& date_format = none) const; void dump(std::ostream& out, const bool relaxed = true) const; diff --git a/test/regress/727B2DF8.test b/test/regress/727B2DF8.test new file mode 100644 index 00000000..599090a1 --- /dev/null +++ b/test/regress/727B2DF8.test @@ -0,0 +1,81 @@ +reg --color +<<< +N $ + += account =~ /^Expenses:Books/ + (Liabilities:Taxes) -0.10 + +~ Monthly + Assets:Bank:Checking $500.00 + Income:Salary + +2004/05/01 * Checking balance + Assets:Bank:Checking $1,000.00 + Equity:Opening Balances + +2004/05/03=2004/05/01 * Investment balance + Assets:Brokerage 50 AAPL @ $30.00 + Equity:Opening Balances + +2004/05/14 * Páy dày + Assets:Bank:Checking 500.00€ + Income:Salary + +2004/05/14 * Another dày in which there is Páying + Asséts:Bánk:Chécking:Asséts:Bánk:Chécking $500.00 + Income:Salary + +2004/05/14 * Another dày in which there is Páying + Русский язык:Русский язык:Русский язык:Русский язык $1000.00 + Income:Salary + +2004/05/27 Book Store + Expenses:Books $20.00 + Expenses:Cards $40.00 + Expenses:Docs $30.00 + Liabilities:MasterCard + +2004/05/27 (100) Credit card company + ; This is an xact note! + ; Sample: Value + Liabilities:MasterCard $20.00 + ; This is a posting note! + ; Sample: Another Value + ; :MyTag: + Assets:Bank:Checking + ; :AnotherTag: +>>>1 +04-May-01 Checking balance [34mAssets:Bank:Checking [0m $1,000.00 $1,000.00 + [34mEq:Opening Balances [0m [31m$-1,000.00[0m 0 +04-May-03 Investment balance [34mAssets:Brokerage [0m 50 AAPL 50 AAPL + [34mEq:Opening Balances [0m [31m$-1,500.00[0m [31m$-1,500.00[0m + 50 AAPL +04-May-14 Páy dày [34mAssets:Bank:Checking [0m 500.00€ [31m$-1,500.00[0m + 50 AAPL + 500.00€ + [34mIncome:Salary [0m [31m-500.00€[0m [31m$-1,500.00[0m + 50 AAPL +04-May-14 Another dày in whic.. [34m..Bá:Ch:As:Bá:Chécking[0m $500.00 [31m$-1,000.00[0m + 50 AAPL + [34mIncome:Salary [0m [31m$-500.00[0m [31m$-1,500.00[0m + 50 AAPL +04-May-14 Another dày in whic.. [34mРу:Ру:Ру:Русский язык [0m $1,000.00 [31m$-500.00[0m + 50 AAPL + [34mIncome:Salary [0m [31m$-1,000.00[0m [31m$-1,500.00[0m + 50 AAPL +04-May-27 [1mBook Store [0m [34mExpenses:Books [0m $20.00 [31m$-1,480.00[0m + 50 AAPL + [34mExpenses:Cards [0m $40.00 [31m$-1,440.00[0m + 50 AAPL + [34mExpenses:Docs [0m $30.00 [31m$-1,410.00[0m + 50 AAPL + [34mLiabilities:MasterCard[0m [31m$-90.00[0m [31m$-1,500.00[0m + 50 AAPL + [34m(Liabilities:Taxes) [0m [31m$-2.00[0m [31m$-1,502.00[0m + 50 AAPL +04-May-27 [1mCredit card company [0m [34mLiabilities:MasterCard[0m $20.00 [31m$-1,482.00[0m + 50 AAPL + [34mAssets:Bank:Checking [0m [31m$-20.00[0m [31m$-1,502.00[0m + 50 AAPL +>>>2 +=== 0 |