2007-05-02 11:37:08 +04:00
|
|
|
#!/usr/bin/env runhaskell
|
2008-10-01 11:04:47 +04:00
|
|
|
{-|
|
|
|
|
hledger - a ledger-compatible text-based accounting tool.
|
2007-02-18 21:12:02 +03:00
|
|
|
|
2008-10-01 12:44:23 +04:00
|
|
|
Copyright (c) 2007-2008 Simon Michael <simon@joyful.com>
|
|
|
|
Released under GPL version 3 or later.
|
2008-10-01 11:04:47 +04:00
|
|
|
|
2008-10-01 12:44:23 +04:00
|
|
|
This is a minimal haskell clone of John Wiegley's ledger
|
2008-10-01 13:33:05 +04:00
|
|
|
<http://newartisans.com/software/ledger.html>. hledger generates
|
2008-10-03 06:25:18 +04:00
|
|
|
simple ledger-compatible register & balance reports from a plain text
|
2008-10-01 13:33:05 +04:00
|
|
|
ledger file, and demonstrates a (naive) purely functional
|
|
|
|
implementation of ledger.
|
2008-10-01 11:04:47 +04:00
|
|
|
|
2008-10-03 06:25:18 +04:00
|
|
|
This module includes some helpers for working with your ledger in ghci. Examples:
|
2008-10-03 03:33:02 +04:00
|
|
|
|
|
|
|
> $ rm -f hledger.o
|
|
|
|
> $ ghci hledger.hs
|
2008-10-03 07:15:16 +04:00
|
|
|
> *Main> l <- ledger
|
2008-10-03 03:33:02 +04:00
|
|
|
> Ledger with 696 entries, 132 accounts
|
|
|
|
> *Main> putStr $ drawTree $ treemap show $ accountnametree l
|
|
|
|
> ...
|
2008-10-03 11:47:36 +04:00
|
|
|
> *Main> putStr $ showLedgerAccountBalances l 1
|
2008-10-03 03:33:02 +04:00
|
|
|
> ...
|
|
|
|
> *Main> printregister l
|
|
|
|
> ...
|
|
|
|
> *Main> accounts l
|
|
|
|
> ...
|
|
|
|
> *Main> accountnamed "expenses:food:groceries"
|
|
|
|
> Account expenses:food:groceries with 60 transactions
|
|
|
|
|
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
|
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
|
2008-10-03 01:47:11 +04:00
|
|
|
import Tests (hunit, quickcheck)
|
2008-10-08 22:02:34 +04:00
|
|
|
import Ledger.Parse (parseLedgerFile, printParseError)
|
2008-10-03 04:05:16 +04:00
|
|
|
import Ledger.Utils hiding (test)
|
2008-10-03 07:15:16 +04:00
|
|
|
import Ledger hiding (rawledger)
|
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
|
2008-10-08 21:00:22 +04:00
|
|
|
(opts, cmd, args) <- parseArguments
|
|
|
|
run cmd opts args
|
|
|
|
where run cmd opts args
|
2008-10-01 16:29:51 +04:00
|
|
|
| Help `elem` opts = putStr usage
|
2008-10-10 05:36:21 +04:00
|
|
|
| Version `elem` opts = putStr version
|
2008-10-08 21:00:22 +04:00
|
|
|
| cmd `isPrefixOf` "selftest" = selftest opts args
|
|
|
|
| cmd `isPrefixOf` "print" = print_ opts args
|
|
|
|
| cmd `isPrefixOf` "register" = register opts args
|
|
|
|
| cmd `isPrefixOf` "balance" = balance opts args
|
2008-10-01 16:29:51 +04:00
|
|
|
| otherwise = putStr usage
|
|
|
|
|
2008-10-08 21:24:59 +04:00
|
|
|
type Command = [Opt] -> [String] -> IO ()
|
2007-02-09 06:17:12 +03:00
|
|
|
|
2008-10-01 16:29:51 +04:00
|
|
|
selftest :: Command
|
2008-10-08 21:00:22 +04:00
|
|
|
selftest _ _ = do
|
2008-10-03 01:47:11 +04:00
|
|
|
hunit
|
|
|
|
quickcheck
|
2007-07-03 22:20:45 +04:00
|
|
|
return ()
|
|
|
|
|
2008-10-01 16:29:51 +04:00
|
|
|
print_ :: Command
|
2008-10-08 21:00:22 +04:00
|
|
|
print_ opts args = parseLedgerAndDo opts args printentries
|
2008-10-01 16:29:51 +04:00
|
|
|
|
|
|
|
register :: Command
|
2008-10-08 21:00:22 +04:00
|
|
|
register opts args = parseLedgerAndDo opts args printregister
|
2008-10-01 16:29:51 +04:00
|
|
|
|
2007-07-09 22:54:41 +04:00
|
|
|
balance :: Command
|
2008-10-08 21:00:22 +04:00
|
|
|
balance opts args = parseLedgerAndDo opts args printbalance
|
2007-03-12 10:40:33 +03:00
|
|
|
where
|
2008-10-03 06:25:18 +04:00
|
|
|
printbalance :: Ledger -> IO ()
|
2008-10-03 11:47:36 +04:00
|
|
|
printbalance l = putStr $ showLedgerAccountBalances l depth
|
2008-10-03 06:25:18 +04:00
|
|
|
where
|
|
|
|
showsubs = (ShowSubs `elem` opts)
|
2008-10-09 17:02:26 +04:00
|
|
|
pats@(acctpats,descpats) = parseAccountDescriptionArgs args
|
2008-10-03 06:25:18 +04:00
|
|
|
depth = case (pats, showsubs) of
|
2008-10-03 15:52:07 +04:00
|
|
|
-- when there is no -s or pattern args, show with depth 1
|
|
|
|
(([],[]), False) -> 1
|
2008-10-03 06:25:18 +04:00
|
|
|
otherwise -> 9999
|
|
|
|
|
|
|
|
-- | parse the user's specified ledger file and do some action with it
|
|
|
|
-- (or report a parse error). This function makes the whole thing go.
|
2008-10-08 21:24:59 +04:00
|
|
|
parseLedgerAndDo :: [Opt] -> [String] -> (Ledger -> IO ()) -> IO ()
|
2008-10-08 21:48:11 +04:00
|
|
|
parseLedgerAndDo opts args cmd =
|
2008-10-08 22:02:34 +04:00
|
|
|
ledgerFilePathFromOpts opts >>= parseLedgerFile >>= either printParseError runthecommand
|
2008-10-08 21:48:11 +04:00
|
|
|
where
|
2008-10-09 17:02:26 +04:00
|
|
|
runthecommand = cmd . cacheLedger aregex . filterLedgerEntries begin end aregex dregex
|
2008-10-08 21:48:11 +04:00
|
|
|
begin = beginDateFromOpts opts
|
|
|
|
end = endDateFromOpts opts
|
2008-10-08 22:02:34 +04:00
|
|
|
aregex = regexFor acctpats
|
|
|
|
dregex = regexFor descpats
|
|
|
|
(acctpats,descpats) = parseAccountDescriptionArgs args
|
2007-07-03 21:25:16 +04:00
|
|
|
|
2008-10-03 03:33:02 +04:00
|
|
|
-- ghci helpers
|
2008-09-28 07:43:59 +04:00
|
|
|
|
2008-10-03 07:15:16 +04:00
|
|
|
-- | get a RawLedger from the file your LEDGER environment variable points to
|
2008-10-03 03:33:02 +04:00
|
|
|
-- or (WARNING) an empty one if there was a problem.
|
2008-10-03 07:15:16 +04:00
|
|
|
rawledger :: IO RawLedger
|
|
|
|
rawledger = do
|
2008-10-08 21:00:22 +04:00
|
|
|
parsed <- ledgerFilePathFromOpts [] >>= parseLedgerFile
|
2008-10-03 07:15:16 +04:00
|
|
|
return $ either (\_ -> RawLedger [] [] [] "") id parsed
|
|
|
|
|
|
|
|
-- | as above, and convert it to a cached Ledger
|
|
|
|
ledger :: IO Ledger
|
|
|
|
ledger = do
|
|
|
|
l <- rawledger
|
2008-10-09 17:02:26 +04:00
|
|
|
return $ cacheLedger wildcard $ filterLedgerEntries "" "" wildcard wildcard l
|
2008-10-01 22:53:43 +04:00
|
|
|
|
2008-10-03 03:33:02 +04:00
|
|
|
-- | get a Ledger from the given file path
|
2008-10-03 07:15:16 +04:00
|
|
|
rawledgerfromfile :: String -> IO RawLedger
|
|
|
|
rawledgerfromfile f = do
|
2008-10-08 21:00:22 +04:00
|
|
|
parsed <- ledgerFilePathFromOpts [File f] >>= parseLedgerFile
|
2008-10-03 07:15:16 +04:00
|
|
|
return $ either (\_ -> RawLedger [] [] [] "") id parsed
|
2008-10-01 22:53:43 +04:00
|
|
|
|
2008-10-03 03:33:02 +04:00
|
|
|
-- | get a named account from your ledger file
|
2008-10-01 22:53:43 +04:00
|
|
|
accountnamed :: AccountName -> IO Account
|
2008-10-03 07:15:16 +04:00
|
|
|
accountnamed a = ledger >>= (return . fromMaybe nullacct . Map.lookup a . accounts)
|
2008-10-03 03:33:02 +04:00
|
|
|
|