diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/report.cc | 32 | ||||
-rw-r--r-- | src/report.h | 41 |
2 files changed, 54 insertions, 19 deletions
diff --git a/src/report.cc b/src/report.cc index 3112a98e..3178755f 100644 --- a/src/report.cc +++ b/src/report.cc @@ -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': diff --git a/src/report.h b/src/report.h index 7897373e..bc87ac07 100644 --- a/src/report.h +++ b/src/report.h @@ -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 |