nicer ghci/repl usage, update docs

This commit is contained in:
Simon Michael 2008-10-12 09:17:21 +00:00
parent a9f021eebe
commit 80ef214f79
7 changed files with 50 additions and 41 deletions

View File

@ -114,12 +114,12 @@ import Utils
-- | Print a balance report.
printbalance :: [Opt] -> [String] -> Ledger -> IO ()
printbalance opts args l = putStr $ balancereport opts args l
balance :: [Opt] -> [String] -> Ledger -> IO ()
balance opts args l = putStr $ showBalanceReport opts args l
-- | Generate balance report output for a ledger, based on options.
balancereport :: [Opt] -> [String] -> Ledger -> String
balancereport opts args l = acctsstr ++ totalstr
showBalanceReport :: [Opt] -> [String] -> Ledger -> String
showBalanceReport opts args l = acctsstr ++ totalstr
where
acctsstr = concatMap (showAccountTreeWithBalances acctnamestoshow) $ subs treetoshow
totalstr = if isZeroAmount total

View File

@ -87,9 +87,6 @@ showEntry e =
showaccountname s = printf "%-34s" s
showcomment s = if (length s) > 0 then " ; "++s else ""
showEntries :: [Entry] -> String
showEntries = concatMap showEntry
entrySetPrecision :: Int -> Entry -> Entry
entrySetPrecision p (Entry d s c desc comm ts prec) =
Entry d s c desc comm (map (ledgerTransactionSetPrecision p) ts) prec

View File

@ -14,6 +14,9 @@ printcommandtests = TestList [
]
-- | Print ledger entries in standard format.
printentries :: [Opt] -> [String] -> Ledger -> IO ()
printentries opts args l = putStr $ showEntries $ setprecisions $ entries $ rawledger l
print' :: [Opt] -> [String] -> Ledger -> IO ()
print' opts args l = putStr $ showEntries opts args l
showEntries :: [Opt] -> [String] -> Ledger -> String
showEntries opts args l = concatMap showEntry $ setprecisions $ entries $ rawledger l
where setprecisions = map (entrySetPrecision (lprecision l))

View File

@ -14,8 +14,8 @@ registercommandtests = TestList [
]
-- | Print a register report.
printregister :: [Opt] -> [String] -> Ledger -> IO ()
printregister opts args l = putStr $ showTransactionsWithBalances opts args l
register :: [Opt] -> [String] -> Ledger -> IO ()
register opts args l = putStr $ showTransactionsWithBalances opts args l
showTransactionsWithBalances :: [Opt] -> [String] -> Ledger -> String
showTransactionsWithBalances opts args l =

View File

@ -102,7 +102,7 @@ balancecommandtests =
\ $-2 income\n\
\ $1 liabilities\n\
\" --"
(balancereport [] [] l)
(showBalanceReport [] [] l)
,
"balance report with showsubs" ~: do
@ -119,7 +119,7 @@ balancecommandtests =
\ $-1 salary\n\
\ $1 liabilities:debts\n\
\" --"
(balancereport [ShowSubs] [] l)
(showBalanceReport [ShowSubs] [] l)
,
"balance report with account pattern o" ~: do
@ -130,7 +130,7 @@ balancecommandtests =
\--------------------\n\
\ $-1\n\
\" --"
(balancereport [] ["o"] l)
(showBalanceReport [] ["o"] l)
,
"balance report with account pattern o and showsubs" ~: do
@ -143,7 +143,7 @@ balancecommandtests =
\--------------------\n\
\ $-1\n\
\" --"
(balancereport [ShowSubs] ["o"] l)
(showBalanceReport [ShowSubs] ["o"] l)
,
"balance report with account pattern a" ~: do
@ -157,7 +157,7 @@ balancecommandtests =
\--------------------\n\
\ $-1\n\
\" --"
(balancereport [] ["a"] l)
(showBalanceReport [] ["a"] l)
,
"balance report with account pattern e" ~: do
@ -169,7 +169,7 @@ balancecommandtests =
\ $-2 income\n\
\ $1 liabilities:debts\n\
\" --"
(balancereport [] ["e"] l)
(showBalanceReport [] ["e"] l)
,
"balance report with unmatched parent of two matched subaccounts" ~: do
@ -180,7 +180,7 @@ balancecommandtests =
\--------------------\n\
\ $-1\n\
\" --"
(balancereport [] ["cash","saving"] l)
(showBalanceReport [] ["cash","saving"] l)
]
-- | Assert a parsed thing equals some expected thing, or print a parse error.

View File

@ -1,22 +1,6 @@
{-|
Utilities for top-level modules. See also "Ledger.Utils".
There are some helpers here for working with your ledger in ghci. Examples:
> $ rm -f *.o Ledger/*.o
> $ ghci hledger.hs
> *Main> l <- myledger
> Ledger with 696 entries, 132 accounts:
> ...
> *Main> printbalance [] [] l
> ...
> *Main> printregister [] [] l
> ...
> *Main> accounts l
> ...
> *Main> myaccount "expenses:food:groceries"
> Account expenses:food:groceries with 60 transactions
Utilities for top-level modules and/or ghci. See also "Ledger.Utils".
-}

View File

@ -10,18 +10,43 @@ This is a minimal haskell clone of John Wiegley's ledger
simple ledger-compatible register & balance reports from a plain text
ledger file, and demonstrates a functional implementation of ledger.
You can use the command line:
> $ hledger --help
or ghci:
> $ ghci hledger
> > l <- ledgerfromfile "sample.ledger"
> > balance [] [] l
> $-1 assets
> $2 expenses
> $-2 income
> $1 liabilities:debts
> > register [] ["income","expenses"] l
> 2007/01/01 income income:salary $-1 $-1
> 2007/01/01 gift income:gifts $-1 $-2
> 2007/01/01 eat & shop expenses:food $1 $-1
> expenses:supplies $1 0
-}
module Main
module Main (
module Utils,
module Options,
module BalanceCommand,
module PrintCommand,
module RegisterCommand,
)
where
import qualified Data.Map as Map (lookup)
import Options
import Ledger
import Utils
import Options
import BalanceCommand
import PrintCommand
import RegisterCommand
import Tests
import Utils
main :: IO ()
@ -32,9 +57,9 @@ main = do
run cmd opts args
| Help `elem` opts = putStr usage
| Version `elem` opts = putStr version
| cmd `isPrefixOf` "balance" = parseLedgerAndDo opts args printbalance
| cmd `isPrefixOf` "print" = parseLedgerAndDo opts args printentries
| cmd `isPrefixOf` "register" = parseLedgerAndDo opts args printregister
| cmd `isPrefixOf` "balance" = parseLedgerAndDo opts args balance
| cmd `isPrefixOf` "print" = parseLedgerAndDo opts args print'
| cmd `isPrefixOf` "register" = parseLedgerAndDo opts args register
| cmd `isPrefixOf` "test" = runtests >> return ()
| otherwise = putStr usage