From 217bfc5e7425e8e452f363a072004e9458025d1d Mon Sep 17 00:00:00 2001 From: Stephen Morgan Date: Wed, 13 Jan 2021 16:31:13 +1100 Subject: [PATCH] lib: Rename alignCell to textCell, minor cleanups. --- hledger-lib/Hledger/Data/Transaction.hs | 6 +++--- hledger-lib/Hledger/Reports/BudgetReport.hs | 2 +- hledger-lib/Hledger/Utils/String.hs | 9 +++------ hledger-lib/Hledger/Utils/Text.hs | 11 +++++------ hledger-lib/Text/Tabular/AsciiWide.hs | 6 +++--- hledger/Hledger/Cli/Commands/Balance.hs | 6 +++--- 6 files changed, 18 insertions(+), 22 deletions(-) diff --git a/hledger-lib/Hledger/Data/Transaction.hs b/hledger-lib/Hledger/Data/Transaction.hs index 5fc467fb7..95ab27cc4 100644 --- a/hledger-lib/Hledger/Data/Transaction.hs +++ b/hledger-lib/Hledger/Data/Transaction.hs @@ -246,11 +246,11 @@ postingAsLines elideamount onelineamounts pstoalignwith p = -- could be removed and we could just keep everything as a Text Builder, but -- would require adding trailing spaces to 42 failing tests. postingblocks = [map T.stripEnd . T.lines . TL.toStrict $ - render [ alignCell BottomLeft statusandaccount - , alignCell BottomLeft " " + render [ textCell BottomLeft statusandaccount + , textCell BottomLeft " " , Cell BottomLeft [amt] , Cell BottomLeft [assertion] - , alignCell BottomLeft samelinecomment + , textCell BottomLeft samelinecomment ] | amt <- shownAmounts] render = renderRow def{tableBorders=False, borderSpaces=False} . Group NoLine . map Header diff --git a/hledger-lib/Hledger/Reports/BudgetReport.hs b/hledger-lib/Hledger/Reports/BudgetReport.hs index 4a8b48a1a..fd2e98738 100644 --- a/hledger-lib/Hledger/Reports/BudgetReport.hs +++ b/hledger-lib/Hledger/Reports/BudgetReport.hs @@ -223,7 +223,7 @@ budgetReportAsText :: ReportOpts -> BudgetReport -> TL.Text budgetReportAsText ropts@ReportOpts{..} budgetr = TB.toLazyText $ TB.fromText title <> TB.fromText "\n\n" <> renderTableB def{tableBorders=False,prettyTable=pretty_tables_} - (alignCell TopLeft) (alignCell TopRight) (uncurry showcell) displayTableWithWidths + (textCell TopLeft) (textCell TopRight) (uncurry showcell) displayTableWithWidths where title = "Budget performance in " <> showDateSpan (periodicReportSpan budgetr) <> (case value_ of diff --git a/hledger-lib/Hledger/Utils/String.hs b/hledger-lib/Hledger/Utils/String.hs index 4f0b79301..8a4800648 100644 --- a/hledger-lib/Hledger/Utils/String.hs +++ b/hledger-lib/Hledger/Utils/String.hs @@ -62,7 +62,7 @@ import Text.Printf (printf) import Hledger.Utils.Parse import Hledger.Utils.Regex (toRegex', regexReplace) import Text.Tabular (Header(..), Properties(..)) -import Text.Tabular.AsciiWide (Align(..), TableOpts(..), alignCell, renderRow) +import Text.Tabular.AsciiWide (Align(..), TableOpts(..), textCell, renderRow) import Text.WideString (charWidth, strWidth) @@ -185,16 +185,13 @@ unbracket s -- Treats wide characters as double width. concatTopPadded :: [String] -> String concatTopPadded = TL.unpack . renderRow def{tableBorders=False, borderSpaces=False} - . Group NoLine . map (Header . cell) - where cell = alignCell BottomLeft . T.pack + . Group NoLine . map (Header . textCell BottomLeft . T.pack) -- | Join several multi-line strings as side-by-side rectangular strings of the same height, bottom-padded. -- Treats wide characters as double width. concatBottomPadded :: [String] -> String concatBottomPadded = TL.unpack . renderRow def{tableBorders=False, borderSpaces=False} - . Group NoLine . map (Header . cell) - where cell = alignCell TopLeft . T.pack - + . Group NoLine . map (Header . textCell TopLeft . T.pack) -- | Join multi-line strings horizontally, after compressing each of -- them to a single line with a comma and space between each original line. diff --git a/hledger-lib/Hledger/Utils/Text.hs b/hledger-lib/Hledger/Utils/Text.hs index 9b6049712..db3440777 100644 --- a/hledger-lib/Hledger/Utils/Text.hs +++ b/hledger-lib/Hledger/Utils/Text.hs @@ -66,15 +66,18 @@ module Hledger.Utils.Text where import Data.Char (digitToInt) -import Data.List (transpose) +import Data.Default (def) #if !(MIN_VERSION_base(4,11,0)) import Data.Semigroup ((<>)) #endif import Data.Text (Text) import qualified Data.Text as T +import qualified Data.Text.Lazy as TL import qualified Data.Text.Lazy.Builder as TB import Hledger.Utils.Test ((@?=), test, tests) +import Text.Tabular (Header(..), Properties(..)) +import Text.Tabular.AsciiWide (Align(..), TableOpts(..), textCell, renderRow) import Text.WideString (WideBuilder(..), wbToText, wbUnpack, charWidth, textWidth) @@ -196,8 +199,7 @@ textUnbracket s -- Treats wide characters as double width. textConcatTopPadded :: [Text] -> Text textConcatTopPadded = TL.toStrict . renderRow def{tableBorders=False, borderSpaces=False} - . Group NoLine . map (Header . cell) - where cell = alignCell BottomLeft + . Group NoLine . map (Header . textCell BottomLeft) -- -- | Join several multi-line strings as side-by-side rectangular strings of the same height, bottom-padded. -- -- Treats wide characters as double width. @@ -253,9 +255,6 @@ textConcatTopPadded = TL.toStrict . renderRow def{tableBorders=False, borderSpac -- ypadded = ls ++ replicate (difforzero h sh) "" -- xpadded = map (padleft sw) ypadded -difforzero :: (Num a, Ord a) => a -> a -> a -difforzero a b = maximum [(a - b), 0] - -- -- | Convert a multi-line string to a rectangular string left-padded to the specified width. -- -- Treats wide characters as double width. -- padleft :: Int -> String -> String diff --git a/hledger-lib/Text/Tabular/AsciiWide.hs b/hledger-lib/Text/Tabular/AsciiWide.hs index 0d71ac008..bb0974891 100644 --- a/hledger-lib/Text/Tabular/AsciiWide.hs +++ b/hledger-lib/Text/Tabular/AsciiWide.hs @@ -46,8 +46,8 @@ emptyCell :: Cell emptyCell = Cell TopRight [] -- | Create a single-line cell from the given contents with its natural width. -alignCell :: Align -> Text -> Cell -alignCell a x = Cell a . map (\x -> WideBuilder (fromText x) (textWidth x)) $ if T.null x then [""] else T.lines x +textCell :: Align -> Text -> Cell +textCell a x = Cell a . map (\x -> WideBuilder (fromText x) (textWidth x)) $ if T.null x then [""] else T.lines x -- | Return the width of a Cell. cellWidth :: Cell -> Int @@ -57,7 +57,7 @@ cellWidth (Cell _ xs) = fromMaybe 0 . maximumMay $ map wbWidth xs -- | Render a table according to common options, for backwards compatibility render :: Bool -> (rh -> Text) -> (ch -> Text) -> (a -> Text) -> Table rh ch a -> TL.Text render pretty fr fc f = renderTable def{prettyTable=pretty} (cell . fr) (cell . fc) (cell . f) - where cell = alignCell TopRight + where cell = textCell TopRight -- | Render a table according to various cell specifications> renderTable :: TableOpts -- ^ Options controlling Table rendering diff --git a/hledger/Hledger/Cli/Commands/Balance.hs b/hledger/Hledger/Cli/Commands/Balance.hs index bcfe7f161..c8154409d 100644 --- a/hledger/Hledger/Cli/Commands/Balance.hs +++ b/hledger/Hledger/Cli/Commands/Balance.hs @@ -425,11 +425,11 @@ renderBalanceReportItem opts (acctname, depth, total) = -- | Render one StringFormat component for a balance report item. renderComponent :: Bool -> ReportOpts -> (AccountName, Int, MixedAmount) -> StringFormatComponent -> Cell -renderComponent _ _ _ (FormatLiteral s) = alignCell TopLeft s +renderComponent _ _ _ (FormatLiteral s) = textCell TopLeft s renderComponent topaligned opts (acctname, depth, total) (FormatField ljust mmin mmax field) = case field of DepthSpacerField -> Cell align [WideBuilder (TB.fromText $ T.replicate d " ") d] where d = maybe id min mmax $ depth * fromMaybe 1 mmin - AccountField -> alignCell align $ formatText ljust mmin mmax acctname + AccountField -> textCell align $ formatText ljust mmin mmax acctname TotalField -> Cell align . pure $ showamt total _ -> Cell align [mempty] where @@ -622,7 +622,7 @@ balanceReportAsTable opts@ReportOpts{average_, row_total_, balancetype_} balanceReportTableAsText :: ReportOpts -> Table T.Text T.Text MixedAmount -> TB.Builder balanceReportTableAsText ReportOpts{..} = Tab.renderTableB def{tableBorders=False, prettyTable=pretty_tables_} - (Tab.alignCell TopLeft) (Tab.alignCell TopRight) showamt + (Tab.textCell TopLeft) (Tab.textCell TopRight) showamt where showamt = Cell TopRight . pure . showMixedAmountB oneLine{displayColour=color_, displayMaxWidth=mmax} mmax = if no_elide_ then Nothing else Just 32