print, register: show virtual accounts bracketed/parenthesised

This commit is contained in:
Simon Michael 2009-05-24 06:22:44 +00:00
parent 2373429b64
commit 92d67926f5
3 changed files with 25 additions and 4 deletions

View File

@ -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)

View File

@ -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

View File

@ -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