Fixed a display issue with the balance report

This commit is contained in:
John Wiegley 2009-10-27 19:01:55 -04:00
parent cbf8f355d1
commit aae134f692
7 changed files with 79 additions and 81 deletions

View file

@ -290,7 +290,11 @@ void balance_t::print(std::ostream& out,
if (first) {
out.width(first_width);
out << (right_justify ? std::right : std::left) << 0;
if (right_justify)
out << std::right;
else
out << std::left;
out << 0;
}
}

View file

@ -75,6 +75,11 @@ public:
TRACE_DTOR(supports_flags);
}
supports_flags& operator=(const supports_flags& other) {
_flags = other._flags;
return *this;
}
flags_t flags() const {
return _flags;
}

View file

@ -92,7 +92,8 @@ namespace {
}
}
format_t::element_t * format_t::parse_elements(const string& fmt)
format_t::element_t * format_t::parse_elements(const string& fmt,
const optional<format_t&>& tmpl)
{
std::auto_ptr<element_t> result;
@ -101,34 +102,6 @@ format_t::element_t * format_t::parse_elements(const string& fmt)
char buf[1024];
char * q = buf;
// The following format codes need to be implemented as functions:
//
// d: COMPLETE_DATE_STRING
// D: DATE_STRING
// S: SOURCE; break
// B: XACT_BEG_POS
// b: XACT_BEG_LINE
// E: XACT_END_POS
// e: XACT_END_LINE
// X: CLEARED
// Y: XACT_CLEARED
// C: CODE
// P: PAYEE
// W: OPT_ACCOUNT
// a: ACCOUNT_NAME
// A: ACCOUNT_FULLNAME
// t: AMOUNT
// o: OPT_AMOUNT
// T: TOTAL
// N: NOTE
// n: OPT_NOTE
// _: DEPTH_SPACER
//
// xB: POST_BEG_POS
// xb: POST_BEG_LINE
// xE: POST_END_POS
// xe: POST_END_LINE
for (const char * p = fmt.c_str(); *p; p++) {
if (*p != '%' && *p != '\\') {
*q++ = *p;
@ -203,6 +176,32 @@ format_t::element_t * format_t::parse_elements(const string& fmt)
current->chars = "%";
break;
case '$': {
if (! tmpl)
throw_(format_error, _("Prior field reference, but no template"));
p++;
if (*p == '0' || (! std::isdigit(*p) &&
*p != 'A' && *p != 'B' && *p != 'C' &&
*p != 'D' && *p != 'E' && *p != 'F'))
throw_(format_error, _("%$ field reference must be a digit from 1-9"));
unsigned int index = std::isdigit(*p) ? *p - '0' : (*p - 'A' + 10);
element_t * tmpl_elem = tmpl->elements.get();
for (unsigned int i = 1; i < index && tmpl_elem; i++) {
tmpl_elem = tmpl_elem->next.get();
while (tmpl_elem && tmpl_elem->type != element_t::EXPR)
tmpl_elem = tmpl_elem->next.get();
}
if (! tmpl_elem)
throw_(format_error, _("%$ reference to a non-existent prior field"));
*current = *tmpl_elem;
break;
}
case '(':
case '{': {
bool format_amount = *p == '{';

View file

@ -61,7 +61,7 @@ DECLARE_EXCEPTION(format_error, std::runtime_error);
*/
class format_t : public noncopyable
{
struct element_t : public supports_flags<>, public noncopyable
struct element_t : public supports_flags<>
{
#define ELEMENT_ALIGN_LEFT 0x01
@ -82,6 +82,21 @@ class format_t : public noncopyable
~element_t() throw() {
TRACE_DTOR(element_t);
}
element_t(const element_t& elem) : supports_flags<>() {
*this = elem;
}
element_t& operator=(const element_t& elem) {
if (this != &elem) {
supports_flags<>::operator=(elem);
type = elem.type;
min_width = elem.min_width;
max_width = elem.max_width;
chars = elem.chars;
expr = elem.expr;
}
return *this;
}
friend inline void mark_red(std::ostream& out, const element_t * elem) {
out.setf(std::ios::left);
@ -114,7 +129,8 @@ public:
static bool default_style_changed;
private:
static element_t * parse_elements(const string& fmt);
static element_t * parse_elements(const string& fmt,
const optional<format_t&>& tmpl);
public:
format_t() {
@ -128,8 +144,8 @@ public:
TRACE_DTOR(format_t);
}
void parse(const string& _format) {
elements.reset(parse_elements(_format));
void parse(const string& _format, const optional<format_t&>& tmpl = none) {
elements.reset(parse_elements(_format, tmpl));
format_string = _format;
}

View file

@ -54,10 +54,10 @@ format_posts::format_posts(report_t& _report,
first_line_format.parse(string(f, 0, p - f));
const char * n = p + 2;
if (const char * p = std::strstr(n, "%/")) {
next_lines_format.parse(string(n, 0, p - n));
between_format.parse(string(p + 2));
next_lines_format.parse(string(n, 0, p - n), first_line_format);
between_format.parse(string(p + 2), first_line_format);
} else {
next_lines_format.parse(n);
next_lines_format.parse(n, first_line_format);
}
} else {
first_line_format.parse(format);
@ -125,14 +125,14 @@ format_accounts::format_accounts(report_t& _report,
account_line_format.parse(string(f, 0, p - f));
const char * n = p + 2;
if (const char * p = std::strstr(n, "%/")) {
total_line_format.parse(string(n, 0, p - n));
separator_format.parse(string(p + 2));
total_line_format.parse(string(n, 0, p - n), account_line_format);
separator_format.parse(string(p + 2), account_line_format);
} else {
total_line_format.parse(n);
total_line_format.parse(n, account_line_format);
}
} else {
account_line_format.parse(format);
total_line_format.parse(format);
total_line_format.parse(format, account_line_format);
}
}

View file

@ -347,7 +347,7 @@ public:
"%(justify(scrub(display_total), 20, -1, true, color))"
" %(!options.flat ? depth_spacer : \"\")"
"%-(ansify_if(partial_account(options.flat), blue if color))\n%/"
"%(justify(scrub(display_total), 20, -1, true, color))\n%/"
"%$1\n%/"
"--------------------\n");
});
@ -377,9 +377,11 @@ public:
OPTION__(report_t, budget_format_, CTOR(report_t, budget_format_) {
on(none,
"%(justify(scrub(get_at(total_expr, 0)), 12, -1, true, color))"
" %(justify(scrub(- get_at(total_expr, 1)), 12, -1, true, color))"
" %(justify(scrub(- get_at(total_expr, 1)), 12, "
" 12 + 1 + 12, true, color))"
" %(justify(scrub(get_at(total_expr, 1) + "
" get_at(total_expr, 0)), 12, -1, true, color))"
" get_at(total_expr, 0)), 12, "
" 12 + 1 + 12 + 1 + 12, true, color))"
" %(ansify_if("
" justify((get_at(total_expr, 1) ? "
" scrub((100% * get_at(total_expr, 0)) / "
@ -390,19 +392,7 @@ public:
" quantity(get_at(total_expr, 1))) >= 1))))"
" %(!options.flat ? depth_spacer : \"\")"
"%-(ansify_if(partial_account(options.flat), blue if color))\n"
"%/"
"%(justify(scrub(get_at(total_expr, 0)), 12, -1, true, color))"
" %(justify(scrub(- get_at(total_expr, 1)), 12, -1, true, color))"
" %(justify(scrub(get_at(total_expr, 1) + "
" get_at(total_expr, 0)), 12, -1, true, color))"
" %(ansify_if("
" justify((get_at(total_expr, 1) ? "
" scrub((100% * get_at(total_expr, 0)) / "
" - get_at(total_expr, 1)) : 0), "
" 5, -1, true, false),"
" magenta if (color and get_at(total_expr, 1) and "
" (abs(quantity(get_at(total_expr, 0)) / "
" quantity(get_at(total_expr, 1))) >= 1))))\n%/"
"%/%$1 %$2 %$3 %$4\n%/"
"------------ ------------ ------------ -----\n");
});
@ -419,9 +409,7 @@ public:
" %(latest_cleared ? format_date(latest_cleared) : \" \")"
" %(!options.flat ? depth_spacer : \"\")"
"%-(ansify_if(partial_account(options.flat), blue if color))\n%/"
"%(justify(scrub(get_at(total_expr, 0)), 16, -1, true, color))"
" %(justify(scrub(get_at(total_expr, 1)), 16, -1, true, color))"
" %(latest_cleared ? format_date(latest_cleared) : \" \")\n%/"
"%$1 %$2 %$3\n%/"
"---------------- ---------------- ---------\n");
});
@ -694,13 +682,7 @@ public:
"%(has_cost & !cost_calculated ?"
" \" @ \" + justify(scrub(abs(cost / amount)), 0) : \"\")"
"%(comment)\n%/"
" %(xact.uncleared ?"
" (cleared ? \"* \" : (pending ? \"! \" : \"\")) : \"\")"
"%-34(account)"
" %12(calculated ? \"\" : justify(scrub(amount), 12, -1, true))"
"%(has_cost & !cost_calculated ?"
" \" @ \" + justify(scrub(abs(cost / amount)), 0) : \"\")"
"%(comment)\n%/\n");
" %$7%$8 %$9%$A%$B\n%/\n");
});
OPTION_(report_t, quantity, DO() { // -O
@ -733,15 +715,7 @@ public:
" %(justify(scrub(display_total), total_width, "
" 4 + date_width + payee_width + account_width + amount_width "
" + 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))"
" %(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, color))\n");
"%(justify(\" \", 2 + date_width + payee_width))%$3 %$4 %$5\n");
});
OPTION(report_t, related); // -r

View file

@ -1558,6 +1558,11 @@ void value_t::print(std::ostream& out,
break;
}
case BALANCE:
as_balance().print(out, first_width, latter_width, right_justify,
colorize);
break;
case STRING:
justify(out, as_string(), first_width, right_justify);
break;
@ -1582,11 +1587,6 @@ void value_t::print(std::ostream& out,
break;
}
case BALANCE:
as_balance().print(out, first_width, latter_width, right_justify,
colorize);
break;
case POINTER:
out << "<POINTER>";
break;