lib: Make sure to add a newline to the end of aregister report.

This commit is contained in:
Stephen Morgan 2021-01-11 15:35:38 +11:00 committed by Simon Michael
parent b5ab5ac9bc
commit b203822cd1
5 changed files with 9 additions and 8 deletions

View File

@ -812,10 +812,9 @@ parseResultToCsv = toListList . unpackFields
unpackFields = (fmap . fmap) T.decodeUtf8 unpackFields = (fmap . fmap) T.decodeUtf8
printCSV :: CSV -> TL.Text printCSV :: CSV -> TL.Text
printCSV = TB.toLazyText . unlined . map printRecord printCSV = TB.toLazyText . unlinesB . map printRecord
where printRecord = mconcat . map TB.fromText . intersperse "," . map printField where printRecord = mconcat . map TB.fromText . intersperse "," . map printField
printField = wrap "\"" "\"" . T.replace "\"" "\\\"\\\"" printField = wrap "\"" "\"" . T.replace "\"" "\\\"\\\""
unlined = (<> TB.fromText "\n") . mconcat . intersperse "\n"
-- | Return the cleaned up and validated CSV data (can be empty), or an error. -- | Return the cleaned up and validated CSV data (can be empty), or an error.
validateCsv :: CsvRules -> Int -> Either String CSV -> Either String [CsvRecord] validateCsv :: CsvRules -> Int -> Either String CSV -> Either String [CsvRecord]

View File

@ -47,6 +47,7 @@ module Hledger.Utils.Text
fitText, fitText,
linesPrepend, linesPrepend,
linesPrepend2, linesPrepend2,
unlinesB,
-- -- * wide-character-aware layout -- -- * wide-character-aware layout
WideBuilder(..), WideBuilder(..),
wbToText, wbToText,
@ -71,6 +72,7 @@ import Data.Monoid
#endif #endif
import Data.Text (Text) import Data.Text (Text)
import qualified Data.Text as T import qualified Data.Text as T
import qualified Data.Text.Lazy.Builder as TB
-- import Text.Parsec -- import Text.Parsec
-- import Text.Printf (printf) -- import Text.Printf (printf)
@ -371,6 +373,11 @@ linesPrepend2 prefix1 prefix2 s = T.unlines $ case T.lines s of
[] -> [] [] -> []
l:ls -> (prefix1<>l) : map (prefix2<>) ls l:ls -> (prefix1<>l) : map (prefix2<>) ls
-- | Concatenate a list of Text Builders with a newline between each item, and
-- another at the end. If the list is empty, return the identity builder.
unlinesB :: [TB.Builder] -> TB.Builder
unlinesB [] = mempty
unlinesB xs = mconcat (intersperse (TB.singleton '\n') xs) <> TB.singleton '\n'
-- | Read a decimal number from a Text. Assumes the input consists only of digit -- | Read a decimal number from a Text. Assumes the input consists only of digit
-- characters. -- characters.

View File

@ -137,7 +137,7 @@ accountTransactionsReportItemAsCsvRecord
-- | Render a register report as plain text suitable for console output. -- | Render a register report as plain text suitable for console output.
accountTransactionsReportAsText :: CliOpts -> Query -> Query -> AccountTransactionsReport -> TL.Text accountTransactionsReportAsText :: CliOpts -> Query -> Query -> AccountTransactionsReport -> TL.Text
accountTransactionsReportAsText copts reportq thisacctq items accountTransactionsReportAsText copts reportq thisacctq items
= TB.toLazyText . mconcat . intersperse (TB.fromText "\n") $ = TB.toLazyText . unlinesB $
title : title :
map (accountTransactionsReportItemAsText copts reportq thisacctq amtwidth balwidth) items map (accountTransactionsReportItemAsText copts reportq thisacctq amtwidth balwidth) items
where where

View File

@ -369,9 +369,6 @@ balanceReportAsText opts ((items, total)) =
unlinesB lines unlinesB lines
<> unlinesB (if no_total_ opts then [] else [overline, totalLines]) <> unlinesB (if no_total_ opts then [] else [overline, totalLines])
where where
unlinesB [] = mempty
unlinesB xs = mconcat (intersperse (TB.singleton '\n') xs) <> TB.singleton '\n'
(lines, sizes) = unzip $ map (balanceReportItemAsText opts) items (lines, sizes) = unzip $ map (balanceReportItemAsText opts) items
-- abuse renderBalanceReportItem to render the total with similar format -- abuse renderBalanceReportItem to render the total with similar format
(totalLines, _) = renderBalanceReportItem opts ("",0,total) (totalLines, _) = renderBalanceReportItem opts ("",0,total)

View File

@ -103,8 +103,6 @@ postingsReportAsText opts items =
balwidth = maximumStrict $ map (wbWidth . showAmt . itembal) items balwidth = maximumStrict $ map (wbWidth . showAmt . itembal) items
itemamt (_,_,_,Posting{pamount=a},_) = a itemamt (_,_,_,Posting{pamount=a},_) = a
itembal (_,_,_,_,a) = a itembal (_,_,_,_,a) = a
unlinesB [] = mempty
unlinesB xs = mconcat (intersperse (TB.fromText "\n") xs) <> TB.fromText "\n"
showAmt = showMixedAmountB noColour{displayMinWidth=Just 12} showAmt = showMixedAmountB noColour{displayMinWidth=Just 12}
-- | Render one register report line item as plain text. Layout is like so: -- | Render one register report line item as plain text. Layout is like so: