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
-- 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

View File

@ -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

View File

@ -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.

View File

@ -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

View File

@ -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

View File

@ -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