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
This commit is contained in:
John Wiegley 2009-06-16 16:57:10 +01:00
parent 995c94ef17
commit 298a4faef3
8 changed files with 127 additions and 30 deletions

View file

@ -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) {

View file

@ -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

View file

@ -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());

View file

@ -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

View file

@ -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

View file

@ -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:

View file

@ -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;

View file

@ -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 Assets:Bank:Checking  $1,000.00 $1,000.00
Eq:Opening Balances  $-1,000.00 0
04-May-03 Investment balance Assets:Brokerage  50 AAPL 50 AAPL
Eq:Opening Balances  $-1,500.00 $-1,500.00
50 AAPL
04-May-14 Páy dày Assets:Bank:Checking  500.00€ $-1,500.00
50 AAPL
500.00€
Income:Salary  -500.00€ $-1,500.00
50 AAPL
04-May-14 Another dày in whic.. ..Bá:Ch:As:Bá:Chécking $500.00 $-1,000.00
50 AAPL
Income:Salary  $-500.00 $-1,500.00
50 AAPL
04-May-14 Another dày in whic.. Ру:Ру:Ру:Русский язык  $1,000.00 $-500.00
50 AAPL
Income:Salary  $-1,000.00 $-1,500.00
50 AAPL
04-May-27 Book Store  Expenses:Books  $20.00 $-1,480.00
50 AAPL
Expenses:Cards  $40.00 $-1,440.00
50 AAPL
Expenses:Docs  $30.00 $-1,410.00
50 AAPL
Liabilities:MasterCard $-90.00 $-1,500.00
50 AAPL
(Liabilities:Taxes)  $-2.00 $-1,502.00
50 AAPL
04-May-27 Credit card company  Liabilities:MasterCard $20.00 $-1,482.00
50 AAPL
Assets:Bank:Checking  $-20.00 $-1,502.00
50 AAPL
>>>2
=== 0