From a7f7c3181fdf52d2ad7e06b9d7976de2ff655fc1 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Sat, 26 Jul 2014 16:54:18 -0700 Subject: [PATCH] balance: show compact headings for common intervals In periodic multicolumn balance reports, column headings now show a more compact description of common periods (years, half-years, quarters, months, weeks) for better readability and screen space efficiency. --- hledger-lib/Hledger/Data/Dates.hs | 33 ++++++++++- tests/balance/intervals.test | 96 +++++++++++++++---------------- 2 files changed, 80 insertions(+), 49 deletions(-) diff --git a/hledger-lib/Hledger/Data/Dates.hs b/hledger-lib/Hledger/Data/Dates.hs index fd84bd60f..776143dfb 100644 --- a/hledger-lib/Hledger/Data/Dates.hs +++ b/hledger-lib/Hledger/Data/Dates.hs @@ -66,6 +66,7 @@ import Data.Maybe import Data.Time.Format import Data.Time.Calendar import Data.Time.Calendar.OrdinalDate +import Data.Time.Calendar.WeekDate import Data.Time.Clock import Data.Time.LocalTime import Safe (headMay, lastMay, readMay) @@ -85,7 +86,37 @@ instance Show DateSpan where showDate :: Day -> String showDate = formatTime defaultTimeLocale "%0C%y/%m/%d" -showDateSpan (DateSpan from to) = +-- | Render a datespan as a display string, abbreviating into a +-- compact form if possible. +showDateSpan ds@(DateSpan (Just from) (Just to)) = + case (toGregorian from, toGregorian to) of + -- special cases we can abbreviate: + -- a year, YYYY + ((fy,1,1), (ty,1,1)) | fy+1==ty -> formatTime defaultTimeLocale "%0C%y" from + -- a month, YYYY/MM + ((fy,fm,1), (ty,tm,1)) | fy==ty && fm+1==tm -> formatTime defaultTimeLocale "%0C%y/%m" from + ((fy,12,1), (ty,1,1)) | fy+1==ty -> formatTime defaultTimeLocale "%0C%y/%m" from + -- a quarter, YYYYqN + ((fy,1,1), (ty,4,1)) | fy==ty -> formatTime defaultTimeLocale "%0C%yq1" from + ((fy,4,1), (ty,7,1)) | fy==ty -> formatTime defaultTimeLocale "%0C%yq2" from + ((fy,7,1), (ty,10,1)) | fy==ty -> formatTime defaultTimeLocale "%0C%yq3" from + ((fy,10,1), (ty,1,1)) | fy+1==ty -> formatTime defaultTimeLocale "%0C%yq4" from + -- a half, YYYYhN + ((fy,1,1), (ty,7,1)) | fy==ty -> formatTime defaultTimeLocale "%0C%yh1" from + ((fy,7,1), (ty,1,1)) | fy+1==ty -> formatTime defaultTimeLocale "%0C%yh2" from + -- a week (two successive mondays), + -- YYYYwN ("week N of year YYYY") + -- _ | let ((fy,fw,fd), (ty,tw,td)) = (toWeekDate from, toWeekDate to) in fy==ty && fw+1==tw && fd==1 && td==1 + -- -> formatTime defaultTimeLocale "%0f%gw%V" from + -- YYYY/MM/DDwN ("week N, starting on YYYY/MM/DD") + _ | let ((fy,fw,fd), (ty,tw,td)) = (toWeekDate from, toWeekDate to) in fy==ty && fw+1==tw && fd==1 && td==1 + -> formatTime defaultTimeLocale "%0C%y/%m/%dw%V" from + -- otherwise, YYYY/MM/DD-YYYY/MM/DD + _ -> showDateSpan' ds +showDateSpan ds = showDateSpan' ds + +-- | Render a datespan as a display string. +showDateSpan' (DateSpan from to) = concat [maybe "" showDate from ,"-" diff --git a/tests/balance/intervals.test b/tests/balance/intervals.test index 1a94f595c..929489e17 100644 --- a/tests/balance/intervals.test +++ b/tests/balance/intervals.test @@ -16,13 +16,13 @@ hledgerdev -f balance-multicol.journal balance -p 'monthly in 2013' --no-total >>> Change of balance (flow): - || 2013/01/01-2013/01/31 2013/02/01-2013/02/28 2013/03/01-2013/03/31 -=================++====================================================================== - assets || 0 1 0 - assets:cash || 0 1 0 - assets:checking || 0 0 1 ------------------++---------------------------------------------------------------------- - || + || 2013/01 2013/02 2013/03 +=================++============================ + assets || 0 1 0 + assets:cash || 0 1 0 + assets:checking || 0 0 1 +-----------------++---------------------------- + || >>>=0 @@ -39,11 +39,11 @@ hledgerdev -f - balance -p 'quarterly in 2013' --empty >>> Change of balance (flow): - || 2013/01/01-2013/03/31 2013/04/01-2013/06/30 2013/07/01-2013/09/30 2013/10/01-2013/12/31 -===++============================================================================================= - a || 2 0 0 0 ----++--------------------------------------------------------------------------------------------- - || 2 0 0 0 + || 2013q1 2013q2 2013q3 2013q4 +===++================================= + a || 2 0 0 0 +---++--------------------------------- + || 2 0 0 0 >>>=0 @@ -100,12 +100,12 @@ hledgerdev -f balance-multicol.journal balance -p 'monthly in 2013' not:assets$ >>> Change of balance (flow): - || 2013/01/01-2013/01/31 2013/02/01-2013/02/28 2013/03/01-2013/03/31 -=================++====================================================================== - assets:cash || 0 1 0 - assets:checking || 0 0 1 ------------------++---------------------------------------------------------------------- - || 0 1 1 + || 2013/01 2013/02 2013/03 +=================++============================ + assets:cash || 0 1 0 + assets:checking || 0 0 1 +-----------------++---------------------------- + || 0 1 1 >>>=0 @@ -145,11 +145,11 @@ hledgerdev -f balance-multicol.journal balance -p 'monthly in 2013' --depth 1 >>> Change of balance (flow): - || 2013/01/01-2013/01/31 2013/02/01-2013/02/28 2013/03/01-2013/03/31 -========++====================================================================== - assets || 0 2 1 ---------++---------------------------------------------------------------------- - || 0 2 1 + || 2013/01 2013/02 2013/03 +========++============================ + assets || 0 2 1 +--------++---------------------------- + || 0 2 1 >>>=0 @@ -158,11 +158,11 @@ hledgerdev -f balance-multicol.journal balance -p 'monthly in 2013' --depth 1 as >>> Change of balance (flow): - || 2013/01/01-2013/01/31 2013/02/01-2013/02/28 2013/03/01-2013/03/31 -========++====================================================================== - assets || 0 1 1 ---------++---------------------------------------------------------------------- - || 0 1 1 + || 2013/01 2013/02 2013/03 +========++============================ + assets || 0 1 1 +--------++---------------------------- + || 0 1 1 >>>=0 @@ -197,13 +197,13 @@ hledgerdev -f balance-multicol.journal balance -p 'monthly in 2013' --tree >>> Change of balance (flow): - || 2013/01/01-2013/01/31 2013/02/01-2013/02/28 2013/03/01-2013/03/31 -============++====================================================================== - assets || 0 2 1 - cash || 0 1 0 - checking || 0 0 1 -------------++---------------------------------------------------------------------- - || 0 2 1 + || 2013/01 2013/02 2013/03 +============++============================ + assets || 0 2 1 + cash || 0 1 0 + checking || 0 0 1 +------------++---------------------------- + || 0 2 1 >>>=0 @@ -249,12 +249,12 @@ hledgerdev -f- balance --monthly --date2 >>> Change of balance (flow): - || 2014/01/01-2014/01/31 -===++======================== - a || 1 - b || 1 ----++------------------------ - || 2 + || 2014/01 +===++========== + a || 1 + b || 1 +---++---------- + || 2 >>>=0 @@ -273,12 +273,12 @@ hledgerdev -f- balance -p 'monthly 2014/1/10-2014/2/20' >>> Change of balance (flow): - || 2014/01/01-2014/01/31 2014/02/01-2014/02/28 -========++=============================================== - after || 0 1 - before || 1 0 - within || 0 1 ---------++----------------------------------------------- - || 1 2 + || 2014/01 2014/02 +========++=================== + after || 0 1 + before || 1 0 + within || 0 1 +--------++------------------- + || 1 2 >>>=0