more code cleanups
This commit is contained in:
parent
628c4241f3
commit
a1d10691a3
@ -164,21 +164,22 @@ showBalanceReport opts args l = acctsstr ++ totalstr
|
|||||||
-- eliding boring parent accounts. Requires a list of the account names we
|
-- eliding boring parent accounts. Requires a list of the account names we
|
||||||
-- are interested in to help with that.
|
-- are interested in to help with that.
|
||||||
showAccountTreeWithBalances :: [AccountName] -> Tree Account -> String
|
showAccountTreeWithBalances :: [AccountName] -> Tree Account -> String
|
||||||
showAccountTreeWithBalances matchedacctnames =
|
showAccountTreeWithBalances matchednames =
|
||||||
showAccountTreeWithBalances' matchedacctnames 0 ""
|
showAccountTreeWithBalances' matchednames 0 ""
|
||||||
where
|
where
|
||||||
showAccountTreeWithBalances' :: [AccountName] -> Int -> String -> Tree Account -> String
|
showAccountTreeWithBalances' :: [AccountName] -> Int -> String -> Tree Account -> String
|
||||||
showAccountTreeWithBalances' matchedacctnames indentlevel prefix (Node (Account fullname _ bal) subs) =
|
showAccountTreeWithBalances' matchednames indent prefix (Node (Account fullname _ bal) subs)
|
||||||
if isboringparent then showsubswithprefix else showacct ++ showsubswithindent
|
| not isboringparent = this ++ subswithindent
|
||||||
|
| otherwise = subswithprefix
|
||||||
where
|
where
|
||||||
showsubswithprefix = showsubs indentlevel (prefix++leafname++":")
|
subswithindent = showsubs (indent+1) ""
|
||||||
showsubswithindent = showsubs (indentlevel+1) ""
|
subswithprefix = showsubs indent (prefix++leafname++":")
|
||||||
showsubs i p = concatMap (showAccountTreeWithBalances' matchedacctnames i p) subs
|
showsubs i p = concatMap (showAccountTreeWithBalances' matchednames i p) subs
|
||||||
showacct = showbal ++ " " ++ indent ++ prefix ++ leafname ++ "\n"
|
this = showbal ++ spaces ++ prefix ++ leafname ++ "\n"
|
||||||
showbal = printf "%20s" $ show bal
|
showbal = printf "%20s" $ show bal
|
||||||
indent = replicate (indentlevel * 2) ' '
|
spaces = " " ++ replicate (indent * 2) ' '
|
||||||
leafname = accountLeafName fullname
|
leafname = accountLeafName fullname
|
||||||
|
isboringparent = numsubs >= 1 && (bal == subbal || not matched)
|
||||||
numsubs = length subs
|
numsubs = length subs
|
||||||
subbal = abalance $ root $ head subs
|
subbal = abalance $ root $ head subs
|
||||||
matched = fullname `elem` matchedacctnames
|
matched = fullname `elem` matchednames
|
||||||
isboringparent = numsubs >= 1 && (bal == subbal || not matched)
|
|
||||||
|
|||||||
@ -15,24 +15,6 @@ import Ledger.Amount
|
|||||||
|
|
||||||
instance Show Entry where show = showEntry
|
instance Show Entry where show = showEntry
|
||||||
|
|
||||||
{-
|
|
||||||
Helpers for the register report. A register entry is displayed as two
|
|
||||||
or more lines like this:
|
|
||||||
|
|
||||||
@
|
|
||||||
date description account amount balance
|
|
||||||
DDDDDDDDDD dddddddddddddddddddd aaaaaaaaaaaaaaaaaaaaaa AAAAAAAAAAA AAAAAAAAAAAA
|
|
||||||
aaaaaaaaaaaaaaaaaaaaaa AAAAAAAAAAA AAAAAAAAAAAA
|
|
||||||
... ... ...
|
|
||||||
|
|
||||||
datewidth = 10
|
|
||||||
descwidth = 20
|
|
||||||
acctwidth = 22
|
|
||||||
amtwidth = 11
|
|
||||||
balwidth = 12
|
|
||||||
@
|
|
||||||
-}
|
|
||||||
|
|
||||||
showEntryDescription e =
|
showEntryDescription e =
|
||||||
(showDate $ edate e) ++ " " ++ (showDescription $ edescription e) ++ " "
|
(showDate $ edate e) ++ " " ++ (showDescription $ edescription e) ++ " "
|
||||||
showDate d = printf "%-10s" d
|
showDate d = printf "%-10s" d
|
||||||
|
|||||||
@ -12,27 +12,36 @@ import Options
|
|||||||
|
|
||||||
-- | Print a register report.
|
-- | Print a register report.
|
||||||
register :: [Opt] -> [String] -> Ledger -> IO ()
|
register :: [Opt] -> [String] -> Ledger -> IO ()
|
||||||
register opts args l = putStr $ showTransactionsWithBalances opts args l
|
register opts args l = putStr $ showRegisterReport opts args l
|
||||||
|
|
||||||
showTransactionsWithBalances :: [Opt] -> [String] -> Ledger -> String
|
{- |
|
||||||
showTransactionsWithBalances opts args l =
|
Generate the register report. Each ledger entry is displayed as two or
|
||||||
unlines $ showTransactionsWithBalances' ts nulltxn startingbalance
|
more lines like this:
|
||||||
|
|
||||||
|
@
|
||||||
|
date (10) description (20) account (22) amount (11) balance (12)
|
||||||
|
DDDDDDDDDD dddddddddddddddddddd aaaaaaaaaaaaaaaaaaaaaa AAAAAAAAAAA AAAAAAAAAAAA
|
||||||
|
aaaaaaaaaaaaaaaaaaaaaa AAAAAAAAAAA AAAAAAAAAAAA
|
||||||
|
... ... ...
|
||||||
|
@
|
||||||
|
-}
|
||||||
|
showRegisterReport :: [Opt] -> [String] -> Ledger -> String
|
||||||
|
showRegisterReport opts args l = showtxns ts nulltxn nullamt
|
||||||
where
|
where
|
||||||
ts = filter matchtxn $ ledgerTransactions l
|
ts = filter matchtxn $ ledgerTransactions l
|
||||||
matchtxn (Transaction _ _ desc acct _ _) = matchLedgerPatterns False apats acct
|
matchtxn Transaction{account=a} = matchLedgerPatterns False apats a
|
||||||
apats = fst $ parseAccountDescriptionArgs args
|
apats = fst $ parseAccountDescriptionArgs args
|
||||||
startingbalance = nullamt
|
|
||||||
showTransactionsWithBalances' :: [Transaction] -> Transaction -> Amount -> [String]
|
-- show transactions, one per line, keeping a running balance
|
||||||
showTransactionsWithBalances' [] _ _ = []
|
showtxns [] _ _ = ""
|
||||||
showTransactionsWithBalances' (t@Transaction{amount=a}:ts) tprev b =
|
showtxns (t@Transaction{amount=a}:ts) tprev bal =
|
||||||
(if isZeroAmount a then [] else this) ++ rest
|
(if isZeroAmount a then "" else this) ++ showtxns ts t bal'
|
||||||
where
|
where
|
||||||
b' = b + (amount t)
|
this = if t `issame` tprev
|
||||||
sameentry (Transaction {entryno=e1}) (Transaction {entryno=e2}) = e1 == e2
|
then showTransactionWithoutDescription t bal'
|
||||||
this = if sameentry t tprev
|
else showTransactionWithDescription t bal'
|
||||||
then [showTransactionWithoutDescription t b']
|
issame t1 t2 = entryno t1 == entryno t2
|
||||||
else [showTransactionWithDescription t b']
|
bal' = bal + amount t
|
||||||
rest = showTransactionsWithBalances' ts t b'
|
|
||||||
|
|
||||||
showTransactionWithDescription :: Transaction -> Amount -> String
|
showTransactionWithDescription :: Transaction -> Amount -> String
|
||||||
showTransactionWithDescription t b =
|
showTransactionWithDescription t b =
|
||||||
|
|||||||
2
Tests.hs
2
Tests.hs
@ -218,7 +218,7 @@ registercommandtests = TestList [
|
|||||||
"register does something" ~:
|
"register does something" ~:
|
||||||
do
|
do
|
||||||
l <- ledgerfromfile "sample.ledger"
|
l <- ledgerfromfile "sample.ledger"
|
||||||
assertnotequal "" $ showTransactionsWithBalances [] [] l
|
assertnotequal "" $ showRegisterReport [] [] l
|
||||||
]
|
]
|
||||||
|
|
||||||
-- | Assert a parsed thing equals some expected thing, or print a parse error.
|
-- | Assert a parsed thing equals some expected thing, or print a parse error.
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user