From b5d1ebba3089cefee2b1f5f2fdeb5d39ccecd7b4 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Sun, 11 Jul 2010 22:51:36 +0000 Subject: [PATCH] print, register, balance: fix layout with mixed-commodity amounts --- Hledger/Cli/Commands/Register.hs | 2 +- hledger-lib/Hledger/Data/Transaction.hs | 13 +++++--- tests/amount-layout-vertical.test | 41 +++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 tests/amount-layout-vertical.test diff --git a/Hledger/Cli/Commands/Register.hs b/Hledger/Cli/Commands/Register.hs index 2737e44d7..a921d7909 100644 --- a/Hledger/Cli/Commands/Register.hs +++ b/Hledger/Cli/Commands/Register.hs @@ -150,7 +150,7 @@ showPostingsWithBalance (p:ps) pprev bal = this ++ showPostingsWithBalance ps p -- | Show one posting and running balance, with or without transaction info. showPostingWithBalance :: Bool -> Posting -> MixedAmount -> String -showPostingWithBalance withtxninfo p b = concatBottomPadded [txninfo ++ pstr ++ " ", bal] ++ "\n" +showPostingWithBalance withtxninfo p b = concatTopPadded [txninfo, pstr, " ", bal] ++ "\n" where ledger3ishlayout = False datedescwidth = if ledger3ishlayout then 34 else 32 diff --git a/hledger-lib/Hledger/Data/Transaction.hs b/hledger-lib/Hledger/Data/Transaction.hs index 65ffcc9ad..501f6a2bb 100644 --- a/hledger-lib/Hledger/Data/Transaction.hs +++ b/hledger-lib/Hledger/Data/Transaction.hs @@ -78,13 +78,18 @@ showTransaction' elide effective t = = map showposting (init ps) ++ [showpostingnoamt (last ps)] | otherwise = map showposting ps where - showposting p = showacct p ++ " " ++ showamount (pamount p) ++ showcomment (pcomment p) showpostingnoamt p = rstrip $ showacct p ++ " " ++ showcomment (pcomment p) + showposting p = concatTopPadded [showacct p + ," " + ,showamt (pamount p) + ,showcomment (pcomment p) + ] showacct p = " " ++ showstatus p ++ printf (printf "%%-%ds" w) (showAccountName Nothing (ptype p) (paccount p)) - w = maximum $ map (length . paccount) ps - showamount = printf "%12s" . showMixedAmountOrZero + where w = maximum $ map (length . paccount) ps + showstatus p = if pstatus p then "* " else "" + showamt = + padleft 12 . showMixedAmountOrZero showcomment s = if null s then "" else " ; "++s - showstatus p = if pstatus p then "* " else "" -- | Show an account name, clipped to the given width if any, and -- appropriately bracketed/parenthesised for the given posting type. diff --git a/tests/amount-layout-vertical.test b/tests/amount-layout-vertical.test new file mode 100644 index 000000000..9e19693a1 --- /dev/null +++ b/tests/amount-layout-vertical.test @@ -0,0 +1,41 @@ +# test mixed amount layout with various commands +bin/hledger -f - print +<<< +2010/1/1 + a EUR 1 ; a euro + b USD 1 ; a dollar + c ; a euro and a dollar +>>> +2010/01/01 + a EUR 1 ; a euro + b USD 1 ; a dollar + EUR -1 + c USD -1 ; a euro and a dollar + +# +bin/hledger -f - register +<<< +2010/1/1 + a EUR 1 ; a euro + b USD 1 ; a dollar + c ; a euro and a dollar +>>> +2010/01/01 a EUR 1 EUR 1 + EUR 1 + b USD 1 USD 1 + EUR -1 + c USD -1 0 +# +bin/hledger -f - balance +<<< +2010/1/1 + a EUR 1 ; a euro + b USD 1 ; a dollar + c ; a euro and a dollar +>>> + EUR 1 a + USD 1 b + EUR -1 + USD -1 c +-------------------- + EUR 0