From 2c2bd8b27effd4ff011510bf49f02c06d56d3e98 Mon Sep 17 00:00:00 2001 From: Stephen Morgan Date: Tue, 12 Jan 2021 13:52:59 +1100 Subject: [PATCH] lib: Refactor unlinesB to be cleaner and more efficient. --- hledger-lib/Hledger/Utils/Text.hs | 16 +++++----------- hledger-lib/Text/Tabular/AsciiWide.hs | 2 +- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/hledger-lib/Hledger/Utils/Text.hs b/hledger-lib/Hledger/Utils/Text.hs index d81b932d4..4bb97ec35 100644 --- a/hledger-lib/Hledger/Utils/Text.hs +++ b/hledger-lib/Hledger/Utils/Text.hs @@ -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. diff --git a/hledger-lib/Text/Tabular/AsciiWide.hs b/hledger-lib/Text/Tabular/AsciiWide.hs index 18c144c35..0d71ac008 100644 --- a/hledger-lib/Text/Tabular/AsciiWide.hs +++ b/hledger-lib/Text/Tabular/AsciiWide.hs @@ -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