lib: Rename alignCell to textCell, minor cleanups.

This commit is contained in:
Stephen Morgan 2021-01-13 16:31:13 +11:00 committed by Simon Michael
parent 9ad0eef69b
commit 217bfc5e74
6 changed files with 18 additions and 22 deletions

View File

@ -246,11 +246,11 @@ postingAsLines elideamount onelineamounts pstoalignwith p =
-- could be removed and we could just keep everything as a Text Builder, but -- could be removed and we could just keep everything as a Text Builder, but
-- would require adding trailing spaces to 42 failing tests. -- would require adding trailing spaces to 42 failing tests.
postingblocks = [map T.stripEnd . T.lines . TL.toStrict $ postingblocks = [map T.stripEnd . T.lines . TL.toStrict $
render [ alignCell BottomLeft statusandaccount render [ textCell BottomLeft statusandaccount
, alignCell BottomLeft " " , textCell BottomLeft " "
, Cell BottomLeft [amt] , Cell BottomLeft [amt]
, Cell BottomLeft [assertion] , Cell BottomLeft [assertion]
, alignCell BottomLeft samelinecomment , textCell BottomLeft samelinecomment
] ]
| amt <- shownAmounts] | amt <- shownAmounts]
render = renderRow def{tableBorders=False, borderSpaces=False} . Group NoLine . map Header render = renderRow def{tableBorders=False, borderSpaces=False} . Group NoLine . map Header

View File

@ -223,7 +223,7 @@ budgetReportAsText :: ReportOpts -> BudgetReport -> TL.Text
budgetReportAsText ropts@ReportOpts{..} budgetr = TB.toLazyText $ budgetReportAsText ropts@ReportOpts{..} budgetr = TB.toLazyText $
TB.fromText title <> TB.fromText "\n\n" <> TB.fromText title <> TB.fromText "\n\n" <>
renderTableB def{tableBorders=False,prettyTable=pretty_tables_} renderTableB def{tableBorders=False,prettyTable=pretty_tables_}
(alignCell TopLeft) (alignCell TopRight) (uncurry showcell) displayTableWithWidths (textCell TopLeft) (textCell TopRight) (uncurry showcell) displayTableWithWidths
where where
title = "Budget performance in " <> showDateSpan (periodicReportSpan budgetr) title = "Budget performance in " <> showDateSpan (periodicReportSpan budgetr)
<> (case value_ of <> (case value_ of

View File

@ -62,7 +62,7 @@ import Text.Printf (printf)
import Hledger.Utils.Parse import Hledger.Utils.Parse
import Hledger.Utils.Regex (toRegex', regexReplace) import Hledger.Utils.Regex (toRegex', regexReplace)
import Text.Tabular (Header(..), Properties(..)) 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) import Text.WideString (charWidth, strWidth)
@ -185,16 +185,13 @@ unbracket s
-- Treats wide characters as double width. -- Treats wide characters as double width.
concatTopPadded :: [String] -> String concatTopPadded :: [String] -> String
concatTopPadded = TL.unpack . renderRow def{tableBorders=False, borderSpaces=False} concatTopPadded = TL.unpack . renderRow def{tableBorders=False, borderSpaces=False}
. Group NoLine . map (Header . cell) . Group NoLine . map (Header . textCell BottomLeft . T.pack)
where cell = alignCell BottomLeft . T.pack
-- | Join several multi-line strings as side-by-side rectangular strings of the same height, bottom-padded. -- | Join several multi-line strings as side-by-side rectangular strings of the same height, bottom-padded.
-- Treats wide characters as double width. -- Treats wide characters as double width.
concatBottomPadded :: [String] -> String concatBottomPadded :: [String] -> String
concatBottomPadded = TL.unpack . renderRow def{tableBorders=False, borderSpaces=False} concatBottomPadded = TL.unpack . renderRow def{tableBorders=False, borderSpaces=False}
. Group NoLine . map (Header . cell) . Group NoLine . map (Header . textCell TopLeft . T.pack)
where cell = alignCell TopLeft . T.pack
-- | Join multi-line strings horizontally, after compressing each of -- | Join multi-line strings horizontally, after compressing each of
-- them to a single line with a comma and space between each original line. -- them to a single line with a comma and space between each original line.

View File

@ -66,15 +66,18 @@ module Hledger.Utils.Text
where where
import Data.Char (digitToInt) import Data.Char (digitToInt)
import Data.List (transpose) import Data.Default (def)
#if !(MIN_VERSION_base(4,11,0)) #if !(MIN_VERSION_base(4,11,0))
import Data.Semigroup ((<>)) import Data.Semigroup ((<>))
#endif #endif
import Data.Text (Text) import Data.Text (Text)
import qualified Data.Text as T import qualified Data.Text as T
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Builder as TB import qualified Data.Text.Lazy.Builder as TB
import Hledger.Utils.Test ((@?=), test, tests) 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) import Text.WideString (WideBuilder(..), wbToText, wbUnpack, charWidth, textWidth)
@ -196,8 +199,7 @@ textUnbracket s
-- Treats wide characters as double width. -- Treats wide characters as double width.
textConcatTopPadded :: [Text] -> Text textConcatTopPadded :: [Text] -> Text
textConcatTopPadded = TL.toStrict . renderRow def{tableBorders=False, borderSpaces=False} textConcatTopPadded = TL.toStrict . renderRow def{tableBorders=False, borderSpaces=False}
. Group NoLine . map (Header . cell) . Group NoLine . map (Header . textCell BottomLeft)
where cell = alignCell BottomLeft
-- -- | Join several multi-line strings as side-by-side rectangular strings of the same height, bottom-padded. -- -- | Join several multi-line strings as side-by-side rectangular strings of the same height, bottom-padded.
-- -- Treats wide characters as double width. -- -- Treats wide characters as double width.
@ -253,9 +255,6 @@ textConcatTopPadded = TL.toStrict . renderRow def{tableBorders=False, borderSpac
-- ypadded = ls ++ replicate (difforzero h sh) "" -- ypadded = ls ++ replicate (difforzero h sh) ""
-- xpadded = map (padleft sw) ypadded -- 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. -- -- | Convert a multi-line string to a rectangular string left-padded to the specified width.
-- -- Treats wide characters as double width. -- -- Treats wide characters as double width.
-- padleft :: Int -> String -> String -- padleft :: Int -> String -> String

View File

@ -46,8 +46,8 @@ emptyCell :: Cell
emptyCell = Cell TopRight [] emptyCell = Cell TopRight []
-- | Create a single-line cell from the given contents with its natural width. -- | Create a single-line cell from the given contents with its natural width.
alignCell :: Align -> Text -> Cell textCell :: 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 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. -- | Return the width of a Cell.
cellWidth :: Cell -> Int 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 a table according to common options, for backwards compatibility
render :: Bool -> (rh -> Text) -> (ch -> Text) -> (a -> Text) -> Table rh ch a -> TL.Text 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) 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> -- | Render a table according to various cell specifications>
renderTable :: TableOpts -- ^ Options controlling Table rendering renderTable :: TableOpts -- ^ Options controlling Table rendering

View File

@ -425,11 +425,11 @@ renderBalanceReportItem opts (acctname, depth, total) =
-- | Render one StringFormat component for a balance report item. -- | Render one StringFormat component for a balance report item.
renderComponent :: Bool -> ReportOpts -> (AccountName, Int, MixedAmount) -> StringFormatComponent -> Cell 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 renderComponent topaligned opts (acctname, depth, total) (FormatField ljust mmin mmax field) = case field of
DepthSpacerField -> Cell align [WideBuilder (TB.fromText $ T.replicate d " ") d] DepthSpacerField -> Cell align [WideBuilder (TB.fromText $ T.replicate d " ") d]
where d = maybe id min mmax $ depth * fromMaybe 1 mmin 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 TotalField -> Cell align . pure $ showamt total
_ -> Cell align [mempty] _ -> Cell align [mempty]
where where
@ -622,7 +622,7 @@ balanceReportAsTable opts@ReportOpts{average_, row_total_, balancetype_}
balanceReportTableAsText :: ReportOpts -> Table T.Text T.Text MixedAmount -> TB.Builder balanceReportTableAsText :: ReportOpts -> Table T.Text T.Text MixedAmount -> TB.Builder
balanceReportTableAsText ReportOpts{..} = balanceReportTableAsText ReportOpts{..} =
Tab.renderTableB def{tableBorders=False, prettyTable=pretty_tables_} Tab.renderTableB def{tableBorders=False, prettyTable=pretty_tables_}
(Tab.alignCell TopLeft) (Tab.alignCell TopRight) showamt (Tab.textCell TopLeft) (Tab.textCell TopRight) showamt
where where
showamt = Cell TopRight . pure . showMixedAmountB oneLine{displayColour=color_, displayMaxWidth=mmax} showamt = Cell TopRight . pure . showMixedAmountB oneLine{displayColour=color_, displayMaxWidth=mmax}
mmax = if no_elide_ then Nothing else Just 32 mmax = if no_elide_ then Nothing else Just 32