From 1fcc8f1c896e9ab72f023bb51f7f55db56e334f9 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Thu, 4 Jan 2024 13:24:57 -1000 Subject: [PATCH] fix:add: apply the D default commodity to bare numbers, per docs [#815] --- hledger/Hledger/Cli/Commands/Add.hs | 3 +-- hledger/Hledger/Cli/Commands/Add.md | 2 +- hledger/test/add.test | 22 ++++++++++++++++++---- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/hledger/Hledger/Cli/Commands/Add.hs b/hledger/Hledger/Cli/Commands/Add.hs index 7a3fbb951..61c9bee28 100644 --- a/hledger/Hledger/Cli/Commands/Add.hs +++ b/hledger/Hledger/Cli/Commands/Add.hs @@ -348,10 +348,9 @@ amountAndCommentWizard PrevInput{..} EntryState{..} = do where parseAmountAndComment s = if s == "<" then return Nothing else either (const Nothing) (return . Just) $ runParser - (evalStateT (amountandcommentp <* eof) nodefcommodityj) + (evalStateT (amountandcommentp <* eof) esJournal) "" (T.pack s) - nodefcommodityj = esJournal{jparsedefaultcommodity=Nothing} amountandcommentp :: JournalParser Identity (Amount, Text) amountandcommentp = do a <- amountp diff --git a/hledger/Hledger/Cli/Commands/Add.md b/hledger/Hledger/Cli/Commands/Add.md index b49bbcc27..6ca0f4941 100644 --- a/hledger/Hledger/Cli/Commands/Add.md +++ b/hledger/Hledger/Cli/Commands/Add.md @@ -26,7 +26,7 @@ Features: - The tab key will auto-complete whenever possible - accounts, payees/descriptions, dates (`yesterday`, `today`, `tomorrow`). If the input area is empty, it will insert the default value. -- If the journal defines a [default commodity](#default-commodity), +- If the journal defines a [default commodity](#d-directive), it will be added to any bare numbers entered. - A parenthesised transaction [code](#entries) may be entered following a date. - [Comments](#transaction-comments) and tags may be entered following a description or amount. diff --git a/hledger/test/add.test b/hledger/test/add.test index 4db9de915..724e35bac 100644 --- a/hledger/test/add.test +++ b/hledger/test/add.test @@ -60,7 +60,21 @@ $$$ printf 'D $1000.0\n' >t$$.j; hledger -f t$$.j add >/dev/null; cat t$$.j; rm >>> /a +\$1000\.00/ >>>2 // -# ** 6. existing commodity with greater precision +# ** 6. default commodity symbol is added when not entered +<<< + + +a +1000 +b + +. + +$$$ printf 'D $1000.00\n' >t$$.j; hledger -f t$$.j add >/dev/null; cat t$$.j; rm -f t$$.j +>>> /a +\$1000\.00/ +>>>2 // + +# ** 7. existing commodity with greater precision <<< @@ -73,7 +87,7 @@ $$$ printf '2010/1/1\n a $1000.00\n b\n' >t$$.j; hledger -f t$$.j add >/dev/nu >>> /a +\$1000\.0/ >>>2 // -# ** 7. existing commodity with less precision +# ** 8. existing commodity with less precision <<< @@ -87,7 +101,7 @@ $$$ printf '2010/1/1\n a $1000.0\n b\n' >t$$.j; hledger -f t$$.j add >/dev/nul >>> /a +\$1000\.00/ >>>2 // -# ** 8. default amounts should not fail to balance due to precision +# ** 9. default amounts should not fail to balance due to precision <<< 2010/1/1 x @@ -100,7 +114,7 @@ $$$ rm -f nosuch.journal; hledger -f nosuch.journal add; rm -f nosuch.journal >>> /Amount 3 \[-0.75\]:/ >>>2 // -## 9. shouldn't add decimals if there aren't any +## 10. shouldn't add decimals if there aren't any ## printf '\n\na\n1\nb\n' | hledger -f /dev/null add # hledger -f /dev/null add # <<<