diff --git a/hledger-lib/Hledger/Reports/BudgetReport.hs b/hledger-lib/Hledger/Reports/BudgetReport.hs index cf9c5e929..e3cd77886 100644 --- a/hledger-lib/Hledger/Reports/BudgetReport.hs +++ b/hledger-lib/Hledger/Reports/BudgetReport.hs @@ -260,20 +260,17 @@ budgetReportAsText ropts@ReportOpts{..} budgetr = budgetReportAsTable :: ReportOpts -> BudgetReport -> Table String String (Maybe MixedAmount, Maybe MixedAmount) budgetReportAsTable ropts@ReportOpts{balancetype_} - (PeriodicReport periods rows (PeriodicReportRow _ coltots grandtot grandavg)) = + (PeriodicReport spans rows (PeriodicReportRow _ coltots grandtot grandavg)) = addtotalrow $ Table (T.Group NoLine $ map Header accts) (T.Group NoLine $ map Header colheadings) (map rowvals rows) where - colheadings = map mkheading periods + colheadings = map (reportPeriodName balancetype_ spans) spans ++ [" Total" | row_total_ ropts] ++ ["Average" | average_ ropts] - where - mkheading = case balancetype_ of - PeriodChange -> showDateSpanMonthAbbrev - _ -> maybe "" (showDate . prevday) . spanEnd + accts = map renderacct rows -- FIXME. Have to check explicitly for which to render here, since -- budgetReport sets accountlistmode to ALTree. Find a principled way to do @@ -290,6 +287,25 @@ budgetReportAsTable ++ [grandavg | average_ ropts && not (null coltots)] )) +-- | Make a name for the given period in a multiperiod report, given +-- the type of balance being reported and the full set of report +-- periods. This will be used as a column heading (or row heading, in +-- a register summary report). We try to pick a useful name as follows: +-- +-- - ending-balance reports: the period's end date +-- +-- - balance change reports where the periods are months and all in the same year: +-- the short month name in the current locale +-- +-- - all other balance change reports: a description of the datespan, +-- abbreviated to compact form if possible (see showDateSpan). +-- +reportPeriodName :: BalanceType -> [DateSpan] -> DateSpan -> String +reportPeriodName balancetype spans = + case balancetype of + PeriodChange -> showDateSpanMonthAbbrev + _ -> maybe "" (showDate . prevday) . spanEnd + -- tests tests_BudgetReport = tests "BudgetReport" [ diff --git a/hledger/Hledger/Cli/Commands/Balance.hs b/hledger/Hledger/Cli/Commands/Balance.hs index 4bd44e0ea..c143c6e1e 100644 --- a/hledger/Hledger/Cli/Commands/Balance.hs +++ b/hledger/Hledger/Cli/Commands/Balance.hs @@ -593,7 +593,7 @@ multiBalanceReportAsText ropts@ReportOpts{..} r = -- | Build a 'Table' from a multi-column balance report. balanceReportAsTable :: ReportOpts -> MultiBalanceReport -> Table String String MixedAmount balanceReportAsTable opts@ReportOpts{average_, row_total_, balancetype_} - (PeriodicReport colspans items (PeriodicReportRow _ coltotals tot avg)) = + (PeriodicReport spans items (PeriodicReportRow _ coltotals tot avg)) = maybetranspose $ addtotalrow $ Table @@ -602,13 +602,9 @@ balanceReportAsTable opts@ReportOpts{average_, row_total_, balancetype_} (map rowvals items) where totalscolumn = row_total_ && balancetype_ `notElem` [CumulativeChange, HistoricalBalance] - colheadings = map mkheading colspans + colheadings = map (reportPeriodName balancetype_ spans) spans ++ [" Total" | totalscolumn] ++ ["Average" | average_] - where - mkheading = case balancetype_ of - PeriodChange -> showDateSpanMonthAbbrev - _ -> maybe "" (showDate . prevday) . spanEnd accts = map renderacct items renderacct row = replicate ((prrDepth row - 1) * 2) ' ' ++ T.unpack (prrDisplayName row)