amount_t::print and value_t::print now use flags

This commit is contained in:
John Wiegley 2010-06-07 08:16:02 -04:00
parent 8bd362b5d1
commit a4d4f99794
9 changed files with 56 additions and 48 deletions

View file

@ -1216,7 +1216,7 @@ void amount_t::parse_conversion(const string& larger_str,
smaller.commodity().set_larger(larger); smaller.commodity().set_larger(larger);
} }
void amount_t::print(std::ostream& _out) const void amount_t::print(std::ostream& _out, const uint_least8_t) const
{ {
VERIFY(valid()); VERIFY(valid());

View file

@ -677,7 +677,13 @@ public:
true, the full internal precision of the amount is displayed, regardless true, the full internal precision of the amount is displayed, regardless
of its commodity's display precision. of its commodity's display precision.
*/ */
void print(std::ostream& out) const; #define AMOUNT_PRINT_NO_FLAGS 0x00
#define AMOUNT_PRINT_RIGHT_JUSTIFY 0x01
#define AMOUNT_PRINT_COLORIZE 0x02
#define AMOUNT_PRINT_NO_COMPUTED_ANNOTATIONS 0x04
void print(std::ostream& out,
const uint_least8_t flags = AMOUNT_PRINT_NO_FLAGS) const;
/*@}*/ /*@}*/

View file

@ -252,11 +252,10 @@ balance_t::strip_annotations(const keep_details_t& what_to_keep) const
return temp; return temp;
} }
void balance_t::print(std::ostream& out, void balance_t::print(std::ostream& out,
const int first_width, const int first_width,
const int latter_width, const int latter_width,
const bool right_justify, const uint_least8_t flags) const
const bool colorize) const
{ {
bool first = true; bool first = true;
int lwidth = latter_width; int lwidth = latter_width;
@ -285,14 +284,14 @@ void balance_t::print(std::ostream& out,
} }
std::ostringstream buf; std::ostringstream buf;
buf << *amount; amount->print(buf, flags);
justify(out, buf.str(), width, right_justify, justify(out, buf.str(), width, flags & AMOUNT_PRINT_RIGHT_JUSTIFY,
colorize && amount->sign() < 0); flags & AMOUNT_PRINT_COLORIZE && amount->sign() < 0);
} }
if (first) { if (first) {
out.width(first_width); out.width(first_width);
if (right_justify) if (flags & AMOUNT_PRINT_RIGHT_JUSTIFY)
out << std::right; out << std::right;
else else
out << std::left; out << std::left;

View file

@ -530,11 +530,10 @@ public:
* relative amounts of those commodities. There is no option to * relative amounts of those commodities. There is no option to
* change this behavior. * change this behavior.
*/ */
void print(std::ostream& out, void print(std::ostream& out,
const int first_width = -1, const int first_width = -1,
const int latter_width = -1, const int latter_width = -1,
const bool right_justify = false, const uint_least8_t flags = AMOUNT_PRINT_NO_FLAGS) const;
const bool colorize = false) const;
/** /**
* Debugging methods. There are two methods defined to help with * Debugging methods. There are two methods defined to help with

View file

@ -172,13 +172,14 @@ namespace {
if (post->amount_expr) { if (post->amount_expr) {
amt = post->amount_expr->text(); amt = post->amount_expr->text();
} else { } else {
std::size_t amount_width = int amount_width =
(report.HANDLER(amount_width_).specified ? (report.HANDLER(amount_width_).specified ?
report.HANDLER(amount_width_).value.to_long() : 12); report.HANDLER(amount_width_).value.to_int() : 12);
std::ostringstream amt_str; std::ostringstream amt_str;
report.scrub(post->amount) report.scrub(post->amount)
.print(amt_str, static_cast<int>(amount_width), -1, true); .print(amt_str, amount_width, -1, AMOUNT_PRINT_RIGHT_JUSTIFY |
AMOUNT_PRINT_NO_COMPUTED_ANNOTATIONS);
amt = amt_str.str(); amt = amt_str.str();
} }

View file

@ -597,12 +597,17 @@ value_t report_t::fn_truncated(call_scope_t& scope)
value_t report_t::fn_justify(call_scope_t& scope) value_t report_t::fn_justify(call_scope_t& scope)
{ {
interactive_t args(scope, "vl&lbb"); interactive_t args(scope, "vl&lbb");
uint_least8_t flags(AMOUNT_PRINT_NO_FLAGS);
if (args.has(3) && args.get<bool>(3))
flags |= AMOUNT_PRINT_RIGHT_JUSTIFY;
if (args.has(4) && args.get<bool>(4))
flags |= AMOUNT_PRINT_COLORIZE;
std::ostringstream out; std::ostringstream out;
args.value_at(0) args.value_at(0)
.print(out, args.get<int>(1), .print(out, args.get<int>(1), args.has(2) ? args.get<int>(2) : -1, flags);
args.has(2) ? args.get<int>(2) : -1,
args.has(3) ? args.get<bool>(3) : false,
args.has(4) ? args.get<bool>(4) : false);
return string_value(out.str()); return string_value(out.str());
} }

View file

@ -1681,18 +1681,17 @@ value_t value_t::strip_annotations(const keep_details_t& what_to_keep) const
return NULL_VALUE; return NULL_VALUE;
} }
void value_t::print(std::ostream& out, void value_t::print(std::ostream& out,
const int first_width, const int first_width,
const int latter_width, const int latter_width,
const bool right_justify, const uint_least8_t flags) const
const bool colorize) const
{ {
if (first_width > 0 && if (first_width > 0 &&
(! is_amount() || as_amount().is_zero()) && (! is_amount() || as_amount().is_zero()) &&
! is_balance() && ! is_string()) { ! is_balance() && ! is_string()) {
out.width(first_width); out.width(first_width);
if (right_justify) if (flags & AMOUNT_PRINT_RIGHT_JUSTIFY)
out << std::right; out << std::right;
else else
out << std::left; out << std::left;
@ -1716,8 +1715,9 @@ void value_t::print(std::ostream& out,
break; break;
case INTEGER: case INTEGER:
if (colorize && as_long() < 0) if (flags & AMOUNT_PRINT_COLORIZE && as_long() < 0)
justify(out, to_string(), first_width, right_justify, true); justify(out, to_string(), first_width,
flags & AMOUNT_PRINT_RIGHT_JUSTIFY, true);
else else
out << as_long(); out << as_long();
break; break;
@ -1727,21 +1727,20 @@ void value_t::print(std::ostream& out,
out << 0; out << 0;
} else { } else {
std::ostringstream buf; std::ostringstream buf;
buf << as_amount(); as_amount().print(buf, flags & AMOUNT_PRINT_NO_COMPUTED_ANNOTATIONS);
justify(out, buf.str(), first_width, right_justify, justify(out, buf.str(), first_width, flags & AMOUNT_PRINT_RIGHT_JUSTIFY,
colorize && as_amount().sign() < 0); flags & AMOUNT_PRINT_COLORIZE && as_amount().sign() < 0);
} }
break; break;
} }
case BALANCE: case BALANCE:
as_balance().print(out, first_width, latter_width, right_justify, as_balance().print(out, first_width, latter_width, flags);
colorize);
break; break;
case STRING: case STRING:
if (first_width > 0) if (first_width > 0)
justify(out, as_string(), first_width, right_justify); justify(out, as_string(), first_width, flags & AMOUNT_PRINT_RIGHT_JUSTIFY);
else else
out << as_string(); out << as_string();
break; break;
@ -1759,8 +1758,7 @@ void value_t::print(std::ostream& out,
else else
out << ", "; out << ", ";
value.print(out, first_width, latter_width, right_justify, value.print(out, first_width, latter_width, flags);
colorize);
} }
out << ')'; out << ')';
break; break;

View file

@ -942,11 +942,11 @@ public:
/** /**
* Printing methods. * Printing methods.
*/ */
void print(std::ostream& out, void print(std::ostream& out,
const int first_width = -1, const int first_width = -1,
const int latter_width = -1, const int latter_width = -1,
const bool right_justify = false, const uint_least8_t flags = AMOUNT_PRINT_NO_FLAGS) const;
const bool colorize = false) const;
void dump(std::ostream& out, const bool relaxed = true) const; void dump(std::ostream& out, const bool relaxed = true) const;
/** /**

View file

@ -4,16 +4,16 @@
>>>2 >>>2
While parsing file "$sourcepath/src/amount.h", line 66: While parsing file "$sourcepath/src/amount.h", line 66:
Error: No quantity specified for amount Error: No quantity specified for amount
While parsing file "$sourcepath/src/amount.h", line 720:
Error: Invalid date/time: line amount_t amoun
While parsing file "$sourcepath/src/amount.h", line 726: While parsing file "$sourcepath/src/amount.h", line 726:
Error: Invalid date/time: line string amount_ Error: Invalid date/time: line amount_t amoun
While parsing file "$sourcepath/src/amount.h", line 732: While parsing file "$sourcepath/src/amount.h", line 732:
Error: Invalid date/time: line string amount_ Error: Invalid date/time: line string amount_
While parsing file "$sourcepath/src/amount.h", line 738: While parsing file "$sourcepath/src/amount.h", line 738:
Error: Invalid date/time: line string amount_ Error: Invalid date/time: line string amount_
While parsing file "$sourcepath/src/amount.h", line 744: While parsing file "$sourcepath/src/amount.h", line 744:
Error: Invalid date/time: line string amount_
While parsing file "$sourcepath/src/amount.h", line 750:
Error: Invalid date/time: line std::ostream& Error: Invalid date/time: line std::ostream&
While parsing file "$sourcepath/src/amount.h", line 751: While parsing file "$sourcepath/src/amount.h", line 757:
Error: Invalid date/time: line std::istream& Error: Invalid date/time: line std::istream&
=== 7 === 7