diff --git a/hledger/Hledger/Cli/Add.hs b/hledger/Hledger/Cli/Add.hs index 3bdf9c6cc..5220e74d4 100644 --- a/hledger/Hledger/Cli/Add.hs +++ b/hledger/Hledger/Cli/Add.hs @@ -92,6 +92,7 @@ getTransaction j opts args defaultDate = do hPutStr stderr $ concatMap (\(n,t) -> printf "[%3d%%] %s" (round $ n*100 :: Int) (show t)) $ take 3 historymatches) getpostingsandvalidate +-- fragile -- | Read postings from the command line until . is entered, using any -- provided historical postings and the journal context to guess defaults. getPostings :: JournalContext -> (AccountName -> Bool) -> Maybe [Posting] -> [Posting] -> InputT IO [Posting] @@ -111,16 +112,22 @@ getPostings ctx accept historicalps enteredps = do | n <= length ps = Just $ ps !! (n-1) | otherwise = Nothing where Just ps = historicalps' - defaultamountstr | isJust bestmatch' = Just $ showMixedAmountWithPrecision maxprecision $ pamount $ fromJust bestmatch' + defaultamountstr | isJust bestmatch' = Just historicalamountstr | n > 1 = Just balancingamountstr | otherwise = Nothing - where balancingamountstr = showMixedAmountWithPrecision maxprecision $ negate $ sumMixedAmountsPreservingHighestPrecision $ map pamount enteredrealps + where + historicalamountstr = showMixedAmountWithPrecision maxprecision $ pamount $ fromJust bestmatch' + balancingamountstr = showMixedAmountWithPrecision maxprecision $ negate $ sumMixedAmountsPreservingHighestPrecision $ map pamount enteredrealps amountstr <- askFor (printf "amount %d" n) defaultamountstr validateamount - let amount = setMixedAmountPrecision maxprecision $ fromparse $ runParser (someamount <|> return missingamt) ctx "" amountstr + let amount = fromparse $ runParser (someamount <|> return missingamt) ctx "" amountstr + amount' = fromparse $ runParser (someamount <|> return missingamt) nullctx "" amountstr defaultamtused = Just (showMixedAmount amount) == defaultamountstr historicalps'' = if defaultamtused then historicalps' else Nothing - commodityadded | showMixedAmountWithPrecision maxprecision amount == amountstr = Nothing - | otherwise = maybe Nothing (Just . commodity) $ headMay $ amounts amount + commodityadded | c == cwithnodef = Nothing + | otherwise = c + where c = maybemixedamountcommodity amount + cwithnodef = maybemixedamountcommodity amount' + maybemixedamountcommodity = maybe Nothing (Just . commodity) . headMay . amounts p = nullposting{paccount=stripbrackets account, pamount=amount, ptype=postingtype account} diff --git a/tests/add-bad-date-fails.test b/tests/add-bad-date-fails.test index 07d4997b9..016ad2219 100644 --- a/tests/add-bad-date-fails.test +++ b/tests/add-bad-date-fails.test @@ -2,4 +2,4 @@ bin/hledger -f nosuch.journal add <<< 2009/1/32 ->>>2 /date .*: date .*/ +>>> /date .*: date .*/ diff --git a/tests/add-blank-date.test b/tests/add-blank-date.test index 10c7345a9..6e9b180b7 100644 --- a/tests/add-blank-date.test +++ b/tests/add-blank-date.test @@ -1,4 +1,4 @@ bin/hledger -f nosuch.journal add <<< ->>>2 /date .*: description/ +>>> /date .*: description/ diff --git a/tests/add-default-amount-precision.test b/tests/add-default-amount-precision.test index 015ef0629..b4bc78718 100644 --- a/tests/add-default-amount-precision.test +++ b/tests/add-default-amount-precision.test @@ -8,4 +8,4 @@ a b 0.5 c ->>>2 /amount 3 \[-0.75\]/ +>>> /amount 3 \[-0.75\]/ diff --git a/tests/add-default-commodity.test b/tests/add-default-commodity.test index 0735c77f1..3c9490183 100644 --- a/tests/add-default-commodity.test +++ b/tests/add-default-commodity.test @@ -1,15 +1,71 @@ -# add should use the (final) default commodity if any -# disabled as add currently requires ctrl-c to terminate +# simple add with no existing journal, no commodity entered + rm -f add-default-commodity-$$.j; bin/hledger -f add-default-commodity-$$.j add; rm -f add-default-commodity-$$.j +<<< + + +a +1000.0 +b + +. +>>> /^date \[.*\]: description \[\]: account 1: amount 1: account 2: amount 2 \[-1000\]: account 3: date \[.*\]: $/ # -# printf 'D £1000.00\n' >add-default-commodity-$$.j; hledger -fadd-default-commodity-$$.j add; rm -f add-default-commodity-$$.j -# <<< -# 2010/1/1 +# default commodity with greater precision + printf 'D $1000.00\n' >add-default-commodity-$$.j; hledger -fadd-default-commodity-$$.j add >/dev/null; cat add-default-commodity-$$.j; rm -f add-default-commodity-$$.j +<<< -# a -# 1000 -# b -# >>> -# 2010/01/01 y -# a £1000.00 -# b £-1000.00 +a +$1000.0 +b + +. +>>> /a +\$1000\.0/ +# +# default commodity with less precision + printf 'D $1000.0\n' >add-default-commodity-$$.j; hledger -fadd-default-commodity-$$.j add >/dev/null; cat add-default-commodity-$$.j; rm -f add-default-commodity-$$.j +<<< + + +a +$1000.00 +b + +. +>>> /a +\$1000\.00/ +# +# existing commodity with greater precision + printf '2010/1/1\n a $1000.00\n b\n' >add-default-commodity-$$.j; hledger -fadd-default-commodity-$$.j add >/dev/null; cat add-default-commodity-$$.j; rm -f add-default-commodity-$$.j +<<< + + +a +$1000.0 +b + +. +>>> /a +\$1000\.0/ +# +# existing commodity with less precision + printf '2010/1/1\n a $1000.0\n b\n' >add-default-commodity-$$.j; hledger -fadd-default-commodity-$$.j add >/dev/null; cat add-default-commodity-$$.j; rm -f add-default-commodity-$$.j +<<< + + +a +$1000.00 +b + +. +>>> /a +\$1000\.00/ +# +# no commodity entered, the (most recent) default commodity should be applied + printf 'D $1000.0\nD £1,000.00\n' >add-default-commodity-$$.j; hledger -fadd-default-commodity-$$.j add; cat add-default-commodity-$$.j; rm -f add-default-commodity-$$.j +<<< +2010/1/1 + +a +1000 +b + +. +>>> /a +£1,000.00/