Skip to content

Commit

Permalink
cln!: tabular: Remove unneeded tabular modules.
Browse files Browse the repository at this point in the history
Text.WideString and Text.Tabular.AsciiWide modules are now redundant and
can be removed. A local definition of Table and concatTables has been
moved to Hledger.Utils.Text.
  • Loading branch information
Xitian9 committed May 2, 2022
1 parent 4b13c36 commit 0cfad33
Show file tree
Hide file tree
Showing 13 changed files with 84 additions and 426 deletions.
1 change: 0 additions & 1 deletion hledger-lib/Hledger/Data/Account.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import Data.Ord (Down(..))
import Safe (headMay)
import Text.Printf (printf)

import Hledger.Utils (buildCell)
import Hledger.Data.AccountName (expandAccountName, clipOrEllipsifyAccountName)
import Hledger.Data.Amount
import Hledger.Data.Types
Expand Down
1 change: 1 addition & 0 deletions hledger-lib/Hledger/Data/Amount.hs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ module Hledger.Data.Amount (
showMixedAmountWithZeroCommodity,
showMixedAmountB,
showMixedAmountLinesB,
buildCell,
mixedAmountSetPrecision,
mixedAmountSetFullPrecision,
canonicaliseMixedAmount,
Expand Down
30 changes: 17 additions & 13 deletions hledger-lib/Hledger/Reports/BudgetReport.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ 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 Text.Layout.Table
--import System.Console.CmdArgs.Explicit as C
--import Lucid as L
import qualified Text.Tabular.AsciiWide as Tab

import Hledger.Data
import Hledger.Utils
Expand Down Expand Up @@ -232,21 +232,25 @@ budgetReportAsText ropts@ReportOpts{..} budgetr = TB.toLazyText $
<> ":"

-- | Build a 'Table' from a multi-column balance report.
budgetReportAsTable :: ReportOpts -> BudgetReport -> Tab.Table Text Text (RenderText)
budgetReportAsTable :: ReportOpts -> BudgetReport -> Table Text Text (RenderText)
budgetReportAsTable
ReportOpts{..}
(PeriodicReport spans items tr) =
maybetransposetable $
addtotalrow $
Tab.Table
(Tab.Group Tab.NoLine $ map Tab.Header accts)
(Tab.Group Tab.NoLine $ map Tab.Header colheadings)
Table
(makeHeader (if transpose_ then right else left) accts)
datesHeader
rows
where
colheadings = ["Commodity" | layout_ == LayoutBare]
++ map (reportPeriodName balanceaccum_ spans) spans
++ [" Total" | row_total_]
++ ["Average" | average_]
datesHeader = case layout_ of
LayoutBare -> groupH NoLine [headerH (headerColumn left Nothing) "Commodity", colheadings]
_ -> colheadings
colheadings = makeHeader (if transpose_ then left else right) $
map (reportPeriodName balanceaccum_ spans) spans
++ [" Total" | row_total_]
++ ["Average" | average_]
makeHeader pos = fullSepH NoLine (repeat $ headerColumn pos Nothing)

-- FIXME. Have to check explicitly for which to render here, since
-- budgetReport sets accountlistmode to ALTree. Find a principled way to do
Expand All @@ -257,16 +261,16 @@ budgetReportAsTable

addtotalrow
| no_total_ = id
| otherwise = let rh = Tab.Group Tab.NoLine . replicate (length totalrows) $ Tab.Header ""
ch = Tab.Header [] -- ignored
in (flip (Tab.concatTables Tab.SingleLine) $ Tab.Table rh ch totalrows)
| otherwise = let rh = fullSepH NoLine (repeat $ headerColumn left Nothing) $ replicate (length totalrows) ""
ch = noneH -- ignored
in (flip (concatTables SingleLine) $ Table rh ch totalrows)

maybetranspose
| transpose_ = transpose
| otherwise = id

maybetransposetable
| transpose_ = \(Tab.Table rh ch vals) -> Tab.Table ch rh (transpose vals)
| transpose_ = \(Table rh ch vals) -> Table ch rh (transpose vals)
| otherwise = id

(accts, rows, totalrows) = (accts, prependcs itemscs (padcells texts), prependcs trcs (padtr trtexts))
Expand Down
14 changes: 4 additions & 10 deletions hledger-lib/Hledger/Reports/MultiBalanceReport.hs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ import Text.Layout.Table

import qualified Data.Text as T
import qualified Data.Text.Lazy.Builder as TB
import qualified Text.Tabular.AsciiWide as Tab

import Hledger.Data
import Hledger.Query
Expand Down Expand Up @@ -590,8 +589,8 @@ cumulativeSum value start = snd . M.mapAccumWithKey accumValued start
-- made using 'balanceReportAsTable'), render it in a format suitable for
-- console output. Amounts with more than two commodities will be elided
-- unless --no-elide is used.
balanceReportTableAsText :: ReportOpts -> Tab.Table T.Text T.Text RenderText -> TB.Builder
balanceReportTableAsText ReportOpts{..} (Tab.Table rh ch cells) =
balanceReportTableAsText :: ReportOpts -> Table T.Text T.Text RenderText -> TB.Builder
balanceReportTableAsText ReportOpts{..} (Table rh ch cells) =
tableStringB colSpec style rowHeader colHeader (map rowG cells) <> TB.singleton '\n'
where
colSpec = case layout_ of
Expand All @@ -600,13 +599,8 @@ balanceReportTableAsText ReportOpts{..} (Tab.Table rh ch cells) =
where
col pos = column expand pos noAlign noCutMark
style = if pretty_ then hledgerPrettyStyle else hledgerStyle
rowHeader = renderText <$> translate left rh
colHeader = renderText <$> translate right ch

translate pos (Tab.Group Tab.NoLine as) = groupH NoLine $ map (translate pos) as
translate pos (Tab.Group Tab.SingleLine as) = groupH SingleLine $ map (translate pos) as
translate pos (Tab.Group Tab.DoubleLine as) = groupH DoubleLine $ map (translate pos) as
translate pos (Tab.Header a) = headerH (headerColumn pos Nothing) a
rowHeader = renderText <$> rh
colHeader = renderText <$> ch

-- tests

Expand Down
24 changes: 20 additions & 4 deletions hledger-lib/Hledger/Utils/Text.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ module Hledger.Utils.Text
module Text.Layout.Table.Cell.WideString,
RenderText,
renderText,
Table(..),
concatTables,
-- * table rendering
hledgerStyle,
hledgerStyleBorders,
Expand Down Expand Up @@ -236,16 +238,30 @@ linesPrepend2 prefix1 prefix2 s = T.unlines $ case T.lines s of
[] -> []
l:ls -> (prefix1<>l) : map (prefix2<>) ls

-- | Join a list of Text Builders with a newline after each item.
unlinesB :: [TB.Builder] -> TB.Builder
unlinesB = foldMap (<> TB.singleton '\n')

-- | Read a decimal number from a Text. Assumes the input consists only of digit
-- characters.
readDecimal :: Text -> Integer
readDecimal = T.foldl' step 0
where step a c = a * 10 + toInteger (digitToInt c)

-- | Join a list of Text Builders with a newline after each item.
unlinesB :: [TB.Builder] -> TB.Builder
unlinesB = foldMap (<> TB.singleton '\n')


-- Tables and rendering

-- | A Table contains information about the row and column headers, as well as a table of data.
data Table rh ch a = Table (HeaderSpec LineStyle rh) (HeaderSpec LineStyle ch) [[a]]

-- | Add the second table below the first, discarding its column headings.
concatTables :: Monoid a => LineStyle -> Table rh ch a -> Table rh ch2 a -> Table rh ch a
concatTables prop (Table hLeft hTop dat) (Table hLeft' _ dat') =
Table (groupH prop [hLeft, hLeft']) hTop (map padRow $ dat ++ dat')
where
numCols = length $ headerContents hTop
padRow r = replicate (numCols - length r) mempty ++ r

-- | An alias for formatted text measured by display length.
type RenderText = Formatted WideText

Expand Down
Loading

0 comments on commit 0cfad33

Please sign in to comment.