Skip to content

Commit

Permalink
lib: Make sure to add a newline to the end of aregister report.
Browse files Browse the repository at this point in the history
  • Loading branch information
Xitian9 authored and simonmichael committed Jan 11, 2021
1 parent b5ab5ac commit b203822
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 8 deletions.
3 changes: 1 addition & 2 deletions hledger-lib/Hledger/Read/CsvReader.hs
Original file line number Diff line number Diff line change
Expand Up @@ -812,10 +812,9 @@ parseResultToCsv = toListList . unpackFields
unpackFields = (fmap . fmap) T.decodeUtf8

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
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.
validateCsv :: CsvRules -> Int -> Either String CSV -> Either String [CsvRecord]
Expand Down
7 changes: 7 additions & 0 deletions hledger-lib/Hledger/Utils/Text.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ module Hledger.Utils.Text
fitText,
linesPrepend,
linesPrepend2,
unlinesB,
-- -- * wide-character-aware layout
WideBuilder(..),
wbToText,
Expand All @@ -71,6 +72,7 @@ import Data.Monoid
#endif
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.Lazy.Builder as TB
-- import Text.Parsec
-- import Text.Printf (printf)

Expand Down Expand Up @@ -371,6 +373,11 @@ linesPrepend2 prefix1 prefix2 s = T.unlines $ case T.lines s of
[] -> []
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
-- characters.
Expand Down
2 changes: 1 addition & 1 deletion hledger/Hledger/Cli/Commands/Aregister.hs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ accountTransactionsReportItemAsCsvRecord
-- | Render a register report as plain text suitable for console output.
accountTransactionsReportAsText :: CliOpts -> Query -> Query -> AccountTransactionsReport -> TL.Text
accountTransactionsReportAsText copts reportq thisacctq items
= TB.toLazyText . mconcat . intersperse (TB.fromText "\n") $
= TB.toLazyText . unlinesB $
title :
map (accountTransactionsReportItemAsText copts reportq thisacctq amtwidth balwidth) items
where
Expand Down
3 changes: 0 additions & 3 deletions hledger/Hledger/Cli/Commands/Balance.hs
Original file line number Diff line number Diff line change
Expand Up @@ -369,9 +369,6 @@ balanceReportAsText opts ((items, total)) =
unlinesB lines
<> unlinesB (if no_total_ opts then [] else [overline, totalLines])
where
unlinesB [] = mempty
unlinesB xs = mconcat (intersperse (TB.singleton '\n') xs) <> TB.singleton '\n'

(lines, sizes) = unzip $ map (balanceReportItemAsText opts) items
-- abuse renderBalanceReportItem to render the total with similar format
(totalLines, _) = renderBalanceReportItem opts ("",0,total)
Expand Down
2 changes: 0 additions & 2 deletions hledger/Hledger/Cli/Commands/Register.hs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ postingsReportAsText opts items =
balwidth = maximumStrict $ map (wbWidth . showAmt . itembal) items
itemamt (_,_,_,Posting{pamount=a},_) = a
itembal (_,_,_,_,a) = a
unlinesB [] = mempty
unlinesB xs = mconcat (intersperse (TB.fromText "\n") xs) <> TB.fromText "\n"
showAmt = showMixedAmountB noColour{displayMinWidth=Just 12}

-- | Render one register report line item as plain text. Layout is like so:
Expand Down

0 comments on commit b203822

Please sign in to comment.