escape codes in format strings; can now redefine individual report formats
This commit is contained in:
parent
5619a1d5be
commit
02168c7823
5 changed files with 65 additions and 19 deletions
41
config.cc
41
config.cc
|
|
@ -5,15 +5,15 @@ namespace ledger {
|
||||||
|
|
||||||
config_t * config = NULL;
|
config_t * config = NULL;
|
||||||
|
|
||||||
const std::string bal_fmt = "%20T %2_%-n\n";
|
std::string bal_fmt = "%20T %2_%-n\n";
|
||||||
const std::string reg_fmt
|
std::string reg_fmt
|
||||||
= "%D %-.20P %-.22N %12.66t %12.80T\n\
|
= "%D %-.20P %-.22N %12.66t %12.80T\n\
|
||||||
%/ %-.22N %12.66t %12.80T\n";
|
%/ %-.22N %12.66t %12.80T\n";
|
||||||
const std::string plot_value_fmt = "%D %t\n";
|
std::string plot_value_fmt = "%D %t\n";
|
||||||
const std::string plot_total_fmt = "%D %T\n";
|
std::string plot_total_fmt = "%D %T\n";
|
||||||
const std::string print_fmt
|
std::string print_fmt
|
||||||
= "\n%D %X%C%P\n %-34N %12o\n%/ %-34N %12o\n";
|
= "\n%D %X%C%P\n %-34N %12o\n%/ %-34N %12o\n";
|
||||||
const std::string equity_fmt
|
std::string equity_fmt
|
||||||
= "\n%D %X%C%P\n%/ %-34N %12t\n";
|
= "\n%D %X%C%P\n%/ %-34N %12t\n";
|
||||||
|
|
||||||
config_t::config_t()
|
config_t::config_t()
|
||||||
|
|
@ -71,7 +71,10 @@ Report filtering:\n\
|
||||||
-R, --real consider only non-virtual transactions\n\n\
|
-R, --real consider only non-virtual transactions\n\n\
|
||||||
-r, --related calculate report using related transactions\n\
|
-r, --related calculate report using related transactions\n\
|
||||||
Output customization:\n\
|
Output customization:\n\
|
||||||
-F, --format STR use STR as the report format\n\
|
-F, --format STR use STR as the format; for each report type, use:\n\
|
||||||
|
--balance-format --equity-format\n\
|
||||||
|
--register-format --plot-value-format\n\
|
||||||
|
--print-format --plot-total-format\n\
|
||||||
-y, --date-format STR use STR as the date format (def: %Y/%m/%d)\n\
|
-y, --date-format STR use STR as the date format (def: %Y/%m/%d)\n\
|
||||||
-E, --empty balance: show accounts with zero balance\n\
|
-E, --empty balance: show accounts with zero balance\n\
|
||||||
-n, --collapse register: collapse entries with multiple transactions\n\
|
-n, --collapse register: collapse entries with multiple transactions\n\
|
||||||
|
|
@ -213,6 +216,30 @@ OPT_BEGIN(date_format, "y:") {
|
||||||
config->date_format = optarg;
|
config->date_format = optarg;
|
||||||
} OPT_END(date_format);
|
} OPT_END(date_format);
|
||||||
|
|
||||||
|
OPT_BEGIN(balance_format, ":") {
|
||||||
|
bal_fmt = optarg;
|
||||||
|
} OPT_END(balance_format);
|
||||||
|
|
||||||
|
OPT_BEGIN(register_format, ":") {
|
||||||
|
reg_fmt = optarg;
|
||||||
|
} OPT_END(register_format);
|
||||||
|
|
||||||
|
OPT_BEGIN(plot_value_format, ":") {
|
||||||
|
plot_value_fmt = optarg;
|
||||||
|
} OPT_END(plot_value_format);
|
||||||
|
|
||||||
|
OPT_BEGIN(plot_total_format, ":") {
|
||||||
|
plot_total_fmt = optarg;
|
||||||
|
} OPT_END(plot_total_format);
|
||||||
|
|
||||||
|
OPT_BEGIN(print_format, ":") {
|
||||||
|
print_fmt = optarg;
|
||||||
|
} OPT_END(print_format);
|
||||||
|
|
||||||
|
OPT_BEGIN(equity_format, ":") {
|
||||||
|
equity_fmt = optarg;
|
||||||
|
} OPT_END(equity_format);
|
||||||
|
|
||||||
OPT_BEGIN(empty, "E") {
|
OPT_BEGIN(empty, "E") {
|
||||||
config->show_empty = true;
|
config->show_empty = true;
|
||||||
} OPT_END(empty);
|
} OPT_END(empty);
|
||||||
|
|
|
||||||
12
config.h
12
config.h
|
|
@ -8,12 +8,12 @@
|
||||||
|
|
||||||
namespace ledger {
|
namespace ledger {
|
||||||
|
|
||||||
extern const std::string bal_fmt;
|
extern std::string bal_fmt;
|
||||||
extern const std::string reg_fmt;
|
extern std::string reg_fmt;
|
||||||
extern const std::string plot_value_fmt;
|
extern std::string plot_value_fmt;
|
||||||
extern const std::string plot_total_fmt;
|
extern std::string plot_total_fmt;
|
||||||
extern const std::string print_fmt;
|
extern std::string print_fmt;
|
||||||
extern const std::string equity_fmt;
|
extern std::string equity_fmt;
|
||||||
|
|
||||||
struct config_t
|
struct config_t
|
||||||
{
|
{
|
||||||
|
|
|
||||||
16
format.cc
16
format.cc
|
|
@ -53,7 +53,7 @@ element_t * format_t::parse_elements(const std::string& fmt)
|
||||||
char * q = buf;
|
char * q = buf;
|
||||||
|
|
||||||
for (const char * p = fmt.c_str(); *p; p++) {
|
for (const char * p = fmt.c_str(); *p; p++) {
|
||||||
if (*p != '%') {
|
if (*p != '%' && *p != '\\') {
|
||||||
*q++ = *p;
|
*q++ = *p;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
@ -75,6 +75,20 @@ element_t * format_t::parse_elements(const std::string& fmt)
|
||||||
current = current->next;
|
current = current->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (*p == '\\') {
|
||||||
|
p++;
|
||||||
|
current->type = element_t::STRING;
|
||||||
|
switch (*p) {
|
||||||
|
case 'b': current->chars = "\b"; break;
|
||||||
|
case 'f': current->chars = "\f"; break;
|
||||||
|
case 'n': current->chars = "\n"; break;
|
||||||
|
case 'r': current->chars = "\r"; break;
|
||||||
|
case 't': current->chars = "\t"; break;
|
||||||
|
case 'v': current->chars = "\v"; break;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
++p;
|
++p;
|
||||||
if (*p == '-') {
|
if (*p == '-') {
|
||||||
current->align_left = true;
|
current->align_left = true;
|
||||||
|
|
|
||||||
8
option.h
8
option.h
|
|
@ -41,11 +41,11 @@ void process_environment(char ** envp, const std::string& tag);
|
||||||
std::vector<option_t> option_handler::options; \
|
std::vector<option_t> option_handler::options; \
|
||||||
option_handler_map option_handler::handlers
|
option_handler_map option_handler::handlers
|
||||||
|
|
||||||
#define OPT_BEGIN(tag, chars) \
|
#define OPT_BEGIN(tag, chars) \
|
||||||
static struct tag ## _handler : public option_handler { \
|
static struct opt_ ## tag ## _handler : public option_handler { \
|
||||||
tag ## _handler() : option_handler(#tag, chars) {} \
|
opt_ ## tag ## _handler() : option_handler(#tag, chars) {} \
|
||||||
virtual void handle_option(const char * optarg)
|
virtual void handle_option(const char * optarg)
|
||||||
|
|
||||||
#define OPT_END(tag) } tag ## _handler_obj
|
#define OPT_END(tag) } opt_ ## tag ## _handler_obj
|
||||||
|
|
||||||
#endif // _OPTION_H
|
#endif // _OPTION_H
|
||||||
|
|
|
||||||
|
|
@ -71,14 +71,19 @@ transaction_t * parse_transaction_text(char * line, account_t * account,
|
||||||
}
|
}
|
||||||
|
|
||||||
char * price_str = std::strchr(cost_str, '@');
|
char * price_str = std::strchr(cost_str, '@');
|
||||||
|
bool per_unit = true;
|
||||||
if (price_str) {
|
if (price_str) {
|
||||||
*price_str++ = '\0';
|
*price_str++ = '\0';
|
||||||
|
if (*price_str == '@') {
|
||||||
|
per_unit = false;
|
||||||
|
price_str++;
|
||||||
|
}
|
||||||
xact->cost = new amount_t;
|
xact->cost = new amount_t;
|
||||||
xact->cost->parse(price_str);
|
xact->cost->parse(price_str);
|
||||||
}
|
}
|
||||||
|
|
||||||
xact->amount.parse(cost_str);
|
xact->amount.parse(cost_str);
|
||||||
if (price_str)
|
if (price_str && per_unit)
|
||||||
*xact->cost *= xact->amount;
|
*xact->cost *= xact->amount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue