Added a new --colors option, for terminal fun

The following colors are applied in the balance and register reports:

    GREEN   To a date, if it occurs in the future
    BOLD    If a payee name relates to an uncleared entry
    BLUE    For account names
    RED     For negative values
This commit is contained in:
John Wiegley 2009-02-21 22:04:01 -04:00
parent 8ca91c1196
commit eb4aa87843
3 changed files with 66 additions and 21 deletions

View file

@ -90,8 +90,6 @@ and causes an immediate reloading of all journal files in the session.
.It Fl \-amount-data Pq Fl j
.It Fl \-amount-width Ar INT
.It Fl \-anon
.It Fl \-ansi
.It Fl \-ansi-invert
.It Fl \-args-only
.It Fl \-average Pq Fl A
.It Fl \-balance-format Ar FMT
@ -105,6 +103,7 @@ and causes an immediate reloading of all journal files in the session.
.It Fl \-code-as-payee
.It Fl \-collapse Pq Fl n
.It Fl \-collapse-if-zero
.It Fl \-colors
.It Fl \-columns Ar INT
.It Fl \-commodity-as-account
(Also \fB\-\-comm\-as\-account\fR).
@ -268,6 +267,17 @@ interpret parentheses, you should always escape them:
.It Nm actual
.It Nm amount
.It Nm amount_expr
.It Fn ansify_if value color bool
Render the given
.Ar value
as a string, applying the proper ANSI escape codes to display it in the given
.Ar color
if
.Ar bool
is true. It typically checks the value of the option
.Nm Fl \-colors ,
for example:
.Dl ansify_if(amount, "blue", options.colors)
.It Nm beg_line
.It Nm beg_pos
.It Nm calculated

View file

@ -220,6 +220,32 @@ value_t report_t::fn_format_date(call_scope_t& args)
return string_value(format_date(args[0].to_date(), args[1].to_string()));
}
value_t report_t::fn_ansify_if(call_scope_t& scope)
{
interactive_t args(scope, "vsb");
if (args.get<bool>(2)) {
string color = args.get<string>(1);
std::ostringstream buf;
if (color == "black") buf << "\e[30m";
else if (color == "red") buf << "\e[31m";
else if (color == "green") buf << "\e[32m";
else if (color == "yellow") buf << "\e[33m";
else if (color == "blue") buf << "\e[34m";
else if (color == "magenta") buf << "\e[35m";
else if (color == "cyan") buf << "\e[36m";
else if (color == "white") buf << "\e[37m";
else if (color == "bold") buf << "\e[1m";
else if (color == "underline") buf << "\e[4m";
else if (color == "blink") buf << "\e[5m";
buf << args.value_at(0);
buf << "\e[0m";
return string_value(buf.str());
} else {
return args.value_at(0);
}
}
namespace {
template <class Type = xact_t,
class handler_ptr = xact_handler_ptr,
@ -330,8 +356,7 @@ option_t<report_t> * report_t::lookup_option(const char * p)
else OPT(amount_);
else OPT(amount_data);
else OPT(anon);
else OPT(ansi);
else OPT(ansi_invert);
else OPT_ALT(colors, ansi);
else OPT(average);
else OPT(account_width_);
else OPT(amount_width_);
@ -353,6 +378,7 @@ option_t<report_t> * report_t::lookup_option(const char * p)
else OPT_ALT(comm_as_account, commodity_as_account);
else OPT(collapse);
else OPT(collapse_if_zero);
else OPT(colors);
else OPT(columns_);
else OPT_ALT(basis, cost);
else OPT_(current);
@ -492,6 +518,8 @@ expr_t::ptr_op_t report_t::lookup(const string& name)
case 'a':
if (is_eq(p, "amount_expr"))
return MAKE_FUNCTOR(report_t::fn_amount_expr);
else if (is_eq(p, "ansify_if"))
return MAKE_FUNCTOR(report_t::fn_ansify_if);
break;
case 'c':

View file

@ -142,6 +142,7 @@ public:
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);
value_t fn_ansify_if(call_scope_t& scope);
value_t fn_false(call_scope_t&) {
return false;
}
@ -226,17 +227,16 @@ public:
});
OPTION(report_t, anon);
OPTION(report_t, ansi);
OPTION(report_t, ansi_invert);
OPTION_(report_t, average, DO() { // -A
parent->HANDLER(display_total_).set_expr("total_expr/count");
});
OPTION__(report_t, balance_format_, CTOR(report_t, balance_format_) {
on("%20(print(scrub(display_total), 20))"
on("%(ansify_if(print(scrub(display_total), 20), \"red\", "
" colors & scrub(display_total) < 0))"
" %(!options.flat ? depth_spacer : \"\")"
"%-(partial_account(options.flat))\n");
"%-(ansify_if(partial_account(options.flat), \"blue\", colors))\n");
});
OPTION(report_t, base);
@ -272,6 +272,7 @@ public:
OPTION(report_t, comm_as_payee); // -x
OPTION(report_t, code_as_account);
OPTION(report_t, comm_as_account);
OPTION(report_t, colors);
OPTION_(report_t, collapse, DO() { // -n
// Make sure that balance reports are collapsed too, but only apply it
@ -494,20 +495,26 @@ public:
});
OPTION__(report_t, register_format_, CTOR(report_t, register_format_) {
on("%(print(date, date_width))"
" %(print(truncate(payee, payee_width), payee_width))"
" %(print(truncate(account, account_width, abbrev_len), account_width))"
" %(print(scrub(display_amount), amount_width, "
"3 + date_width + payee_width + account_width + amount_width))"
" %(print(scrub(display_total), total_width, "
"4 + date_width + payee_width + account_width + amount_width "
"+ total_width))\n%/"
on("%(ansify_if(print(date, date_width), \"green\", colors & date > today))"
" %(ansify_if(print(truncate(payee, payee_width), payee_width), "
" \"bold\", colors & !cleared))"
" %(ansify_if(print(truncate(account, account_width, abbrev_len), "
" account_width), \"blue\", colors))"
" %(ansify_if(print(scrub(display_amount), amount_width, "
" 3 + date_width + payee_width + account_width + amount_width), "
" \"red\", colors & scrub(display_amount) < 0))"
" %(ansify_if(print(scrub(display_total), total_width, "
" 4 + date_width + payee_width + account_width + amount_width "
" + total_width), \"red\", colors & scrub(display_amount) < 0))\n%/"
"%(print(\" \", 2 + date_width + payee_width))"
"%(print(truncate(account, account_width, abbrev_len), account_width))"
" %(print(scrub(display_amount), amount_width, 3 + date_width "
"+ payee_width + account_width + amount_width))"
" %(print(scrub(display_total), total_width, 4 + date_width "
"+ payee_width + account_width + amount_width + total_width))\n");
"%(ansify_if(print(truncate(account, account_width, abbrev_len), "
" account_width), \"blue\", colors))"
" %(ansify_if(print(scrub(display_amount), amount_width, "
" 3 + date_width + payee_width + account_width + amount_width), "
" \"red\", colors & scrub(display_amount) < 0))"
" %(ansify_if(print(scrub(display_total), total_width, "
" 4 + date_width + payee_width + account_width + amount_width "
" + total_width), \"red\", colors & scrub(display_amount) < 0))\n");
});
OPTION(report_t, related); // -r