diff --git a/hledger-lib/Hledger/Data/Amount.hs b/hledger-lib/Hledger/Data/Amount.hs index 5b7ad2f06..e77cb557d 100644 --- a/hledger-lib/Hledger/Data/Amount.hs +++ b/hledger-lib/Hledger/Data/Amount.hs @@ -69,6 +69,7 @@ module Hledger.Data.Amount ( amountstyle, styleAmount, styleAmountExceptPrecision, + amountUnstyled, showAmount, cshowAmount, showAmountWithZeroCommodity, @@ -108,6 +109,7 @@ module Hledger.Data.Amount ( mixedAmountTotalPriceToUnitPrice, -- ** rendering styleMixedAmount, + mixedAmountUnstyled, showMixedAmount, showMixedAmountOneLine, showMixedAmountDebug, @@ -351,6 +353,10 @@ styleAmountExceptPrecision styles a@Amount{astyle=AmountStyle{asprecision=origp} Just s -> a{astyle=s{asprecision=origp}} 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 -- commodity's display settings. String representations equivalent to -- 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 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 -- normalising it to one amount per commodity. Assumes amounts have -- no or similar prices, otherwise this can show misleading prices. diff --git a/hledger/Hledger/Cli/Commands/Add.hs b/hledger/Hledger/Cli/Commands/Add.hs index 36c8e0183..237525b7d 100644 --- a/hledger/Hledger/Cli/Commands/Add.hs +++ b/hledger/Hledger/Cli/Commands/Add.hs @@ -333,8 +333,12 @@ amountAndCommentWizard PrevInput{..} EntryState{..} = do (mhistoricalp,followedhistoricalsofar) = case esSimilarTransaction of Nothing -> (Nothing,False) - Just Transaction{tpostings=ps} -> (if length ps >= pnum then Just (ps !! (pnum-1)) else Nothing - ,all (\(a,b) -> pamount a == pamount b) $ zip esPostings ps) + Just Transaction{tpostings=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 (d:_,_,_) -> d (_,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\"." $ parser parseAmountAndComment $ withCompletion (amountCompleter def) $ - defaultTo' def $ nonEmpty $ + defaultTo' def $ + nonEmpty $ linePrewritten (green $ printf "Amount %d%s: " pnum (showDefault def)) (fromMaybe "" $ prevAmountAndCmnt `atMay` length esPostings) "" where parseAmountAndComment s = if s == "<" then return Nothing else either (const Nothing) (return . Just) $