add: don't let number style disturb the default (Fixes #1378)

This commit is contained in:
Simon Michael 2020-11-14 11:08:01 -08:00
parent e8672c4cd5
commit 0ce9c352ec
2 changed files with 18 additions and 3 deletions

View File

@ -69,6 +69,7 @@ module Hledger.Data.Amount (
amountstyle, amountstyle,
styleAmount, styleAmount,
styleAmountExceptPrecision, styleAmountExceptPrecision,
amountUnstyled,
showAmount, showAmount,
cshowAmount, cshowAmount,
showAmountWithZeroCommodity, showAmountWithZeroCommodity,
@ -108,6 +109,7 @@ module Hledger.Data.Amount (
mixedAmountTotalPriceToUnitPrice, mixedAmountTotalPriceToUnitPrice,
-- ** rendering -- ** rendering
styleMixedAmount, styleMixedAmount,
mixedAmountUnstyled,
showMixedAmount, showMixedAmount,
showMixedAmountOneLine, showMixedAmountOneLine,
showMixedAmountDebug, showMixedAmountDebug,
@ -351,6 +353,10 @@ styleAmountExceptPrecision styles a@Amount{astyle=AmountStyle{asprecision=origp}
Just s -> a{astyle=s{asprecision=origp}} Just s -> a{astyle=s{asprecision=origp}}
Nothing -> a Nothing -> a
-- | Reset this amount's display style to the default.
amountUnstyled :: Amount -> Amount
amountUnstyled a = a{astyle=amountstyle}
-- | Get the string representation of an amount, based on its -- | Get the string representation of an amount, based on its
-- commodity's display settings. String representations equivalent to -- commodity's display settings. String representations equivalent to
-- zero are converted to just \"0\". The special "missing" amount is -- zero are converted to just \"0\". The special "missing" amount is
@ -609,6 +615,10 @@ mixedAmountIsZero = all amountIsZero . amounts . normaliseMixedAmountSquashPrice
styleMixedAmount :: M.Map CommoditySymbol AmountStyle -> MixedAmount -> MixedAmount styleMixedAmount :: M.Map CommoditySymbol AmountStyle -> MixedAmount -> MixedAmount
styleMixedAmount styles (Mixed as) = Mixed $ map (styleAmount styles) as styleMixedAmount styles (Mixed as) = Mixed $ map (styleAmount styles) as
-- | Reset each individual amount's display style to the default.
mixedAmountUnstyled :: MixedAmount -> MixedAmount
mixedAmountUnstyled = mapMixedAmount amountUnstyled
-- | Get the string representation of a mixed amount, after -- | Get the string representation of a mixed amount, after
-- normalising it to one amount per commodity. Assumes amounts have -- normalising it to one amount per commodity. Assumes amounts have
-- no or similar prices, otherwise this can show misleading prices. -- no or similar prices, otherwise this can show misleading prices.

View File

@ -333,8 +333,12 @@ amountAndCommentWizard PrevInput{..} EntryState{..} = do
(mhistoricalp,followedhistoricalsofar) = (mhistoricalp,followedhistoricalsofar) =
case esSimilarTransaction of case esSimilarTransaction of
Nothing -> (Nothing,False) Nothing -> (Nothing,False)
Just Transaction{tpostings=ps} -> (if length ps >= pnum then Just (ps !! (pnum-1)) else Nothing Just Transaction{tpostings=ps} ->
,all (\(a,b) -> pamount a == pamount b) $ zip esPostings ps) ( if length ps >= pnum then Just (ps !! (pnum-1)) else Nothing
, all sameamount $ zip esPostings ps
)
where
sameamount (p1,p2) = mixedAmountUnstyled (pamount p1) == mixedAmountUnstyled (pamount p2)
def = case (esArgs, mhistoricalp, followedhistoricalsofar) of def = case (esArgs, mhistoricalp, followedhistoricalsofar) of
(d:_,_,_) -> d (d:_,_,_) -> d
(_,Just hp,True) -> showamt $ pamount hp (_,Just hp,True) -> showamt $ pamount hp
@ -343,7 +347,8 @@ amountAndCommentWizard PrevInput{..} EntryState{..} = do
retryMsg "A valid hledger amount is required. Eg: 1, $2, 3 EUR, \"4 red apples\"." $ retryMsg "A valid hledger amount is required. Eg: 1, $2, 3 EUR, \"4 red apples\"." $
parser parseAmountAndComment $ parser parseAmountAndComment $
withCompletion (amountCompleter def) $ withCompletion (amountCompleter def) $
defaultTo' def $ nonEmpty $ defaultTo' def $
nonEmpty $
linePrewritten (green $ printf "Amount %d%s: " pnum (showDefault def)) (fromMaybe "" $ prevAmountAndCmnt `atMay` length esPostings) "" linePrewritten (green $ printf "Amount %d%s: " pnum (showDefault def)) (fromMaybe "" $ prevAmountAndCmnt `atMay` length esPostings) ""
where where
parseAmountAndComment s = if s == "<" then return Nothing else either (const Nothing) (return . Just) $ parseAmountAndComment s = if s == "<" then return Nothing else either (const Nothing) (return . Just) $