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)
|
2008-09-28 07:43:59 +04:00
|
|
|
import qualified Data.Map as Map (lookup)
|
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
|
2008-05-27 00:43:11 +04:00
|
|
|
| cmd `isPrefixOf` "print" = doWithFilteredLedger opts pats printentries
|
|
|
|
| cmd `isPrefixOf` "register" = doWithFilteredLedger opts pats printregister
|
2007-07-11 09:46:20 +04:00
|
|
|
| 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 10:58:47 +04:00
|
|
|
doWithFilteredLedger :: [Flag] -> FilterPatterns -> (Ledger -> IO ()) -> IO ()
|
2007-07-11 09:46:20 +04:00
|
|
|
doWithFilteredLedger opts pats cmd = do
|
|
|
|
ledgerFilePath opts >>= parseLedgerFile >>= doWithParsed pats cmd
|
2007-07-09 22:54:41 +04:00
|
|
|
|
2007-07-11 10:58:47 +04:00
|
|
|
doWithParsed :: FilterPatterns -> (Ledger -> IO ()) -> (Either ParseError LedgerFile) -> IO ()
|
2007-07-11 09:46:20 +04:00
|
|
|
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 10:58:47 +04:00
|
|
|
type Command = [Flag] -> FilterPatterns -> 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 ()
|
|
|
|
|
2008-05-27 00:43:11 +04:00
|
|
|
printentries l = putStr $ showEntries $ setprecisions $ entries $ rawledger l
|
|
|
|
where setprecisions = map (entrySetPrecision (lprecision l))
|
|
|
|
|
|
|
|
printregister l = putStr $ showTransactionsWithBalances
|
|
|
|
(sortBy (comparing date) $ ledgerTransactions l)
|
|
|
|
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 10:58:47 +04:00
|
|
|
depth = case (pats, showsubs) of
|
|
|
|
((Nothing,_), False) -> 1
|
2007-07-09 22:54:41 +04:00
|
|
|
otherwise -> 9999
|
2007-07-03 21:25:16 +04:00
|
|
|
|
2008-09-28 07:43:59 +04:00
|
|
|
-- helpers for interacting in ghci
|
|
|
|
|
2008-10-01 04:29:58 +04:00
|
|
|
-- | returns a Ledger parsed from the file your LEDGER environment variable
|
2008-09-28 07:43:59 +04:00
|
|
|
-- points to or (WARNING:) an empty one if there was a problem.
|
|
|
|
myledger :: IO Ledger
|
|
|
|
myledger = do
|
|
|
|
parsed <- ledgerFilePath [] >>= parseLedgerFile
|
|
|
|
let ledgerfile = either (\_ -> LedgerFile [] [] [] "") id parsed
|
|
|
|
return $ cacheLedger (argpats [] []) ledgerfile
|
|
|
|
|
2008-10-01 04:29:58 +04:00
|
|
|
-- | similar, but accepts a file path
|
2008-09-28 07:43:59 +04:00
|
|
|
ledgerfromfile :: String -> IO Ledger
|
|
|
|
ledgerfromfile f = do
|
|
|
|
parsed <- ledgerFilePath [File f] >>= parseLedgerFile
|
|
|
|
let ledgerfile = either (\_ -> LedgerFile [] [] [] "") id parsed
|
|
|
|
return $ cacheLedger (argpats [] []) ledgerfile
|
|
|
|
|
|
|
|
accountnamed :: AccountName -> IO Account
|
|
|
|
accountnamed a = myledger >>= (return . fromMaybe nullacct . Map.lookup a . accounts)
|
2008-10-01 04:29:58 +04:00
|
|
|
|
2008-09-28 07:43:59 +04:00
|
|
|
|
|
|
|
--clearedBalanceToDate :: String -> Amount
|
|
|
|
|
2007-07-03 21:25:16 +04:00
|
|
|
{-
|
2008-09-28 07:43:59 +04:00
|
|
|
ghci examples:
|
|
|
|
|
|
|
|
$ ghci hledger.hs
|
|
|
|
GHCi, version 6.8.2: http://www.haskell.org/ghc/ :? for help
|
|
|
|
Loading package base ... linking ... done.
|
|
|
|
Ok, modules loaded: Utils, Main, Tests, Parse, Models, Ledger, LedgerFile, LedgerEntry, Amount, Currency, Types, LedgerTransaction, AccountName, Transaction, Account, TimeLog, Options.
|
|
|
|
Prelude Main> l <- myledger
|
|
|
|
<..snip..>
|
|
|
|
Ledger with 628 entries, 128 accounts
|
|
|
|
Prelude Main>
|
|
|
|
|
|
|
|
$ ghci hledger.hs
|
|
|
|
> l <- myledger
|
|
|
|
> putStr $ drawTree $ treemap show $ accountnametree l
|
|
|
|
> putStr $ showLedgerAccounts l 1
|
|
|
|
> printregister l
|
|
|
|
> import Types
|
|
|
|
> accounts l
|
|
|
|
> accountnamed "assets"
|
|
|
|
|
2007-07-03 21:25:16 +04:00
|
|
|
|
|
|
|
-}
|