dev: distinguish oneLineFmt and oneLineNoCostFmt; add fullZeroFmt

This commit is contained in:
Simon Michael 2024-01-23 16:20:28 -10:00
parent 999cba8c31
commit df79aa5e35
8 changed files with 31 additions and 21 deletions

View File

@ -79,8 +79,10 @@ module Hledger.Data.Amount (
-- ** rendering
AmountFormat(..),
defaultFmt,
fullZeroFmt,
noCostFmt,
oneLineFmt,
oneLineNoCostFmt,
machineFmt,
showAmount,
showAmountWith,
@ -217,7 +219,7 @@ quoteCommoditySymbolIfNeeded s
| otherwise = s
-- | Formatting options available when displaying Amounts and MixedAmounts.
-- Similar to "AmountStyle" but lower level, and not attached to amounts or commodities.
-- Similar to "AmountStyle" but lower level, not attached to amounts or commodities, and can override it in some ways.
-- See also hledger manual > "Amount formatting, parseability", which speaks of human, hledger, and machine output.
data AmountFormat = AmountFormat
{ displayCommodity :: Bool -- ^ Whether to display commodity symbols.
@ -254,17 +256,25 @@ defaultFmt = AmountFormat {
, displayColour = False
}
-- | Like defaultFmt but show zero amounts with commodity symbol and styling, like non-zero amounts.
fullZeroFmt :: AmountFormat
fullZeroFmt = defaultFmt{displayZeroCommodity=True}
-- | Like defaultFmt but don't show costs.
noCostFmt :: AmountFormat
noCostFmt = defaultFmt{displayCost=False}
-- | Like noCostFmt but display amounts on one line rather than several.
-- | Like defaultFmt but display all amounts on one line.
oneLineFmt :: AmountFormat
oneLineFmt = noCostFmt{displayOneLine=True}
oneLineFmt = defaultFmt{displayOneLine=True}
-- | A (slightly more) machine-readable amount format; like oneLineFmt but don't show digit group marks.
-- | Like noCostFmt but display all amounts on one line.
oneLineNoCostFmt :: AmountFormat
oneLineNoCostFmt = noCostFmt{displayOneLine=True}
-- | A (slightly more) machine-readable amount format; like oneLineNoCostFmt but don't show digit group marks.
machineFmt :: AmountFormat
machineFmt = oneLineFmt{displayDigitGroups=False}
machineFmt = oneLineNoCostFmt{displayDigitGroups=False}
-------------------------------------------------------------------------------
-- Amount arithmetic
@ -1032,7 +1042,7 @@ showMixedAmountWith fmt = wbUnpack . showMixedAmountB fmt
-- | Get the one-line string representation of a mixed amount (also showing any costs).
-- See showMixedAmountB for special cases.
showMixedAmountOneLine :: MixedAmount -> String
showMixedAmountOneLine = wbUnpack . showMixedAmountB oneLineFmt{displayCost=True}
showMixedAmountOneLine = wbUnpack . showMixedAmountB oneLineNoCostFmt{displayCost=True}
-- | Like showMixedAmount, but zero amounts are shown with their
-- commodity if they have one.
@ -1054,7 +1064,7 @@ showMixedAmountWithoutCost c = wbUnpack . showMixedAmountB noCostFmt{displayColo
--
-- > showMixedAmountOneLineWithoutCost c = wbUnpack . showMixedAmountB oneLineFmt{displayColour=c}
showMixedAmountOneLineWithoutCost :: Bool -> MixedAmount -> String
showMixedAmountOneLineWithoutCost c = wbUnpack . showMixedAmountB oneLineFmt{displayColour=c}
showMixedAmountOneLineWithoutCost c = wbUnpack . showMixedAmountB oneLineNoCostFmt{displayColour=c}
-- | Like showMixedAmountOneLineWithoutCost, but show at most the given width,
-- with an elision indicator if there are more.
@ -1062,7 +1072,7 @@ showMixedAmountOneLineWithoutCost c = wbUnpack . showMixedAmountB oneLineFmt{dis
--
-- > showMixedAmountElided w c = wbUnpack . showMixedAmountB oneLineFmt{displayColour=c, displayMaxWidth=Just w}
showMixedAmountElided :: Int -> Bool -> MixedAmount -> String
showMixedAmountElided w c = wbUnpack . showMixedAmountB oneLineFmt{displayColour=c, displayMaxWidth=Just w}
showMixedAmountElided w c = wbUnpack . showMixedAmountB oneLineNoCostFmt{displayColour=c, displayMaxWidth=Just w}
-- | Get an unambiguous string representation of a mixed amount for debugging.
showMixedAmountDebug :: MixedAmount -> String

View File

@ -337,7 +337,7 @@ budgetReportAsTable
rowfuncs :: [CommoditySymbol] -> (BudgetShowMixed, BudgetPercBudget)
rowfuncs cs = case layout_ of
LayoutWide width ->
( pure . showMixedAmountB oneLineFmt{displayMaxWidth=width, displayColour=color_}
( pure . showMixedAmountB oneLineNoCostFmt{displayMaxWidth=width, displayColour=color_}
, \a -> pure . percentage a)
_ -> ( showMixedAmountLinesB noCostFmt{displayCommodity=layout_/=LayoutBare, displayCommodityOrder=Just cs, displayMinWidth=Nothing, displayColour=color_}
, \a b -> fmap (percentage' a b) cs)
@ -466,7 +466,7 @@ budgetReportAsCsv
where
flattentuples tups = concat [[a,b] | (a,b) <- tups]
showNorm = maybe "" (wbToText . showMixedAmountB oneLineFmt)
showNorm = maybe "" (wbToText . showMixedAmountB oneLineNoCostFmt)
rowAsTexts :: (PeriodicReportRow a BudgetCell -> Text)
-> PeriodicReportRow a BudgetCell
@ -480,7 +480,7 @@ budgetReportAsCsv
$ vals
where
cs = S.toList . foldl' S.union mempty . fmap maCommodities $ catMaybes vals
dopts = oneLineFmt{displayCommodity=layout_ /= LayoutBare, displayCommodityOrder=Just cs, displayMinWidth=Nothing}
dopts = oneLineNoCostFmt{displayCommodity=layout_ /= LayoutBare, displayCommodityOrder=Just cs, displayMinWidth=Nothing}
vals = flattentuples as
++ concat [[rowtot, budgettot] | row_total_]
++ concat [[rowavg, budgetavg] | average_]

View File

@ -80,7 +80,7 @@ asDrawHelper UIState{aScreen=scr, aopts=uopts, ajournal=j, aMode=mode} ropts scr
displayitems = ass ^. assList . listElementsL
acctwidths = V.map (\AccountsScreenItem{..} -> asItemIndentLevel + realLength asItemDisplayAccountName) displayitems
balwidths = V.map (maybe 0 (wbWidth . showMixedAmountB oneLineFmt) . asItemMixedAmount) displayitems
balwidths = V.map (maybe 0 (wbWidth . showMixedAmountB oneLineNoCostFmt) . asItemMixedAmount) displayitems
preferredacctwidth = V.maximum acctwidths
totalacctwidthseen = V.sum acctwidths
preferredbalwidth = V.maximum balwidths
@ -169,7 +169,7 @@ asDrawItem (acctwidth, balwidth) selected AccountsScreenItem{..} =
splitAmounts balBuilder
where
balBuilder = maybe mempty showamt asItemMixedAmount
showamt = showMixedAmountB oneLineFmt{displayMinWidth=Just balwidth, displayMaxWidth=Just balwidth}
showamt = showMixedAmountB oneLineNoCostFmt{displayMinWidth=Just balwidth, displayMaxWidth=Just balwidth}
balspace = T.replicate (2 + balwidth - wbWidth balBuilder) " "
splitAmounts = foldr1 (<+>) . intersperse (str ", ") . map renderamt . T.splitOn ", " . wbToText
renderamt :: T.Text -> Widget Name

View File

@ -291,7 +291,7 @@ rsUpdate uopts d j rss@RSS{_rssAccount, _rssForceInclusive, _rssList=oldlist} =
,rsItemTransaction = t
}
where
showamt = showMixedAmountB oneLineFmt{displayMaxWidth=Just 3}
showamt = showMixedAmountB oneLineNoCostFmt{displayMaxWidth=Just 3}
wd = whichDate ropts'
-- blank items are added to allow more control of scroll position; we won't allow movement over these.

View File

@ -110,7 +110,7 @@ registerChartHtml q title percommoditytxnreports = $(hamletFile "templates/chart
colorForCommodity = fromMaybe 0 . flip lookup commoditiesIndex
commoditiesIndex = zip (map fst percommoditytxnreports) [0..] :: [(CommoditySymbol,Int)]
simpleMixedAmountQuantity = maybe 0 aquantity . listToMaybe . amounts . mixedAmountStripCosts
showZeroCommodity = wbUnpack . showMixedAmountB oneLineFmt{displayCost=False,displayZeroCommodity=True}
showZeroCommodity = wbUnpack . showMixedAmountB oneLineNoCostFmt{displayCost=False,displayZeroCommodity=True}
shownull c = if null c then " " else c
nodatelink = (RegisterR, [("q", T.unwords $ removeDates q)])

View File

@ -148,9 +148,9 @@ htmlRow CliOpts{reportspec_=ReportSpec{_rsReportOpts=ropts}} reportq thisacctq
L.tr_ (do (L.td_ . toHtml . show . transactionRegisterDate (whichDate ropts) reportq thisacctq) t
(L.td_ . toHtml) tdescription
(L.td_ . toHtml) otheracctsstr
-- piggy back on the oneLineFmt display style for now.
(L.td_ . toHtml . wbUnpack . showMixedAmountB oneLineFmt) amt
(L.td_ . toHtml . wbUnpack . showMixedAmountB oneLineFmt) bal)
-- piggy back on the oneLineNoCostFmt display style for now.
(L.td_ . toHtml . wbUnpack . showMixedAmountB oneLineNoCostFmt) amt
(L.td_ . toHtml . wbUnpack . showMixedAmountB oneLineNoCostFmt) bal)
-- | Render a register report as plain text suitable for console output.
accountTransactionsReportAsText :: CliOpts -> Query -> Query -> AccountTransactionsReport -> TL.Text

View File

@ -470,7 +470,7 @@ balanceReportAsText' opts ((items, total)) =
[ Cell TopRight damts
, Cell TopLeft (fmap wbFromText cs)
, Cell TopLeft (replicate (length damts - 1) mempty ++ [wbFromText dispname]) ]
where dopts = oneLineFmt{displayCommodity=layout_ opts /= LayoutBare, displayCommodityOrder=Just cs, displayColour=color_ opts}
where dopts = oneLineNoCostFmt{displayCommodity=layout_ opts /= LayoutBare, displayCommodityOrder=Just cs, displayColour=color_ opts}
cs = if mixedAmountLooksZero amt then [""] else S.toList $ maCommodities amt
dispname = T.replicate ((dep - 1) * 2) " " <> acctname
damts = showMixedAmountLinesB dopts amt
@ -781,7 +781,7 @@ multiBalanceRowAsCsvText :: ReportOpts -> [DateSpan] -> PeriodicReportRow a Mixe
multiBalanceRowAsCsvText opts colspans = fmap (fmap wbToText) . multiBalanceRowAsWbs machineFmt opts colspans
multiBalanceRowAsTableText :: ReportOpts -> PeriodicReportRow a MixedAmount -> [[WideBuilder]]
multiBalanceRowAsTableText opts = multiBalanceRowAsWbs oneLineFmt{displayColour=color_ opts} opts []
multiBalanceRowAsTableText opts = multiBalanceRowAsWbs oneLineNoCostFmt{displayColour=color_ opts} opts []
tests_Balance = testGroup "Balance" [

View File

@ -282,7 +282,7 @@ postingsOrTransactionsReportAsText alignAll opts itemAsText itemamt itembal repo
startAlign = (if alignAll then id else take chunkSize) itemsWithAmounts
itemsWithAmounts = map (\x -> (x, showAmt $ itemamt x, showAmt $ itembal x)) report
showAmt = showMixedAmountLinesB oneLineFmt{displayColour=opts^.color__}
showAmt = showMixedAmountLinesB oneLineNoCostFmt{displayColour=opts^.color__}
amt = second3
bal = third3