summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2009-06-16 16:57:10 +0100
committerJohn Wiegley <johnw@newartisans.com>2009-06-16 16:57:10 +0100
commit298a4faef371cae8f308c0b729a1a34ea7aa927c (patch)
tree277f3125c4e03f7c80c8187c6165a66286131a9b /src
parent995c94ef178d143c2458ebf66ddb084aa782e975 (diff)
downloadfork-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
Diffstat (limited to 'src')
-rw-r--r--src/balance.cc6
-rw-r--r--src/balance.h3
-rw-r--r--src/report.cc7
-rw-r--r--src/report.h26
-rw-r--r--src/unistring.h13
-rw-r--r--src/value.cc20
-rw-r--r--src/value.h1
7 files changed, 46 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;