From edfcd3554f687516adda26de4455f3e90976d1df Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Wed, 15 Jan 2025 16:05:39 -1000 Subject: [PATCH] lib: showAmountCost(B): drop leading whitespace --- hledger-lib/Hledger/Data/Amount.hs | 10 +++++----- hledger-lib/Hledger/Data/Journal.hs | 6 +++--- hledger/test/pivot.test | 14 +++++++------- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/hledger-lib/Hledger/Data/Amount.hs b/hledger-lib/Hledger/Data/Amount.hs index 347bb9d8fa8..ccf85ef2de9 100644 --- a/hledger-lib/Hledger/Data/Amount.hs +++ b/hledger-lib/Hledger/Data/Amount.hs @@ -673,22 +673,22 @@ showAmountB space = if not (T.null comm) && ascommodityspaced style then WideBuilder (TB.singleton ' ') 1 else mempty cost = if displayCost then showAmountCostB afmt a else mempty --- Show an amount's cost as @ UNITCOST or @@ TOTALCOST. +-- Show an amount's cost as @ UNITCOST or @@ TOTALCOST, plus a leading space, or "" if there's no cost. showAmountCost :: Amount -> String showAmountCost = wbUnpack . showAmountCostB defaultFmt --- Show an amount's cost as @ UNITCOST or @@ TOTALCOST (builder version). +-- showAmountCost, efficient builder version. showAmountCostB :: AmountFormat -> Amount -> WideBuilder showAmountCostB afmt amt = case acost amt of - Nothing -> mempty + Nothing -> mempty Just (UnitCost pa) -> WideBuilder (TB.fromString " @ ") 3 <> showAmountB afmt pa Just (TotalCost pa) -> WideBuilder (TB.fromString " @@ ") 4 <> showAmountB afmt (sign pa) where sign = if aquantity amt < 0 then negate else id showAmountCostDebug :: Maybe AmountCost -> String showAmountCostDebug Nothing = "" -showAmountCostDebug (Just (UnitCost pa)) = " @ " ++ showAmountDebug pa -showAmountCostDebug (Just (TotalCost pa)) = " @@ " ++ showAmountDebug pa +showAmountCostDebug (Just (UnitCost pa)) = "@ " ++ showAmountDebug pa +showAmountCostDebug (Just (TotalCost pa)) = "@@ " ++ showAmountDebug pa -- | Colour version. For a negative amount, adds ANSI codes to change the colour, -- currently to hard-coded red. diff --git a/hledger-lib/Hledger/Data/Journal.hs b/hledger-lib/Hledger/Data/Journal.hs index 8e34088b97e..2ba95ee4f33 100644 --- a/hledger-lib/Hledger/Data/Journal.hs +++ b/hledger-lib/Hledger/Data/Journal.hs @@ -1176,9 +1176,9 @@ pivotComponent fieldortagname p | fieldortagname == "note", Just t <- ptransaction p = transactionNote t | fieldortagname == "status", Just t <- ptransaction p = T.pack . show . tstatus $ t | fieldortagname == "acct" = paccount p - | fieldortagname `elem` commnames = case map acommodity $ amounts $ pamount p of [s] -> s; _ -> unknown - | fieldortagname == "amt" = case amounts $ pamount p of [a] -> T.pack $ show $ aquantity a; _ -> unknown - | fieldortagname == "cost" = case amounts $ pamount p of [a@Amount{acost=Just _}] -> T.pack $ showAmountCost a; _ -> unknown + | fieldortagname `elem` commnames = case map acommodity $ amounts $ pamount p of [s] -> s; _ -> unknown + | fieldortagname == "amt" = case amounts $ pamount p of [a] -> T.pack $ show $ aquantity a; _ -> unknown + | fieldortagname == "cost" = case amounts $ pamount p of [a@Amount{acost=Just _}] -> T.pack $ lstrip $ showAmountCost a; _ -> unknown | Just (_, tagvalue) <- postingFindTag fieldortagname p = tagvalue | otherwise = unknown where diff --git a/hledger/test/pivot.test b/hledger/test/pivot.test index e34b53b283b..e4dae385bbe 100644 --- a/hledger/test/pivot.test +++ b/hledger/test/pivot.test @@ -200,10 +200,10 @@ Balance changes in 2025: $ hledger -f- bal -Y --pivot=cost Balance changes in 2025: - || 2025 -=========++================= - || $-210 - @ $10 || 1 A - @ $100 || 2 B ----------++----------------- - || $-210, 1 A, 2 B + || 2025 +========++================= + || $-210 + @ $10 || 1 A + @ $100 || 2 B +--------++----------------- + || $-210, 1 A, 2 B