lib: Refactor unlinesB to be cleaner and more efficient.

This commit is contained in:
Stephen Morgan 2021-01-12 13:52:59 +11:00 committed by Simon Michael
parent d3a1caa6c1
commit 2c2bd8b27e
2 changed files with 6 additions and 12 deletions

View File

@ -66,19 +66,15 @@ module Hledger.Utils.Text
where
import Data.Char (digitToInt)
import Data.List
import Data.List (transpose)
#if !(MIN_VERSION_base(4,11,0))
import Data.Monoid
import Data.Semigroup ((<>))
#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)
-- import Hledger.Utils.Parse
-- import Hledger.Utils.Regex
import Hledger.Utils.Test
import Hledger.Utils.Test ((@?=), test, tests)
import Text.WideString (WideBuilder(..), wbToText, wbUnpack, charWidth, textWidth)
@ -373,11 +369,9 @@ 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.
-- | Join a list of Text Builders with a newline after each item.
unlinesB :: [TB.Builder] -> TB.Builder
unlinesB [] = mempty
unlinesB xs = mconcat (intersperse (TB.singleton '\n') xs) <> TB.singleton '\n'
unlinesB = foldMap (<> TB.singleton '\n')
-- | Read a decimal number from a Text. Assumes the input consists only of digit
-- characters.

View File

@ -103,7 +103,7 @@ renderTableB topts@TableOpts{prettyTable=pretty, tableBorders=borders} fr fc f (
-- borders and bars
addBorders xs = if borders then bar VT SingleLine : xs ++ [bar VB SingleLine] else xs
bar vpos prop = mconcat $ renderHLine vpos borders pretty sizes ch2 prop
unlinesB = (<>singleton '\n') . mconcat . intersperse "\n"
unlinesB = foldMap (<> singleton '\n')
-- | Render a single row according to cell specifications.
renderRow :: TableOpts -> Header Cell -> TL.Text