hledger/hledger.hs

124 lines
3.8 KiB
Haskell
Raw Normal View History

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.
2008-10-01 11:04:47 +04:00
Copyright 2007-, Simon Michael & contributors, released under GPLv3 or
later.
This is a basic haskell clone of John Wiegley's ledger
<http://newartisans.com/software/ledger.html>. hledger does basic
register & balance reports, and demonstrates a (naive) purely
functional implementation of ledger.
See the "Types" module 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)
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-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 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
| 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
| 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
doWithFilteredLedger :: [Flag] -> FilterPatterns -> (Ledger -> IO ()) -> IO ()
doWithFilteredLedger opts pats cmd = do
ledgerFilePath opts >>= parseLedgerFile >>= doWithParsed pats cmd
2007-07-09 22:54:41 +04:00
doWithParsed :: FilterPatterns -> (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
Right l -> cmd $ cacheLedger pats l
2007-07-09 22:54:41 +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
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-07-09 22:54:41 +04:00
balance :: Command
balance opts pats = do
doWithFilteredLedger opts pats printbalance
where
2007-07-04 13:28:07 +04:00
printbalance l =
2007-07-09 22:54:41 +04:00
putStr $ showLedgerAccounts l depth
where
showsubs = (ShowSubs `elem` opts)
depth = case (pats, showsubs) of
((Nothing,_), False) -> 1
2007-07-09 22:54:41 +04:00
otherwise -> 9999
2008-09-28 07:43:59 +04:00
-- helpers for interacting in ghci
2008-10-01 11:04:47 +04:00
-- | return 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 11:04:47 +04:00
-- | return a Ledger parsed from the given 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
{-
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"
-}