cli: Commands.Register.postingsReportAsSpreadsheet: generalize CSV output to HTML and FODS

This commit is contained in:
Henning Thielemann 2024-10-16 12:52:49 +02:00 committed by Simon Michael
parent 121b648bdb
commit 991c56caef
3 changed files with 38 additions and 15 deletions

View File

@ -19,6 +19,7 @@ module Hledger.Write.Spreadsheet (
transposeCell, transposeCell,
transpose, transpose,
horizontalSpan, horizontalSpan,
addHeaderBorders,
addRowSpanHeader, addRowSpanHeader,
rawTableContent, rawTableContent,
) where ) where
@ -172,6 +173,10 @@ transpose :: [[Cell border text]] -> [[Cell border text]]
transpose = List.transpose . map (map transposeCell) transpose = List.transpose . map (map transposeCell)
addHeaderBorders :: [Cell () text] -> [Cell NumLines text]
addHeaderBorders =
map (\c -> c {cellBorder = noBorder {borderBottom = DoubleLine}})
horizontalSpan :: horizontalSpan ::
(Lines border, Monoid text) => (Lines border, Monoid text) =>
[a] -> Cell border text -> [Cell border text] [a] -> Cell border text -> [Cell border text]

View File

@ -308,7 +308,7 @@ import Hledger.Cli.Utils
import Hledger.Write.Csv (CSV, printCSV, printTSV) import Hledger.Write.Csv (CSV, printCSV, printTSV)
import Hledger.Write.Ods (printFods) import Hledger.Write.Ods (printFods)
import Hledger.Write.Html.Lucid (printHtml) import Hledger.Write.Html.Lucid (printHtml)
import Hledger.Write.Spreadsheet (rawTableContent, addRowSpanHeader, headerCell) import Hledger.Write.Spreadsheet (rawTableContent, addHeaderBorders, addRowSpanHeader, headerCell)
import qualified Hledger.Write.Spreadsheet as Ods import qualified Hledger.Write.Spreadsheet as Ods
@ -641,11 +641,6 @@ headerDateSpanCell base query spn =
Ods.cellAnchor = composeAnchor base $ replaceDate prd query Ods.cellAnchor = composeAnchor base $ replaceDate prd query
} }
addHeaderBorders :: [Ods.Cell () text] -> [Ods.Cell Ods.NumLines text]
addHeaderBorders =
map (\c -> c {Ods.cellBorder =
Ods.noBorder {Ods.borderBottom = Ods.DoubleLine}})
simpleDateSpanCell :: DateSpan -> Ods.Cell Ods.NumLines Text simpleDateSpanCell :: DateSpan -> Ods.Cell Ods.NumLines Text
simpleDateSpanCell = Ods.defaultCell . showDateSpan simpleDateSpanCell = Ods.defaultCell . showDateSpan

View File

@ -20,6 +20,7 @@ module Hledger.Cli.Commands.Register (
import Data.Default (def) import Data.Default (def)
import Data.Maybe (fromMaybe, isJust) import Data.Maybe (fromMaybe, isJust)
import qualified Data.Map as Map
import qualified Data.Text as T import qualified Data.Text as T
import qualified Data.Text.Lazy as TL import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.IO as TL import qualified Data.Text.Lazy.IO as TL
@ -27,14 +28,19 @@ import qualified Data.Text.Lazy.Builder as TB
import System.Console.CmdArgs.Explicit (flagNone, flagReq) import System.Console.CmdArgs.Explicit (flagNone, flagReq)
import Hledger hiding (per) import Hledger hiding (per)
import Hledger.Write.Csv (CSV, CsvRecord, printCSV, printTSV) import Hledger.Write.Csv (CSV, printCSV, printTSV)
import Hledger.Write.Ods (printFods)
import Hledger.Write.Html.Lucid (printHtml)
import qualified Hledger.Write.Spreadsheet as Spr
import Hledger.Cli.CliOptions import Hledger.Cli.CliOptions
import Hledger.Cli.Utils import Hledger.Cli.Utils
import Text.Tabular.AsciiWide hiding (render) import Text.Tabular.AsciiWide (Cell(..), Align(..), Properties(..), Header(Header, Group), renderRowB, textCell, tableBorders, borderSpaces)
import qualified Lucid
import Data.List (sortBy) import Data.List (sortBy)
import Data.Char (toUpper) import Data.Char (toUpper)
import Data.List.Extra (intersect) import Data.List.Extra (intersect)
import System.Exit (exitFailure) import System.Exit (exitFailure)
import qualified System.IO as IO
registermode = hledgerCommandMode registermode = hledgerCommandMode
$(embedFileRelative "Hledger/Cli/Commands/Register.txt") $(embedFileRelative "Hledger/Cli/Commands/Register.txt")
@ -90,21 +96,38 @@ register opts@CliOpts{rawopts_=rawopts, reportspec_=rspec} j
styles = journalCommodityStylesWith HardRounding j styles = journalCommodityStylesWith HardRounding j
rpt = postingsReport rspec j rpt = postingsReport rspec j
render | fmt=="txt" = postingsReportAsText opts render | fmt=="txt" = postingsReportAsText opts
| fmt=="json" = toJsonText
| fmt=="csv" = printCSV . postingsReportAsCsv | fmt=="csv" = printCSV . postingsReportAsCsv
| fmt=="tsv" = printTSV . postingsReportAsCsv | fmt=="tsv" = printTSV . postingsReportAsCsv
| fmt=="json" = toJsonText | fmt=="html" =
(<>"\n") . Lucid.renderText . printHtml .
map (map (fmap Lucid.toHtml)) . postingsReportAsSpreadsheet
| fmt=="fods" =
printFods IO.localeEncoding . Map.singleton "Register" .
(,) (Just 1, Nothing) . postingsReportAsSpreadsheet
| otherwise = error' $ unsupportedOutputFormatError fmt -- PARTIAL: | otherwise = error' $ unsupportedOutputFormatError fmt -- PARTIAL:
where fmt = outputFormatFromOpts opts where fmt = outputFormatFromOpts opts
postingsReportAsCsv :: PostingsReport -> CSV postingsReportAsCsv :: PostingsReport -> CSV
postingsReportAsCsv is = postingsReportAsCsv = Spr.rawTableContent . postingsReportAsSpreadsheet
["txnidx","date","code","description","account","amount","total"]
:
map postingsReportItemAsCsvRecord is
postingsReportItemAsCsvRecord :: PostingsReportItem -> CsvRecord postingsReportAsSpreadsheet ::
postingsReportItemAsCsvRecord (_, _, _, p, b) = [idx,date,code,desc,acct,amt,bal] PostingsReport -> [[Spr.Cell Spr.NumLines T.Text]]
postingsReportAsSpreadsheet is =
Spr.addHeaderBorders
(map Spr.headerCell
["txnidx","date","code","description","account","amount","total"])
:
map postingsReportItemAsRecord is
postingsReportItemAsRecord ::
(Spr.Lines border) => PostingsReportItem -> [Spr.Cell border T.Text]
postingsReportItemAsRecord (_, _, _, p, b) =
[cell idx,
(cell date) {Spr.cellType = Spr.TypeDate},
cell code, cell desc, cell acct, cell amt, cell bal]
where where
cell = Spr.defaultCell
idx = T.pack . show . maybe 0 tindex $ ptransaction p idx = T.pack . show . maybe 0 tindex $ ptransaction p
date = showDate $ postingDate p -- XXX csv should show date2 with --date2 date = showDate $ postingDate p -- XXX csv should show date2 with --date2
code = maybe "" tcode $ ptransaction p code = maybe "" tcode $ ptransaction p