From df79aa5e35f4dc23706e433811945e8d04b60216 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Tue, 23 Jan 2024 16:20:28 -1000 Subject: [PATCH] dev: distinguish oneLineFmt and oneLineNoCostFmt; add fullZeroFmt --- hledger-lib/Hledger/Data/Amount.hs | 26 ++++++++++++++------ hledger-lib/Hledger/Reports/BudgetReport.hs | 6 ++--- hledger-ui/Hledger/UI/AccountsScreen.hs | 4 +-- hledger-ui/Hledger/UI/UIScreens.hs | 2 +- hledger-web/Hledger/Web/Handler/RegisterR.hs | 2 +- hledger/Hledger/Cli/Commands/Aregister.hs | 6 ++--- hledger/Hledger/Cli/Commands/Balance.hs | 4 +-- hledger/Hledger/Cli/Utils.hs | 2 +- 8 files changed, 31 insertions(+), 21 deletions(-) diff --git a/hledger-lib/Hledger/Data/Amount.hs b/hledger-lib/Hledger/Data/Amount.hs index 55e858be6..ae6fc5690 100644 --- a/hledger-lib/Hledger/Data/Amount.hs +++ b/hledger-lib/Hledger/Data/Amount.hs @@ -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 diff --git a/hledger-lib/Hledger/Reports/BudgetReport.hs b/hledger-lib/Hledger/Reports/BudgetReport.hs index 251c45ab7..b68add901 100644 --- a/hledger-lib/Hledger/Reports/BudgetReport.hs +++ b/hledger-lib/Hledger/Reports/BudgetReport.hs @@ -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_] diff --git a/hledger-ui/Hledger/UI/AccountsScreen.hs b/hledger-ui/Hledger/UI/AccountsScreen.hs index 10e7b731b..af5dd99bb 100644 --- a/hledger-ui/Hledger/UI/AccountsScreen.hs +++ b/hledger-ui/Hledger/UI/AccountsScreen.hs @@ -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 diff --git a/hledger-ui/Hledger/UI/UIScreens.hs b/hledger-ui/Hledger/UI/UIScreens.hs index 67f12a5e4..8dde1c56f 100644 --- a/hledger-ui/Hledger/UI/UIScreens.hs +++ b/hledger-ui/Hledger/UI/UIScreens.hs @@ -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. diff --git a/hledger-web/Hledger/Web/Handler/RegisterR.hs b/hledger-web/Hledger/Web/Handler/RegisterR.hs index 2bce45854..1faead720 100644 --- a/hledger-web/Hledger/Web/Handler/RegisterR.hs +++ b/hledger-web/Hledger/Web/Handler/RegisterR.hs @@ -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)]) diff --git a/hledger/Hledger/Cli/Commands/Aregister.hs b/hledger/Hledger/Cli/Commands/Aregister.hs index b4d571aee..5cf7f6d41 100644 --- a/hledger/Hledger/Cli/Commands/Aregister.hs +++ b/hledger/Hledger/Cli/Commands/Aregister.hs @@ -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 diff --git a/hledger/Hledger/Cli/Commands/Balance.hs b/hledger/Hledger/Cli/Commands/Balance.hs index 9adadb4f2..5474198c3 100644 --- a/hledger/Hledger/Cli/Commands/Balance.hs +++ b/hledger/Hledger/Cli/Commands/Balance.hs @@ -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" [ diff --git a/hledger/Hledger/Cli/Utils.hs b/hledger/Hledger/Cli/Utils.hs index c9b4f1c11..2cd69d1eb 100644 --- a/hledger/Hledger/Cli/Utils.hs +++ b/hledger/Hledger/Cli/Utils.hs @@ -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