diff options
-rw-r--r-- | src/format.cc | 15 | ||||
-rw-r--r-- | src/format.h | 6 | ||||
-rw-r--r-- | src/report.h | 17 | ||||
-rw-r--r-- | test/baseline/opt-truncate.test | 144 |
4 files changed, 166 insertions, 16 deletions
diff --git a/src/format.cc b/src/format.cc index 4066cabd..6620e5a4 100644 --- a/src/format.cc +++ b/src/format.cc @@ -38,6 +38,9 @@ namespace ledger { +format_t::elision_style_t format_t::default_style = TRUNCATE_TRAILING; +bool format_t::default_style_changed = false; + void format_t::element_t::dump(std::ostream& out) const { out << "Element: "; @@ -386,22 +389,22 @@ string format_t::truncate(const unistring& ustr, std::size_t width, std::ostringstream buf; - elision_style_t style = TRUNCATE_TRAILING; - if (account_abbrev_length > 0) + elision_style_t style = default_style; + if (account_abbrev_length > 0 && ! default_style_changed) style = ABBREVIATE; switch (style) { case TRUNCATE_LEADING: // This method truncates at the beginning. - buf << ".." << ustr.extract(len - width, width); + buf << ".." << ustr.extract(len - (width - 2), width - 2); break; case TRUNCATE_MIDDLE: // This method truncates in the middle. - buf << ustr.extract(0, width / 2) + buf << ustr.extract(0, (width - 2) / 2) << ".." - << ustr.extract(len - (width / 2 + width % 2), - width / 2 + width % 2); + << ustr.extract(len - ((width - 2) / 2 + (width - 2) % 2), + (width - 2) / 2 + (width - 2) % 2); break; case ABBREVIATE: diff --git a/src/format.h b/src/format.h index 13dd0876..03ed28c7 100644 --- a/src/format.h +++ b/src/format.h @@ -107,12 +107,14 @@ class format_t : public noncopyable scoped_ptr<element_t> elements; public: - enum elision_style_t { + static enum elision_style_t { TRUNCATE_TRAILING, TRUNCATE_MIDDLE, TRUNCATE_LEADING, ABBREVIATE - }; + } default_style; + + static bool default_style_changed; private: static element_t * parse_elements(const string& fmt); diff --git a/src/report.h b/src/report.h index 45d4f11a..5a9fb8a5 100644 --- a/src/report.h +++ b/src/report.h @@ -52,6 +52,7 @@ #include "stream.h" #include "option.h" #include "commodity.h" +#include "format.h" namespace ledger { @@ -755,18 +756,18 @@ public: OPTION(report_t, total_data); - OPTION_(report_t, truncate_, DO() { -#if 0 + OPTION_(report_t, truncate_, DO_(args) { string style(args[1].to_string()); if (style == "leading") - format_t::elision_style = format_t::TRUNCATE_LEADING; + format_t::default_style = format_t::TRUNCATE_LEADING; else if (style == "middle") - format_t::elision_style = format_t::TRUNCATE_MIDDLE; + format_t::default_style = format_t::TRUNCATE_MIDDLE; else if (style == "trailing") - format_t::elision_style = format_t::TRUNCATE_TRAILING; - else if (style == "abbrev") - format_t::elision_style = format_t::ABBREVIATE; -#endif + format_t::default_style = format_t::TRUNCATE_TRAILING; + else + throw_(std::invalid_argument, + _("Unrecognized truncation style: '%1'") << style); + format_t::default_style_changed = true; }); OPTION_(report_t, unbudgeted, DO() { diff --git a/test/baseline/opt-truncate.test b/test/baseline/opt-truncate.test new file mode 100644 index 00000000..0f9190c2 --- /dev/null +++ b/test/baseline/opt-truncate.test @@ -0,0 +1,144 @@ +reg airfare --truncate=leading +<<< +2008/01/11 LIAT + Expenses:Travel:Airfare $40.00 + Liabilities:MasterCard + +2008/01/14 cheaptickets.com + Expenses:Travel:Airfare $182.19 + Liabilities:MasterCard + +2008/02/05 CTX + Expenses:Travel:Auto $240.38 + Liabilities:MasterCard + +2008/02/05 UNITED + Expenses:Travel:Airfare $238.80 + Liabilities:MasterCard + +2008/02/05 UNITED + Expenses:Travel:Airfare $238.80 + Liabilities:MasterCard + +2008/02/22 BUDGET RENT-A-CAR + Expenses:Travel:Auto $40.59 + Liabilities:MasterCard + +2008/03/16 IBERIA + Expenses:Travel:Airfare $1,231.60 + Liabilities:MasterCard + +2008/03/16 IBERIA + Expenses:Travel:Airfare $1,231.60 + Liabilities:MasterCard + +2008/04/03 AMERICAN + Expenses:Travel:Airfare $155.86 + Liabilities:MasterCard + +2008/04/03 AMERICAN + Expenses:Travel:Airfare $155.86 + Liabilities:MasterCard + +2008/04/30 UNITED + Expenses:Travel:Airfare $437.21 + Liabilities:MasterCard + +2008/04/30 UNITED + Expenses:Travel:Airfare $437.21 + Liabilities:MasterCard + +2008/08/08 BCIS I-131 FILING FEE- + Expenses:Travel:Passport $170.00 + Liabilities:MasterCard + +2008/09/06 AMERICAN + Expenses:Travel:Airfare $912.60 + Liabilities:MasterCard + +2008/09/06 AMERICAN + Expenses:Travel:Airfare $912.60 + Liabilities:MasterCard + +2008/09/22 AGNT FEE + Expenses:Travel:Airfare $70.00 + Liabilities:MasterCard + +2008/09/22 DELTA + Expenses:Travel:Airfare $806.20 + Liabilities:MasterCard + +2008/09/22 DELTA + Expenses:Travel:Airfare $806.20 + Liabilities:MasterCard + +2008/09/22 LIAT 1974 LIMITED + Expenses:Travel:Airfare $418.34 + Liabilities:MasterCard + +2008/12/26 U.S. Department of State + Expenses:Travel:Passport $127.00 + Assets:Checking + +2008/12/26 U.S. Department of State + Expenses:Travel:Passport $127.00 + Assets:Checking +>>>1 +08-Jan-11 LIAT ..enses:Travel:Airfare $40.00 $40.00 +08-Jan-14 cheaptickets.com ..enses:Travel:Airfare $182.19 $222.19 +08-Feb-05 UNITED ..enses:Travel:Airfare $238.80 $460.99 +08-Feb-05 UNITED ..enses:Travel:Airfare $238.80 $699.79 +08-Mar-16 IBERIA ..enses:Travel:Airfare $1,231.60 $1,931.39 +08-Mar-16 IBERIA ..enses:Travel:Airfare $1,231.60 $3,162.99 +08-Apr-03 AMERICAN ..enses:Travel:Airfare $155.86 $3,318.85 +08-Apr-03 AMERICAN ..enses:Travel:Airfare $155.86 $3,474.71 +08-Apr-30 UNITED ..enses:Travel:Airfare $437.21 $3,911.92 +08-Apr-30 UNITED ..enses:Travel:Airfare $437.21 $4,349.13 +08-Sep-06 AMERICAN ..enses:Travel:Airfare $912.60 $5,261.73 +08-Sep-06 AMERICAN ..enses:Travel:Airfare $912.60 $6,174.33 +08-Sep-22 AGNT FEE ..enses:Travel:Airfare $70.00 $6,244.33 +08-Sep-22 DELTA ..enses:Travel:Airfare $806.20 $7,050.53 +08-Sep-22 DELTA ..enses:Travel:Airfare $806.20 $7,856.73 +08-Sep-22 LIAT 1974 LIMITED ..enses:Travel:Airfare $418.34 $8,275.07 +>>>2 +=== 0 +reg airfare --truncate=middle +>>>1 +08-Jan-11 LIAT Expenses:T..el:Airfare $40.00 $40.00 +08-Jan-14 cheaptickets.com Expenses:T..el:Airfare $182.19 $222.19 +08-Feb-05 UNITED Expenses:T..el:Airfare $238.80 $460.99 +08-Feb-05 UNITED Expenses:T..el:Airfare $238.80 $699.79 +08-Mar-16 IBERIA Expenses:T..el:Airfare $1,231.60 $1,931.39 +08-Mar-16 IBERIA Expenses:T..el:Airfare $1,231.60 $3,162.99 +08-Apr-03 AMERICAN Expenses:T..el:Airfare $155.86 $3,318.85 +08-Apr-03 AMERICAN Expenses:T..el:Airfare $155.86 $3,474.71 +08-Apr-30 UNITED Expenses:T..el:Airfare $437.21 $3,911.92 +08-Apr-30 UNITED Expenses:T..el:Airfare $437.21 $4,349.13 +08-Sep-06 AMERICAN Expenses:T..el:Airfare $912.60 $5,261.73 +08-Sep-06 AMERICAN Expenses:T..el:Airfare $912.60 $6,174.33 +08-Sep-22 AGNT FEE Expenses:T..el:Airfare $70.00 $6,244.33 +08-Sep-22 DELTA Expenses:T..el:Airfare $806.20 $7,050.53 +08-Sep-22 DELTA Expenses:T..el:Airfare $806.20 $7,856.73 +08-Sep-22 LIAT 1974 LIMITED Expenses:T..el:Airfare $418.34 $8,275.07 +>>>2 +=== 0 +reg airfare --truncate=trailing +>>>1 +08-Jan-11 LIAT Expenses:Travel:Airf.. $40.00 $40.00 +08-Jan-14 cheaptickets.com Expenses:Travel:Airf.. $182.19 $222.19 +08-Feb-05 UNITED Expenses:Travel:Airf.. $238.80 $460.99 +08-Feb-05 UNITED Expenses:Travel:Airf.. $238.80 $699.79 +08-Mar-16 IBERIA Expenses:Travel:Airf.. $1,231.60 $1,931.39 +08-Mar-16 IBERIA Expenses:Travel:Airf.. $1,231.60 $3,162.99 +08-Apr-03 AMERICAN Expenses:Travel:Airf.. $155.86 $3,318.85 +08-Apr-03 AMERICAN Expenses:Travel:Airf.. $155.86 $3,474.71 +08-Apr-30 UNITED Expenses:Travel:Airf.. $437.21 $3,911.92 +08-Apr-30 UNITED Expenses:Travel:Airf.. $437.21 $4,349.13 +08-Sep-06 AMERICAN Expenses:Travel:Airf.. $912.60 $5,261.73 +08-Sep-06 AMERICAN Expenses:Travel:Airf.. $912.60 $6,174.33 +08-Sep-22 AGNT FEE Expenses:Travel:Airf.. $70.00 $6,244.33 +08-Sep-22 DELTA Expenses:Travel:Airf.. $806.20 $7,050.53 +08-Sep-22 DELTA Expenses:Travel:Airf.. $806.20 $7,856.73 +08-Sep-22 LIAT 1974 LIMITED Expenses:Travel:Airf.. $418.34 $8,275.07 +>>>2 +=== 0 |