preliminary --display/-d support (-d "d>DATE", sufficient for reconciling)

This commit is contained in:
Simon Michael 2008-11-24 21:51:31 +00:00
parent 5f47e4d259
commit 0582046648
4 changed files with 45 additions and 4 deletions

View File

@ -538,3 +538,16 @@ y = do
smartparsedate :: String -> Date
smartparsedate s = parsedate $ printf "%04s/%02s/%02s" y m d
where (y,m,d) = fromparse $ parsewith smartdate s
-- | Parse a --display expression of the form "d>DATE"
type TransactionMatcher = Transaction -> Bool
datedisplayexpr :: Parser TransactionMatcher
datedisplayexpr = do
char 'd'
char '>'
(y,m,d) <- smartdate
let edate = parsedate $ printf "%04s/%02s/%02s" y m d
return $ \(Transaction{date=tdate}) -> tdate > edate

View File

@ -7,6 +7,8 @@ import Text.Printf
import Ledger.AccountName (negativepatternchar)
import Ledger.Parse (smartparsedate)
import Ledger.Dates
import Ledger.Utils
usagehdr = "Usage: hledger [OPTS] COMMAND [ACCTPATTERNS] [-- DESCPATTERNS]\n\nOptions"++warning++":"
warning = if negativepatternchar=='-' then " (must appear before command)" else " (can appear anywhere)"
@ -35,6 +37,7 @@ options = [
Option ['C'] ["cleared"] (NoArg Cleared) "report only on cleared entries",
Option ['B'] ["cost","basis"] (NoArg CostBasis) "report cost basis of commodities",
Option [] ["depth"] (ReqArg Depth "N") "balance report: maximum account depth to show",
Option ['d'] ["display"] (ReqArg Display "EXPR") "display only transactions matching EXPR\n(where EXPR is 'd>Y/M/D')",
Option ['E'] ["empty"] (NoArg Empty) "balance report: show accounts with zero balance",
Option ['R'] ["real"] (NoArg Real) "report only on real (non-virtual) transactions",
Option ['n'] ["collapse"] (NoArg Collapse) "balance report: no grand total",
@ -55,6 +58,7 @@ data Opt =
Cleared |
CostBasis |
Depth String |
Display String |
Empty |
Real |
Collapse |
@ -135,6 +139,17 @@ depthFromOpts opts =
getdepth (Depth s) = [s]
getdepth _ = []
-- | Get the value of the display option, if any.
displayFromOpts :: [Opt] -> Maybe String
displayFromOpts opts =
case displayopts of
(s:_) -> Just s
_ -> Nothing
where
displayopts = concatMap getdisplay opts
getdisplay (Display s) = [s]
getdisplay _ = []
-- | Gather any ledger-style account/description pattern arguments into
-- two lists. These are 0 or more account patterns optionally followed by
-- -- and 0 or more description patterns.

View File

@ -28,14 +28,17 @@ DDDDDDDDDD dddddddddddddddddddd aaaaaaaaaaaaaaaaaaaaaa AAAAAAAAAAA AAAAAAAAAAAA
showRegisterReport :: [Opt] -> [String] -> Ledger -> String
showRegisterReport opts args l = showtxns ts nulltxn nullmixedamt
where
ts = filter matchtxn $ ledgerTransactions l
matchtxn Transaction{account=a} = matchpats apats a
ts = filter matchapats $ ledgerTransactions l
matchapats t = matchpats apats $ account t
apats = fst $ parseAccountDescriptionArgs args
matchdisplayopt Nothing t = True
matchdisplayopt (Just e) t = (fromparse $ parsewith datedisplayexpr e) t
dopt = displayFromOpts opts
-- show transactions, one per line, with a running balance
-- show display-filtered transactions, one per line, with a running balance
showtxns [] _ _ = ""
showtxns (t@Transaction{amount=a}:ts) tprev bal =
(if isZeroMixedAmount a then "" else this) ++ showtxns ts t bal'
(if (isZeroMixedAmount a || (not $ matchdisplayopt dopt t)) then "" else this) ++ showtxns ts t bal'
where
this = showtxn (t `issame` tprev) t bal'
issame t1 t2 = entryno t1 == entryno t2
@ -51,3 +54,4 @@ showRegisterReport opts args l = showtxns ts nulltxn nullmixedamt
txn = showRawTransaction $ RawTransaction a amt "" tt
bal = padleft 12 (showMixedAmountOrZero b)
Transaction{date=da,description=de,account=a,amount=amt,ttype=tt} = t

View File

@ -315,6 +315,15 @@ registercommand_tests = TestList [
"2007/01/01 eat & shop assets:cash $-2 $-2\n" ++
"")
$ showRegisterReport [] ["cash"] l
,
"register report with display expression" ~:
do
l <- ledgerfromfile [] "sample.ledger"
assertequal (
"2008/01/01 pay off liabilities:debts $1 $1\n" ++
" assets:checking $-1 0\n" ++
"")
$ showRegisterReport [Display "d>2007/12"] [] l
]
------------------------------------------------------------------------------