From 92d67926f5c1b021907a4ebfc905408e5693c5d2 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Sun, 24 May 2009 06:22:44 +0000 Subject: [PATCH] print, register: show virtual accounts bracketed/parenthesised --- Ledger/LedgerTransaction.hs | 15 +++++++++++++-- Ledger/Transaction.hs | 5 +++-- Tests.hs | 9 +++++++++ 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/Ledger/LedgerTransaction.hs b/Ledger/LedgerTransaction.hs index cdacf8945..5fc064298 100644 --- a/Ledger/LedgerTransaction.hs +++ b/Ledger/LedgerTransaction.hs @@ -74,12 +74,23 @@ showLedgerTransaction' elide t = where showposting p = showacct p ++ " " ++ (showamount $ pamount p) ++ (showcomment $ pcomment p) showpostingnoamt p = rstrip $ showacct p ++ " " ++ (showcomment $ pcomment p) - showacct p = " " ++ showstatus p ++ (showaccountname $ paccount p) + showacct p = " " ++ showstatus p ++ (printf "%-34s" $ showAccountName (Just 34) (ptype p) (paccount p)) showamount = printf "%12s" . showMixedAmount - showaccountname s = printf "%-34s" s showcomment s = if (length s) > 0 then " ; "++s else "" 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. +showAccountName :: Maybe Int -> PostingType -> AccountName -> String +showAccountName w = fmt + where + fmt RegularPosting = take w' + fmt VirtualPosting = parenthesise . reverse . take (w'-2) . reverse + fmt BalancedVirtualPosting = bracket . reverse . take (w'-2) . reverse + w' = fromMaybe 999999 w + parenthesise s = "("++s++")" + bracket s = "["++s++"]" + isLedgerTransactionBalanced :: LedgerTransaction -> Bool isLedgerTransactionBalanced (LedgerTransaction {ltpostings=ps}) = all (isReallyZeroMixedAmount . costOfMixedAmount . sum . map pamount) diff --git a/Ledger/Transaction.hs b/Ledger/Transaction.hs index 6d1d0d756..1e326347e 100644 --- a/Ledger/Transaction.hs +++ b/Ledger/Transaction.hs @@ -14,7 +14,7 @@ import Ledger.Dates import Ledger.Utils import Ledger.Types import Ledger.Dates -import Ledger.LedgerTransaction +import Ledger.LedgerTransaction (showAccountName) import Ledger.Posting import Ledger.Amount @@ -23,8 +23,9 @@ instance Show Transaction where show=showTransaction showTransaction :: Transaction -> String showTransaction (Transaction eno stat d desc a amt ttype) = - s ++ unwords [showDate d,desc,a,show amt,show ttype] + s ++ unwords [showDate d,desc,a',show amt,show ttype] where s = if stat then " *" else "" + a' = showAccountName Nothing ttype a -- | Convert a 'LedgerTransaction' to two or more 'Transaction's. An id number -- is attached to the transactions to preserve their grouping - it should diff --git a/Tests.hs b/Tests.hs index 6ecd1c399..1052edd59 100644 --- a/Tests.hs +++ b/Tests.hs @@ -92,6 +92,15 @@ hledger.hs: could not balance this transaction, amounts do not add up to zero: @ +@ +$ printf "2009/1/1 x\n (virtual) 100\n a 1\n b\n" | runhaskell hledger.hs -f- print 2>&1 ; true +2009/01/01 x + (virtual) 100 + a 1 + b + +@ + Unicode input/output tests -- layout of the balance command with unicode names