diff --git a/hledger-lib/Hledger/Read/CsvReader.hs b/hledger-lib/Hledger/Read/CsvReader.hs index 258c52eaa..6623e001a 100644 --- a/hledger-lib/Hledger/Read/CsvReader.hs +++ b/hledger-lib/Hledger/Read/CsvReader.hs @@ -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] diff --git a/hledger-lib/Hledger/Utils/Text.hs b/hledger-lib/Hledger/Utils/Text.hs index 6a4950d73..d81b932d4 100644 --- a/hledger-lib/Hledger/Utils/Text.hs +++ b/hledger-lib/Hledger/Utils/Text.hs @@ -47,6 +47,7 @@ module Hledger.Utils.Text fitText, linesPrepend, linesPrepend2, + unlinesB, -- -- * wide-character-aware layout WideBuilder(..), wbToText, @@ -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) @@ -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. diff --git a/hledger/Hledger/Cli/Commands/Aregister.hs b/hledger/Hledger/Cli/Commands/Aregister.hs index 693ff6170..0a880a9c6 100644 --- a/hledger/Hledger/Cli/Commands/Aregister.hs +++ b/hledger/Hledger/Cli/Commands/Aregister.hs @@ -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 diff --git a/hledger/Hledger/Cli/Commands/Balance.hs b/hledger/Hledger/Cli/Commands/Balance.hs index 11f86cf04..2b41ac63b 100644 --- a/hledger/Hledger/Cli/Commands/Balance.hs +++ b/hledger/Hledger/Cli/Commands/Balance.hs @@ -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) diff --git a/hledger/Hledger/Cli/Commands/Register.hs b/hledger/Hledger/Cli/Commands/Register.hs index bb4132992..c9332ebfb 100644 --- a/hledger/Hledger/Cli/Commands/Register.hs +++ b/hledger/Hledger/Cli/Commands/Register.hs @@ -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: