move parseLedgerAndDo back to hledger main

This commit is contained in:
Simon Michael 2008-10-03 02:25:18 +00:00
parent 5531918b8c
commit 9a2dc41d30
3 changed files with 25 additions and 26 deletions

View File

@ -136,8 +136,8 @@ printregister l = putStr $ showTransactionsWithBalances
nullamt{precision=lprecision l} nullamt{precision=lprecision l}
{-| {-|
This and the functions below help generate ledger-compatible balance This and the functions below generate ledger-compatible balance report
reports. Here's how it should work: output. Here's how it should work:
a sample account tree: a sample account tree:

View File

@ -1,4 +1,4 @@
module Options (parseOptions, parsePatternArgs, wildcard, Flag(..), usage, ledgerFilePath, parseLedgerAndDo) module Options (parseOptions, parsePatternArgs, wildcard, Flag(..), usage, ledgerFilePath)
where where
import System.Console.GetOpt import System.Console.GetOpt
import System.Directory import System.Directory
@ -92,12 +92,3 @@ regexFor ss = mkRegex $ "(" ++ (unwords $ intersperse "|" ss) ++ ")"
wildcard :: Regex wildcard :: Regex
wildcard = mkRegex ".*" wildcard = mkRegex ".*"
-- | 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.
parseLedgerAndDo :: [Flag] -> (Regex,Regex) -> (Ledger -> IO ()) -> IO ()
parseLedgerAndDo opts pats cmd = do
path <- ledgerFilePath opts
parsed <- parseLedgerFile path
case parsed of Left err -> parseError err
Right l -> cmd $ cacheLedger l pats

View File

@ -7,11 +7,11 @@ Released under GPL version 3 or later.
This is a minimal haskell clone of John Wiegley's ledger This is a minimal haskell clone of John Wiegley's ledger
<http://newartisans.com/software/ledger.html>. hledger generates <http://newartisans.com/software/ledger.html>. hledger generates
simple ledger-compatible register & balance reports from a standard simple ledger-compatible register & balance reports from a plain text
ledger file, and demonstrates a (naive) purely functional ledger file, and demonstrates a (naive) purely functional
implementation of ledger. implementation of ledger.
This module includes some helpers for querying your ledger in ghci. Examples: This module includes some helpers for working with your ledger in ghci. Examples:
> $ rm -f hledger.o > $ rm -f hledger.o
> $ ghci hledger.hs > $ ghci hledger.hs
@ -38,7 +38,7 @@ import qualified Data.Map as Map (lookup)
import Options import Options
import Tests (hunit, quickcheck) import Tests (hunit, quickcheck)
import Ledger import Ledger
import Ledger.Parse (parseLedgerFile) import Ledger.Parse (parseLedgerFile, parseError)
import Ledger.Utils hiding (test) import Ledger.Utils hiding (test)
@ -70,11 +70,10 @@ register :: Command
register opts pats = parseLedgerAndDo opts pats printregister register opts pats = parseLedgerAndDo opts pats printregister
balance :: Command balance :: Command
balance opts pats = do balance opts pats = parseLedgerAndDo opts pats printbalance
parseLedgerAndDo opts pats printbalance
where where
printbalance l = printbalance :: Ledger -> IO ()
putStr $ showLedgerAccounts l depth printbalance l = putStr $ showLedgerAccounts l depth
where where
showsubs = (ShowSubs `elem` opts) showsubs = (ShowSubs `elem` opts)
depth = case (pats, showsubs) of depth = case (pats, showsubs) of
@ -83,6 +82,15 @@ balance opts pats = do
((wildcard,_), False) -> 1 ((wildcard,_), False) -> 1
otherwise -> 9999 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.
parseLedgerAndDo :: [Flag] -> (Regex,Regex) -> (Ledger -> IO ()) -> IO ()
parseLedgerAndDo opts pats cmd = do
path <- ledgerFilePath opts
parsed <- parseLedgerFile path
case parsed of Left err -> parseError err
Right l -> cmd $ cacheLedger l pats
-- ghci helpers -- ghci helpers
-- | get a Ledger from the file your LEDGER environment variable points to -- | get a Ledger from the file your LEDGER environment variable points to