2007-05-02 11:37:08 +04:00
|
|
|
#!/usr/bin/env runhaskell
|
2007-02-18 21:12:02 +03:00
|
|
|
{-
|
2007-03-10 03:06:48 +03:00
|
|
|
hledger - ledger-compatible money management tool (& haskell study)
|
|
|
|
GPLv3, (c) Simon Michael & contributors
|
2007-05-01 11:15:53 +04:00
|
|
|
A port of John Wiegley's ledger at http://newartisans.com/ledger.html
|
2007-02-18 21:12:02 +03:00
|
|
|
|
2007-07-02 20:43:14 +04:00
|
|
|
See Types.hs for a code overview.
|
2007-02-18 21:12:02 +03:00
|
|
|
-}
|
2007-01-28 13:30:24 +03:00
|
|
|
|
2007-02-16 14:51:30 +03:00
|
|
|
module Main
|
2007-02-10 22:16:56 +03:00
|
|
|
where
|
2007-02-11 02:24:33 +03:00
|
|
|
import System
|
2007-02-16 14:51:30 +03:00
|
|
|
import Text.ParserCombinators.Parsec (ParseError)
|
2007-01-28 00:51:59 +03:00
|
|
|
|
2007-01-30 12:07:12 +03:00
|
|
|
import Options
|
2007-02-11 02:10:04 +03:00
|
|
|
import Models
|
2007-02-09 04:23:12 +03:00
|
|
|
import Parse
|
|
|
|
import Tests
|
2007-07-04 07:41:15 +04:00
|
|
|
import Utils hiding (test)
|
2007-02-09 03:18:20 +03:00
|
|
|
|
2007-03-12 10:40:33 +03:00
|
|
|
|
2007-02-09 03:18:20 +03:00
|
|
|
main :: IO ()
|
2007-01-30 12:07:12 +03:00
|
|
|
main = do
|
2007-03-12 10:40:33 +03:00
|
|
|
(opts, (cmd:args)) <- getArgs >>= parseOptions
|
2007-07-11 09:46:20 +04:00
|
|
|
let pats = parsePatternArgs args
|
|
|
|
run cmd opts pats
|
|
|
|
where run cmd opts pats
|
2007-05-01 09:55:35 +04:00
|
|
|
| Help `elem` opts = putStr usage
|
2007-07-11 09:46:20 +04:00
|
|
|
| cmd `isPrefixOf` "test" = test opts pats
|
|
|
|
| cmd `isPrefixOf` "print" = printcmd opts pats
|
|
|
|
| cmd `isPrefixOf` "register" = register opts pats
|
|
|
|
| cmd `isPrefixOf` "balance" = balance opts pats
|
2007-03-12 10:58:44 +03:00
|
|
|
| otherwise = putStr usage
|
2007-02-09 06:17:12 +03:00
|
|
|
|
2007-07-11 09:46:20 +04:00
|
|
|
doWithFilteredLedger :: [Flag] -> ([Regex],[Regex]) -> (Ledger -> IO ()) -> IO ()
|
|
|
|
doWithFilteredLedger opts pats cmd = do
|
|
|
|
ledgerFilePath opts >>= parseLedgerFile >>= doWithParsed pats cmd
|
2007-07-09 22:54:41 +04:00
|
|
|
|
2007-07-11 09:46:20 +04:00
|
|
|
doWithParsed :: ([Regex],[Regex]) -> (Ledger -> IO ()) -> (Either ParseError LedgerFile) -> IO ()
|
|
|
|
doWithParsed pats cmd parsed = do
|
2007-07-09 22:54:41 +04:00
|
|
|
case parsed of Left e -> parseError e
|
2007-07-11 09:46:20 +04:00
|
|
|
Right l -> cmd $ cacheLedger pats l
|
2007-07-09 22:54:41 +04:00
|
|
|
|
2007-07-11 09:46:20 +04:00
|
|
|
type Command = [Flag] -> ([Regex],[Regex]) -> IO ()
|
2007-02-09 06:17:12 +03:00
|
|
|
|
2007-07-09 22:54:41 +04:00
|
|
|
test :: Command
|
2007-07-11 09:46:20 +04:00
|
|
|
test opts pats = do
|
2007-07-03 22:20:45 +04:00
|
|
|
Tests.hunit
|
|
|
|
Tests.quickcheck
|
|
|
|
return ()
|
|
|
|
|
2007-07-09 22:54:41 +04:00
|
|
|
printcmd :: Command
|
2007-07-11 09:46:20 +04:00
|
|
|
printcmd opts pats = do
|
|
|
|
doWithFilteredLedger opts pats printentries
|
2007-07-09 21:38:01 +04:00
|
|
|
where
|
|
|
|
printentries l = putStr $ showEntries $ setprecision $ entries $ rawledger l
|
|
|
|
where
|
|
|
|
setprecision = map (entrySetPrecision (lprecision l))
|
|
|
|
|
2007-07-09 22:54:41 +04:00
|
|
|
register :: Command
|
2007-07-11 09:46:20 +04:00
|
|
|
register opts pats = do
|
|
|
|
doWithFilteredLedger opts pats printregister
|
2007-03-12 10:40:33 +03:00
|
|
|
where
|
2007-07-04 13:28:07 +04:00
|
|
|
printregister l =
|
2007-03-12 10:40:33 +03:00
|
|
|
putStr $ showTransactionsWithBalances
|
2007-07-09 22:54:41 +04:00
|
|
|
(sortBy (comparing date) $ ledgerTransactions l)
|
2007-07-04 05:38:56 +04:00
|
|
|
nullamt{precision=lprecision l}
|
2007-03-12 10:40:33 +03:00
|
|
|
|
2007-07-09 22:54:41 +04:00
|
|
|
balance :: Command
|
2007-07-11 09:46:20 +04:00
|
|
|
balance opts pats = do
|
|
|
|
doWithFilteredLedger opts pats printbalance
|
2007-03-12 10:40:33 +03:00
|
|
|
where
|
2007-07-04 13:28:07 +04:00
|
|
|
printbalance l =
|
2007-07-09 22:54:41 +04:00
|
|
|
putStr $ showLedgerAccounts l depth
|
2007-03-12 10:40:33 +03:00
|
|
|
where
|
|
|
|
showsubs = (ShowSubs `elem` opts)
|
2007-07-11 09:46:20 +04:00
|
|
|
(acctpats,_) = pats
|
2007-07-09 22:54:41 +04:00
|
|
|
depth = case (acctpats, showsubs) of
|
|
|
|
([],False) -> 1
|
|
|
|
otherwise -> 9999
|
2007-07-03 21:25:16 +04:00
|
|
|
|
|
|
|
{-
|
|
|
|
interactive testing:
|
|
|
|
|
|
|
|
*Main> p <- ledgerFilePath [File "./test.dat"] >>= parseLedgerFile
|
2007-07-04 13:51:37 +04:00
|
|
|
*Main> let r = either (\_ -> LedgerFile [] [] []) id p
|
2007-07-03 21:25:16 +04:00
|
|
|
*Main> let l = cacheLedger r
|
|
|
|
*Main> let ant = accountnametree l
|
|
|
|
*Main> let at = accounts l
|
|
|
|
*Main> putStr $ drawTree $ treemap show $ ant
|
|
|
|
*Main> putStr $ showLedgerAccounts l [] False 1
|
|
|
|
*Main> :m +Tests
|
|
|
|
*Main Tests> l7
|
|
|
|
|
|
|
|
-}
|