Skip to content

Commit 5d02eaf

Browse files
committed
cli: support for new layout bare-wide
1 parent a758eb0 commit 5d02eaf

File tree

4 files changed

+112
-34
lines changed

4 files changed

+112
-34
lines changed

hledger-lib/Hledger/Reports/ReportOptions.hs

+3-1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ instance Default AccountListMode where def = ALFlat
119119
data Layout = LayoutWide (Maybe Int)
120120
| LayoutTall
121121
| LayoutBare
122+
| LayoutBareWide
122123
| LayoutTidy
123124
deriving (Eq, Show)
124125

@@ -373,6 +374,7 @@ layoutopt rawopts = fromMaybe (LayoutWide Nothing) $ layout <|> column
373374
, ("tall", LayoutTall)
374375
, ("bare", LayoutBare)
375376
, ("tidy", LayoutTidy)
377+
, ("bare-wide", LayoutBareWide)
376378
]
377379
-- For `--layout=elided,n`, elide to the given width
378380
(s,n) = break (==',') $ map toLower opt
@@ -381,7 +383,7 @@ layoutopt rawopts = fromMaybe (LayoutWide Nothing) $ layout <|> column
381383
c | Just w' <- readMay c -> Just w'
382384
_ -> usageError "width in --layout=wide,WIDTH must be an integer"
383385

384-
err = usageError "--layout's argument should be \"wide[,WIDTH]\", \"tall\", \"bare\", or \"tidy\""
386+
err = usageError "--layout's argument should be \"wide[,WIDTH]\", \"tall\", \"bare\", \"bare-wide\", or \"tidy\""
385387

386388
-- Get the period specified by any -b/--begin, -e/--end and/or -p/--period
387389
-- options appearing in the command line.

hledger-lib/Hledger/Write/Spreadsheet.hs

+10
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ module Hledger.Write.Spreadsheet (
2424
rawTableContent,
2525
cellFromMixedAmount,
2626
cellsFromMixedAmount,
27+
cellFromAmount,
2728
) where
2829

2930
import qualified Hledger.Data.Amount as Amt
@@ -235,6 +236,15 @@ cellsFromMixedAmount bopts (cls, mixedAmt) =
235236
})
236237
(Amt.showMixedAmountLinesPartsB bopts mixedAmt)
237238

239+
cellFromAmount ::
240+
(Lines border) =>
241+
AmountFormat -> (Class, (wb, Amount)) -> Cell border wb
242+
cellFromAmount bopts (cls, (str,amt)) =
243+
(defaultCell str) {
244+
cellClass = cls,
245+
cellType = amountType bopts amt
246+
}
247+
238248
amountType :: AmountFormat -> Amount -> Type
239249
amountType bopts amt =
240250
TypeAmount $

hledger/Hledger/Cli/Commands/Balance.hs

+67-22
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ import Hledger.Write.Ods (printFods)
308308
import Hledger.Write.Html.Lucid (printHtml)
309309
import Hledger.Write.Spreadsheet (rawTableContent, headerCell,
310310
addHeaderBorders, addRowSpanHeader,
311-
cellFromMixedAmount, cellsFromMixedAmount)
311+
cellFromMixedAmount, cellsFromMixedAmount, cellFromAmount)
312312
import qualified Hledger.Write.Spreadsheet as Ods
313313

314314

@@ -594,6 +594,9 @@ renderComponent topaligned oneline opts (acctname, dep, total) (FormatField ljus
594594
}
595595

596596

597+
headerWithoutBorders :: [Ods.Cell () text] -> [Ods.Cell Ods.NumLines text]
598+
headerWithoutBorders = map (\c -> c {Ods.cellBorder = Ods.noBorder})
599+
597600
simpleDateSpanCell :: DateSpan -> Ods.Cell Ods.NumLines Text
598601
simpleDateSpanCell = Ods.defaultCell . showDateSpan
599602

@@ -622,8 +625,11 @@ balanceReportAsSpreadsheet opts (items, total) =
622625
headers =
623626
addHeaderBorders $ map headerCell $
624627
"account" : case layout_ opts of
628+
LayoutBareWide -> allCommodities
625629
LayoutBare -> ["commodity", "balance"]
626630
_ -> ["balance"]
631+
allCommodities =
632+
S.toAscList $ foldMap (\(_,_,_,ma) -> maCommodities ma) items
627633
rows ::
628634
RowClass -> BalanceReportItem ->
629635
[[Ods.Cell Ods.NumLines Text]]
@@ -635,6 +641,15 @@ balanceReportAsSpreadsheet opts (items, total) =
635641
cell $ renderBalanceAcct opts nbsp (name, dispName, dep) in
636642
addRowSpanHeader accountCell $
637643
case layout_ opts of
644+
LayoutBareWide ->
645+
let bopts =
646+
machineFmt {
647+
displayCommodity = False,
648+
displayCommodityOrder = Just allCommodities
649+
} in
650+
[map (\bldAmt ->
651+
fmap wbToText $ cellFromAmount bopts (amountClass rc, bldAmt)) $
652+
showMixedAmountLinesPartsB bopts ma]
638653
LayoutBare ->
639654
map (\a -> [cell $ acommodity a, renderAmount rc $ mixedAmount a])
640655
. amounts $ mixedAmountStripCosts ma
@@ -658,29 +673,41 @@ balanceReportAsSpreadsheet opts (items, total) =
658673
multiBalanceReportAsCsv :: ReportOpts -> MultiBalanceReport -> CSV
659674
multiBalanceReportAsCsv opts@ReportOpts{..} report =
660675
(if transpose_ then transpose else id) $
661-
rawTableContent $ header : body ++ totals
676+
rawTableContent $ header ++ body ++ totals
662677
where
663678
(header, body, totals) =
664-
multiBalanceReportAsSpreadsheetParts machineFmt opts report
679+
multiBalanceReportAsSpreadsheetParts machineFmt opts
680+
(allCommoditiesFromPeriodicReport $ prRows report) report
665681

666682
-- | Render the Spreadsheet table rows (CSV, ODS, HTML) for a MultiBalanceReport.
667683
-- Returns the heading row, 0 or more body rows, and the totals row if enabled.
668684
multiBalanceReportAsSpreadsheetParts ::
669-
AmountFormat -> ReportOpts -> MultiBalanceReport ->
670-
([Ods.Cell Ods.NumLines Text],
685+
AmountFormat -> ReportOpts ->
686+
[CommoditySymbol] -> MultiBalanceReport ->
687+
([[Ods.Cell Ods.NumLines Text]],
671688
[[Ods.Cell Ods.NumLines Text]],
672689
[[Ods.Cell Ods.NumLines Text]])
673-
multiBalanceReportAsSpreadsheetParts fmt opts@ReportOpts{..} (PeriodicReport colspans items tr) =
674-
(headers, concatMap fullRowAsTexts items, addTotalBorders totalrows)
690+
multiBalanceReportAsSpreadsheetParts fmt opts@ReportOpts{..}
691+
allCommodities (PeriodicReport colspans items tr) =
692+
(allHeaders, concatMap fullRowAsTexts items, addTotalBorders totalrows)
675693
where
676694
accountCell label =
677695
(Ods.defaultCell label) {Ods.cellClass = Ods.Class "account"}
678696
hCell cls label = (headerCell label) {Ods.cellClass = Ods.Class cls}
697+
allHeaders =
698+
case layout_ of
699+
LayoutBareWide ->
700+
[headerWithoutBorders $
701+
Ods.emptyCell :
702+
concatMap (Ods.horizontalSpan allCommodities) dateHeaders,
703+
headers]
704+
_ -> [headers]
679705
headers =
680706
addHeaderBorders $
681707
hCell "account" "account" :
682708
case layout_ of
683709
LayoutTidy -> map headerCell tidyColumnLabels
710+
LayoutBareWide -> dateHeaders >> map headerCell allCommodities
684711
LayoutBare -> headerCell "commodity" : dateHeaders
685712
_ -> dateHeaders
686713
dateHeaders =
@@ -701,7 +728,7 @@ multiBalanceReportAsSpreadsheetParts fmt opts@ReportOpts{..} (PeriodicReport col
701728
rowAsText Total simpleDateSpanCell tr
702729
rowAsText rc dsCell =
703730
map (map (fmap wbToText)) .
704-
multiBalanceRowAsCellBuilders fmt opts colspans rc dsCell
731+
multiBalanceRowAsCellBuilders fmt opts colspans allCommodities rc dsCell
705732

706733
tidyColumnLabels :: [Text]
707734
tidyColumnLabels =
@@ -721,10 +748,12 @@ multiBalanceReportAsSpreadsheet ::
721748
((Maybe Int, Maybe Int), [[Ods.Cell Ods.NumLines Text]])
722749
multiBalanceReportAsSpreadsheet ropts mbr =
723750
let (header,body,total) =
724-
multiBalanceReportAsSpreadsheetParts oneLineNoCostFmt ropts mbr
751+
multiBalanceReportAsSpreadsheetParts oneLineNoCostFmt ropts
752+
(allCommoditiesFromPeriodicReport $ prRows mbr) mbr
725753
in (if transpose_ ropts then swap *** Ods.transpose else id) $
726-
((Just 1, case layout_ ropts of LayoutWide _ -> Just 1; _ -> Nothing),
727-
header : body ++ total)
754+
((Just $ case layout_ ropts of LayoutBareWide -> 2; _ -> 1,
755+
case layout_ ropts of LayoutWide _ -> Just 1; _ -> Nothing),
756+
header ++ body ++ total)
728757

729758

730759
-- | Render a multi-column balance report as plain text suitable for console output.
@@ -795,19 +824,24 @@ multiBalanceReportAsTable opts@ReportOpts{summary_only_, average_, balanceaccum_
795824
(concat rows)
796825
where
797826
colheadings = ["Commodity" | layout_ opts == LayoutBare]
798-
++ (if not summary_only_ then map (reportPeriodName balanceaccum_ spans) spans else [])
827+
++ (if not summary_only_
828+
then case layout_ opts of
829+
LayoutBareWide -> spans >> allCommodities
830+
_ -> map (reportPeriodName balanceaccum_ spans) spans
831+
else [])
799832
++ [" Total" | multiBalanceHasTotalsColumn opts]
800833
++ ["Average" | average_]
834+
allCommodities = allCommoditiesFromPeriodicReport items
801835
(accts, rows) = unzip $ fmap fullRowAsTexts items
802836
where
803837
fullRowAsTexts row = (replicate (length rs) (renderacct row), rs)
804838
where
805-
rs = multiBalanceRowAsText opts row
839+
rs = multiBalanceRowAsText opts allCommodities row
806840
renderacct row' = T.replicate (prrIndent row' * 2) " " <> prrDisplayName row'
807841
addtotalrow
808842
| no_total_ opts = id
809843
| otherwise =
810-
let totalrows = multiBalanceRowAsText opts tr
844+
let totalrows = multiBalanceRowAsText opts allCommodities tr
811845
rowhdrs = Group NoLine $ map Header $ totalRowHeadingText : replicate (length totalrows - 1) ""
812846
colhdrs = Header [] -- unused, concatTables will discard
813847
in (flip (concatTables SingleLine) $ Table rowhdrs colhdrs totalrows)
@@ -816,12 +850,17 @@ multiBalanceReportAsTable opts@ReportOpts{summary_only_, average_, balanceaccum_
816850
multiColumnTableInterRowBorder = NoLine
817851
multiColumnTableInterColumnBorder = if pretty_ opts then SingleLine else NoLine
818852

853+
allCommoditiesFromPeriodicReport ::
854+
[PeriodicReportRow a MixedAmount] -> [CommoditySymbol]
855+
allCommoditiesFromPeriodicReport =
856+
S.toAscList . foldMap (foldMap maCommodities . prrAmounts)
857+
819858
multiBalanceRowAsCellBuilders ::
820-
AmountFormat -> ReportOpts -> [DateSpan] ->
859+
AmountFormat -> ReportOpts -> [DateSpan] -> [CommoditySymbol] ->
821860
RowClass -> (DateSpan -> Ods.Cell Ods.NumLines Text) ->
822861
PeriodicReportRow a MixedAmount ->
823862
[[Ods.Cell Ods.NumLines WideBuilder]]
824-
multiBalanceRowAsCellBuilders bopts ropts@ReportOpts{..} colspans
863+
multiBalanceRowAsCellBuilders bopts ropts@ReportOpts{..} colspans allCommodities
825864
rc renderDateSpanCell (PeriodicReportRow _acct as rowtot rowavg) =
826865
case layout_ of
827866
LayoutWide width -> [fmap (cellFromMixedAmount bopts{displayMaxWidth=width}) clsamts]
@@ -832,6 +871,8 @@ multiBalanceRowAsCellBuilders bopts ropts@ReportOpts{..} colspans
832871
. transpose -- each row becomes a list of Text quantities
833872
. map (cellsFromMixedAmount bopts{displayCommodity=False, displayCommodityOrder=Just cs, displayMinWidth=Nothing})
834873
$ clsamts
874+
LayoutBareWide -> [concatMap (cellsFromMixedAmount bopts{displayCommodity=False, displayCommodityOrder=Just allCommodities, displayMinWidth=Nothing})
875+
$ clsamts]
835876
LayoutTidy -> concat
836877
. zipWith (map . addDateColumns) colspans
837878
. map ( zipWith (\c a -> [wbCell c, a]) cs
@@ -874,16 +915,20 @@ multiBalanceHasTotalsColumn :: ReportOpts -> Bool
874915
multiBalanceHasTotalsColumn ropts =
875916
row_total_ ropts && balanceaccum_ ropts `notElem` [Cumulative, Historical]
876917

877-
multiBalanceRowAsText :: ReportOpts -> PeriodicReportRow a MixedAmount -> [[WideBuilder]]
878-
multiBalanceRowAsText opts =
918+
multiBalanceRowAsText ::
919+
ReportOpts -> [CommoditySymbol] -> PeriodicReportRow a MixedAmount -> [[WideBuilder]]
920+
multiBalanceRowAsText opts allCommodities =
879921
rawTableContent .
880-
multiBalanceRowAsCellBuilders oneLineNoCostFmt{displayColour=color_ opts} opts []
922+
multiBalanceRowAsCellBuilders oneLineNoCostFmt{displayColour=color_ opts}
923+
opts [] allCommodities
881924
Value simpleDateSpanCell
882925

883-
multiBalanceRowAsCsvText :: ReportOpts -> [DateSpan] -> PeriodicReportRow a MixedAmount -> [[T.Text]]
884-
multiBalanceRowAsCsvText opts colspans =
926+
multiBalanceRowAsCsvText ::
927+
ReportOpts -> [DateSpan] -> [CommoditySymbol] ->
928+
PeriodicReportRow a MixedAmount -> [[T.Text]]
929+
multiBalanceRowAsCsvText opts colspans allCommodities =
885930
map (map (wbToText . Ods.cellContent)) .
886-
multiBalanceRowAsCellBuilders machineFmt opts colspans
931+
multiBalanceRowAsCellBuilders machineFmt opts colspans allCommodities
887932
Value simpleDateSpanCell
888933

889934

hledger/Hledger/Cli/CompoundBalanceCommand.hs

+32-11
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import qualified Data.List.NonEmpty as NonEmpty
2323
import qualified Data.Text as T
2424
import qualified Data.Text.Lazy as TL
2525
import qualified Data.Text.Lazy.Builder as TB
26+
import qualified Data.Set as Set
2627
import Data.Time.Calendar (Day, addDays)
2728
import System.Console.CmdArgs.Explicit as C (Mode, flagNone, flagReq)
2829
import qualified System.IO as IO
@@ -285,12 +286,14 @@ compoundBalanceReportAsText ropts (CompoundPeriodicReport title _colspans subrep
285286
-- [COL1LINE1, COL2LINE1]
286287
-- [COL1LINE2, COL2LINE2]
287288
-- ]
288-
coltotalslines = multiBalanceRowAsText ropts totalsrow
289+
coltotalslines = multiBalanceRowAsText ropts allCommodities totalsrow
289290
totalstable = Table
290291
(Group NoLine $ map Header $ "Net:" : replicate (length coltotalslines - 1) "") -- row headers
291292
(Header []) -- column headers, concatTables will discard these
292293
coltotalslines -- cell values
293294

295+
allCommodities = allCommoditiesFromSubreports subreports
296+
294297
-- | Convert a named multi balance report to a table suitable for
295298
-- concatenating with others to make a compound balance report table.
296299
subreportAsTable ropts1 (title1, r, _) = tablewithtitle
@@ -355,14 +358,21 @@ compoundBalanceReportAsSpreadsheet fmt accountLabel maybeBlank ropts cbr =
355358
_ -> []
356359
dataHeaders =
357360
(guard (layout_ ropts /= LayoutTidy) >>) $
358-
map (Spr.headerCell . reportPeriodName (balanceaccum_ ropts) colspans)
361+
map (dataHeaderCell . reportPeriodName (balanceaccum_ ropts) colspans)
359362
colspans ++
360-
(guard (multiBalanceHasTotalsColumn ropts) >> [Spr.headerCell "Total"]) ++
361-
(guard (average_ ropts) >> [Spr.headerCell "Average"])
363+
(guard (multiBalanceHasTotalsColumn ropts) >> [dataHeaderCell "Total"]) ++
364+
(guard (average_ ropts) >> [dataHeaderCell "Average"])
365+
dataHeaderCell label =
366+
(Spr.headerCell label) {Spr.cellSpan = Spr.SpanHorizontal numSubColumns}
362367
headerrow = leadingHeaders ++ dataHeaders
363368

364369
blankrow =
365370
fmap (Spr.horizontalSpan headerrow . Spr.defaultCell) maybeBlank
371+
numSubColumns =
372+
case layout_ ropts of
373+
LayoutBareWide -> length allCommodities
374+
_ -> 1
375+
allCommodities = allCommoditiesFromSubreports subreports
366376

367377
-- Make rows for a subreport: its title row, not the headings row,
368378
-- the data rows, any totals row, and a blank row for whitespace.
@@ -371,14 +381,18 @@ compoundBalanceReportAsSpreadsheet fmt accountLabel maybeBlank ropts cbr =
371381
subreportrows (subreporttitle, mbr, _increasestotal) =
372382
let
373383
(_, bodyrows, mtotalsrows) =
374-
multiBalanceReportAsSpreadsheetParts fmt ropts mbr
375-
376-
in
377-
Spr.horizontalSpan headerrow
378-
((Spr.defaultCell subreporttitle){
384+
multiBalanceReportAsSpreadsheetParts fmt ropts allCommodities mbr
385+
accountCell =
386+
(Spr.defaultCell subreporttitle) {
379387
Spr.cellStyle = Spr.Body Spr.Total,
380388
Spr.cellClass = Spr.Class "account"
381-
}) :
389+
}
390+
391+
in
392+
(case layout_ ropts of
393+
LayoutBareWide ->
394+
accountCell : map Spr.headerCell (dataHeaders >> allCommodities)
395+
_ -> Spr.horizontalSpan headerrow accountCell) :
382396
bodyrows ++
383397
mtotalsrows ++
384398
maybeToList blankrow ++
@@ -387,7 +401,7 @@ compoundBalanceReportAsSpreadsheet fmt accountLabel maybeBlank ropts cbr =
387401
totalrows =
388402
if no_total_ ropts || length subreports == 1 then []
389403
else
390-
multiBalanceRowAsCellBuilders fmt ropts colspans
404+
multiBalanceRowAsCellBuilders fmt ropts colspans allCommodities
391405
Total simpleDateSpanCell totalrow
392406
-- make a table of rendered lines of the report totals row
393407
& map (map (fmap wbToText))
@@ -399,3 +413,10 @@ compoundBalanceReportAsSpreadsheet fmt accountLabel maybeBlank ropts cbr =
399413
in (title,
400414
((Just 1, Just 1),
401415
headerrow :| concatMap subreportrows subreports ++ totalrows))
416+
417+
allCommoditiesFromSubreports ::
418+
[(text, PeriodicReport a MixedAmount, bool)] -> [CommoditySymbol]
419+
allCommoditiesFromSubreports =
420+
Set.toAscList .
421+
foldMap (\(_,mbr,_) ->
422+
foldMap (foldMap maCommodities . prrAmounts) $ prRows mbr)

0 commit comments

Comments
 (0)