hledger/hledger.hs

98 lines
3.0 KiB
Haskell
Raw Normal View History

2007-05-02 11:37:08 +04:00
#!/usr/bin/env runhaskell
{-
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-07-02 20:43:14 +04:00
See Types.hs for a code overview.
-}
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
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-02-09 03:18:20 +03:00
main :: IO ()
2007-01-30 12:07:12 +03:00
main = do
(opts, (cmd:args)) <- getArgs >>= parseOptions
let (acctpats, descpats) = parseLedgerPatternArgs args
run cmd opts acctpats descpats
where run cmd opts acctpats descpats
2007-05-01 09:55:35 +04:00
| Help `elem` opts = putStr usage
| cmd `isPrefixOf` "register" = register opts acctpats descpats
| cmd `isPrefixOf` "balance" = balance opts acctpats descpats
| cmd `isPrefixOf` "print" = printcmd opts acctpats descpats
2007-07-04 07:41:15 +04:00
| cmd `isPrefixOf` "test" = test
2007-03-12 10:58:44 +03:00
| otherwise = putStr usage
2007-02-09 06:17:12 +03:00
-- commands
2007-07-04 07:41:15 +04:00
test :: IO ()
test = do
2007-07-03 22:20:45 +04:00
Tests.hunit
Tests.quickcheck
return ()
printcmd :: [Flag] -> [String] -> [String] -> IO ()
printcmd opts acctpats descpats = do
doWithLedger opts acctpats descpats printentries
where
printentries l = putStr $ showEntries $ setprecision $ entries $ rawledger l
where
setprecision = map (entrySetPrecision (lprecision l))
register :: [Flag] -> [String] -> [String] -> IO ()
register opts acctpats descpats = do
doWithLedger opts acctpats descpats printregister
where
2007-07-04 13:28:07 +04:00
printregister l =
putStr $ showTransactionsWithBalances
(sortBy (comparing date) (ledgerTransactionsMatching (acctpats, descpats) l))
nullamt{precision=lprecision l}
balance :: [Flag] -> [String] -> [String] -> IO ()
balance opts acctpats descpats = do
doWithLedger opts acctpats descpats printbalance
where
2007-07-04 13:28:07 +04:00
printbalance l =
2007-07-02 23:39:34 +04:00
putStr $ showLedgerAccounts l acctpats showsubs maxdepth
where
showsubs = (ShowSubs `elem` opts)
maxdepth = case (acctpats, showsubs) of
([],False) -> 1
otherwise -> 9999
2007-02-10 20:36:50 +03:00
-- utils
doWithLedger :: [Flag] -> [String] -> [String] -> (Ledger -> IO ()) -> IO ()
doWithLedger opts acctpats descpats cmd = do
ledgerFilePath opts >>= parseLedgerFile >>= doWithParsed acctpats descpats cmd
2007-02-10 20:36:50 +03:00
doWithParsed :: [String] -> [String] -> (Ledger -> IO ()) -> (Either ParseError LedgerFile) -> IO ()
doWithParsed acctpats descpats cmd parsed = do
case parsed of Left e -> parseError e
Right l -> cmd $ cacheLedger acctpats descpats l
{-
interactive testing:
*Main> p <- ledgerFilePath [File "./test.dat"] >>= parseLedgerFile
*Main> let r = either (\_ -> LedgerFile [] [] []) id p
*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
-}