hledger/hledger.hs

104 lines
2.8 KiB
Haskell
Raw Normal View History

{-
2007-03-10 03:06:48 +03:00
hledger - ledger-compatible money management tool (& haskell study)
GPLv3, (c) Simon Michael & contributors
inspired by John Wiegley's ledger at http://newartisans.com/ledger.html
2007-03-10 03:06:48 +03:00
modules/models are organized roughly like this; each layer can only
reference things below it:
hledger
Options
Tests
Parse
Models
TimeLog
TimeLogEntry
Account
Ledger
EntryTransaction
Entry
Transaction
AccountName
BasicTypes
Utils
-}
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 System.Environment (withArgs) -- for testing in old hugs
2007-02-10 20:36:50 +03:00
import Test.HUnit (runTestTT)
2007-02-10 22:16:56 +03:00
import Test.QuickCheck (quickCheck)
2007-02-16 14:51:30 +03:00
import Text.ParserCombinators.Parsec (ParseError)
import Debug.Trace
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-02-16 14:51:30 +03:00
import Utils
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
run cmd opts args
where run cmd opts args
| cmd `isPrefixOf` "register" = register opts args
| cmd `isPrefixOf` "balance" = balance opts args
| cmd `isPrefixOf` "test" = test
| otherwise = putStr showusage
2007-02-09 06:17:12 +03:00
-- commands
register :: [Flag] -> [String] -> IO ()
register opts args = do
doWithLedger opts $ printRegister
where
printRegister ledger =
putStr $ showTransactionsWithBalances
(ledgerTransactionsMatching (acctpats,descpats) ledger)
0
where (acctpats,descpats) = parseLedgerPatternArgs args
balance :: [Flag] -> [String] -> IO ()
balance opts args = do
doWithLedger opts $ printBalance
where
printBalance ledger =
putStr $ showLedgerAccounts ledger acctpats showsubs maxdepth
where
(acctpats,_) = parseLedgerPatternArgs args
showsubs = (ShowSubs `elem` opts)
maxdepth = case (acctpats, showsubs) of
([],False) -> 1
otherwise -> 9999
test :: IO ()
2007-02-10 20:36:50 +03:00
test = do
hcounts <- runTestTT tests
qcounts <- mapM quickCheck props
2007-02-10 20:36:50 +03:00
return ()
where showHunitCounts c =
reverse $ tail $ reverse ("passed " ++ (unwords $ drop 5 $ words (show c)))
2007-02-10 20:36:50 +03:00
-- utils
doWithLedger :: [Flag] -> (Ledger -> IO ()) -> IO ()
doWithLedger opts cmd = do
ledgerFilePath opts >>= parseLedgerFile >>= doWithParsed cmd
2007-02-10 20:36:50 +03:00
doWithParsed :: Show a => (a -> IO ()) -> (Either ParseError a) -> IO ()
doWithParsed action parsed = do
case parsed of Left e -> parseError e
Right l -> action l
2007-03-11 03:56:03 +03:00
-- interactive testing:
--
-- p <- ledgerFilePath [] >>= parseLedgerFile
2007-03-11 03:56:03 +03:00
-- let l = either (\_ -> Ledger [] [] []) id p
-- let ant = ledgerAccountNameTree l
-- let at = ledgerAccountTreeMatching l [] True 999
-- putStr $ drawTree $ treemap show $ ledgerAccountTreeMatching l ["a"] False 999