Fixed the way values are justified for printing

This commit is contained in:
John Wiegley 2009-02-22 03:14:58 -04:00
parent 640279c65d
commit 04fd1ae24c
6 changed files with 54 additions and 42 deletions

View file

@ -215,7 +215,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
const int latter_width,
const bool right_justify) const
{
bool first = true;
int lwidth = latter_width;
@ -244,13 +245,12 @@ void balance_t::print(std::ostream& out,
std::ostringstream buf;
buf << *amount;
justify(out, buf.str(), width, true);
justify(out, buf.str(), width, right_justify);
}
if (first) {
std::ostringstream buf;
buf << amount_t(0L);
justify(out, buf.str(), first_width, true);
out.width(first_width);
out << (right_justify ? std::right : std::left) << 0;
}
}

View file

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

View file

@ -174,15 +174,16 @@ value_t report_t::fn_truncate(call_scope_t& args)
env.has(2) ? env.get<long>(2) : -1));
}
value_t report_t::fn_print(call_scope_t& args)
value_t report_t::fn_justify(call_scope_t& scope)
{
interactive_t env(args, "vl&ls");
interactive_t args(scope, "vl&lbs");
std::ostringstream out;
env.value_at(0)
args.value_at(0)
.strip_annotations(what_to_keep())
.print(out, env.get<long>(1),
env.has(2) ? env.get<long>(2) : -1,
env.has(3) ? env.get<string>(3) :
.print(out, args.get<long>(1),
args.has(2) ? args.get<long>(2) : -1,
args.has(3),
args.has(4) ? args.get<string>(4) :
(HANDLED(date_format_) ?
HANDLER(date_format_).str() : optional<string>()));
return string_value(out.str());
@ -602,7 +603,9 @@ expr_t::ptr_op_t report_t::lookup(const string& name)
break;
case 'j':
if (is_eq(p, "join"))
if (is_eq(p, "justify"))
return MAKE_FUNCTOR(report_t::fn_justify);
else if (is_eq(p, "join"))
return MAKE_FUNCTOR(report_t::fn_join);
break;
@ -648,8 +651,6 @@ expr_t::ptr_op_t report_t::lookup(const string& name)
break;
}
}
else if (is_eq(p, "print"))
return MAKE_FUNCTOR(report_t::fn_print);
break;
case 'q':

View file

@ -138,7 +138,7 @@ public:
value_t fn_quantity(call_scope_t& scope);
value_t fn_rounded(call_scope_t& scope);
value_t fn_truncate(call_scope_t& scope);
value_t fn_print(call_scope_t& scope);
value_t fn_justify(call_scope_t& scope);
value_t fn_quoted(call_scope_t& scope);
value_t fn_join(call_scope_t& scope);
value_t fn_format_date(call_scope_t& scope);
@ -233,7 +233,7 @@ public:
});
OPTION__(report_t, balance_format_, CTOR(report_t, balance_format_) {
on("%(ansify_if(print(scrub(display_total), 20), \"red\", "
on("%(ansify_if(justify(scrub(display_total), 20, -1, true), \"red\", "
" color & scrub(display_total) < 0))"
" %(!options.flat ? depth_spacer : \"\")"
"%-(ansify_if(partial_account(options.flat), \"blue\", color))\n");
@ -475,10 +475,12 @@ public:
"%(code ? \" (\" + code + \")\" : \"\") %(payee)%(entry.comment | \"\")\n"
" %(entry.uncleared ? (cleared ? \"* \" : (pending ? \"! \" : \"\")) : \"\")"
"%-34(account)"
" %12(calculated ? \"\" : scrub(amount))%(comment | \"\")\n%/"
" %12(calculated ? \"\" : justify(scrub(amount), 12, -1, true))"
"%(comment | \"\")\n%/"
" %(entry.uncleared ? (cleared ? \"* \" : (pending ? \"! \" : \"\")) : \"\")"
"%-34(account)"
" %12(calculated ? \"\" : scrub(amount))%(comment | \"\")\n%/\n");
" %12(calculated ? \"\" : justify(scrub(amount), 12, -1, true))"
"%(comment | \"\")\n%/\n");
});
OPTION_(report_t, quantity, DO() { // -O
@ -498,26 +500,26 @@ public:
});
OPTION__(report_t, register_format_, CTOR(report_t, register_format_) {
on("%(ansify_if(print(date, date_width), \"green\", color & date > today))"
" %(ansify_if(print(truncate(payee, payee_width), payee_width), "
on("%(ansify_if(justify(date, date_width), \"green\", color & date > today))"
" %(ansify_if(justify(truncate(payee, payee_width), payee_width), "
" \"bold\", color & !cleared))"
" %(ansify_if(print(truncate(account, account_width, abbrev_len), "
" %(ansify_if(justify(truncate(account, account_width, abbrev_len), "
" account_width), \"blue\", color))"
" %(ansify_if(print(scrub(display_amount), amount_width, "
" 3 + date_width + payee_width + account_width + amount_width), "
" %(ansify_if(justify(scrub(display_amount), amount_width, "
" 3 + date_width + payee_width + account_width + amount_width, true), "
" \"red\", color & scrub(display_amount) < 0))"
" %(ansify_if(print(scrub(display_total), total_width, "
" %(ansify_if(justify(scrub(display_total), total_width, "
" 4 + date_width + payee_width + account_width + amount_width "
" + total_width), \"red\", color & scrub(display_amount) < 0))\n%/"
"%(print(\" \", 2 + date_width + payee_width))"
"%(ansify_if(print(truncate(account, account_width, abbrev_len), "
" + total_width, true), \"red\", color & scrub(display_amount) < 0))\n%/"
"%(justify(\" \", 2 + date_width + payee_width))"
"%(ansify_if(justify(truncate(account, account_width, abbrev_len), "
" account_width), \"blue\", color))"
" %(ansify_if(print(scrub(display_amount), amount_width, "
" 3 + date_width + payee_width + account_width + amount_width), "
" %(ansify_if(justify(scrub(display_amount), amount_width, "
" 3 + date_width + payee_width + account_width + amount_width, true), "
" \"red\", color & scrub(display_amount) < 0))"
" %(ansify_if(print(scrub(display_total), total_width, "
" %(ansify_if(justify(scrub(display_total), total_width, "
" 4 + date_width + payee_width + account_width + amount_width "
" + total_width), \"red\", color & scrub(display_amount) < 0))\n");
" + total_width, true), \"red\", color & scrub(display_amount) < 0))\n");
});
OPTION(report_t, related); // -r

View file

@ -1317,12 +1317,17 @@ value_t value_t::strip_annotations(const keep_details_t& what_to_keep) const
void value_t::print(std::ostream& out,
const int first_width,
const int latter_width,
const bool right_justify,
const optional<string>& date_format) const
{
if (first_width > 0 &&
! is_amount() && ! is_balance() && ! is_string()) {
out.width(first_width);
out << std::left;
if (right_justify)
out << std::right;
else
out << std::left;
}
switch (type()) {
@ -1355,17 +1360,17 @@ void value_t::print(std::ostream& out,
case AMOUNT: {
if (as_amount().is_zero()) {
out.width(first_width);
out << std::right << 0L;
out << (right_justify ? std::right : std::left) << 0;
} else {
std::ostringstream buf;
buf << as_amount();
justify(out, buf.str(), first_width, true);
justify(out, buf.str(), first_width, right_justify);
}
break;
}
case STRING:
justify(out, as_string(), first_width);
justify(out, as_string(), first_width, right_justify);
break;
case MASK:
@ -1381,14 +1386,15 @@ void value_t::print(std::ostream& out,
else
out << ", ";
value.print(out, first_width, latter_width, date_format);
value.print(out, first_width, latter_width, right_justify,
date_format);
}
out << ')';
break;
}
case BALANCE:
as_balance().print(out, first_width, latter_width);
as_balance().print(out, first_width, latter_width, right_justify);
break;
case POINTER:

View file

@ -890,9 +890,10 @@ public:
* Printing methods.
*/
void print(std::ostream& out,
const int first_width = - 1,
const int latter_width = -1,
const optional<string>& date_format = none) const;
const int first_width = -1,
const int latter_width = -1,
const bool right_justify = false,
const optional<string>& date_format = none) const;
void dump(std::ostream& out, const bool relaxed = true) const;
/**