nicer ghci/repl usage, update docs
This commit is contained in:
parent
a9f021eebe
commit
80ef214f79
@ -114,12 +114,12 @@ import Utils
|
|||||||
|
|
||||||
|
|
||||||
-- | Print a balance report.
|
-- | Print a balance report.
|
||||||
printbalance :: [Opt] -> [String] -> Ledger -> IO ()
|
balance :: [Opt] -> [String] -> Ledger -> IO ()
|
||||||
printbalance opts args l = putStr $ balancereport opts args l
|
balance opts args l = putStr $ showBalanceReport opts args l
|
||||||
|
|
||||||
-- | Generate balance report output for a ledger, based on options.
|
-- | Generate balance report output for a ledger, based on options.
|
||||||
balancereport :: [Opt] -> [String] -> Ledger -> String
|
showBalanceReport :: [Opt] -> [String] -> Ledger -> String
|
||||||
balancereport opts args l = acctsstr ++ totalstr
|
showBalanceReport opts args l = acctsstr ++ totalstr
|
||||||
where
|
where
|
||||||
acctsstr = concatMap (showAccountTreeWithBalances acctnamestoshow) $ subs treetoshow
|
acctsstr = concatMap (showAccountTreeWithBalances acctnamestoshow) $ subs treetoshow
|
||||||
totalstr = if isZeroAmount total
|
totalstr = if isZeroAmount total
|
||||||
|
|||||||
@ -87,9 +87,6 @@ showEntry e =
|
|||||||
showaccountname s = printf "%-34s" s
|
showaccountname s = printf "%-34s" s
|
||||||
showcomment s = if (length s) > 0 then " ; "++s else ""
|
showcomment s = if (length s) > 0 then " ; "++s else ""
|
||||||
|
|
||||||
showEntries :: [Entry] -> String
|
|
||||||
showEntries = concatMap showEntry
|
|
||||||
|
|
||||||
entrySetPrecision :: Int -> Entry -> Entry
|
entrySetPrecision :: Int -> Entry -> Entry
|
||||||
entrySetPrecision p (Entry d s c desc comm ts prec) =
|
entrySetPrecision p (Entry d s c desc comm ts prec) =
|
||||||
Entry d s c desc comm (map (ledgerTransactionSetPrecision p) ts) prec
|
Entry d s c desc comm (map (ledgerTransactionSetPrecision p) ts) prec
|
||||||
|
|||||||
@ -14,6 +14,9 @@ printcommandtests = TestList [
|
|||||||
]
|
]
|
||||||
|
|
||||||
-- | Print ledger entries in standard format.
|
-- | Print ledger entries in standard format.
|
||||||
printentries :: [Opt] -> [String] -> Ledger -> IO ()
|
print' :: [Opt] -> [String] -> Ledger -> IO ()
|
||||||
printentries opts args l = putStr $ showEntries $ setprecisions $ entries $ rawledger l
|
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))
|
where setprecisions = map (entrySetPrecision (lprecision l))
|
||||||
|
|||||||
@ -14,8 +14,8 @@ registercommandtests = TestList [
|
|||||||
]
|
]
|
||||||
|
|
||||||
-- | Print a register report.
|
-- | Print a register report.
|
||||||
printregister :: [Opt] -> [String] -> Ledger -> IO ()
|
register :: [Opt] -> [String] -> Ledger -> IO ()
|
||||||
printregister opts args l = putStr $ showTransactionsWithBalances opts args l
|
register opts args l = putStr $ showTransactionsWithBalances opts args l
|
||||||
|
|
||||||
showTransactionsWithBalances :: [Opt] -> [String] -> Ledger -> String
|
showTransactionsWithBalances :: [Opt] -> [String] -> Ledger -> String
|
||||||
showTransactionsWithBalances opts args l =
|
showTransactionsWithBalances opts args l =
|
||||||
|
|||||||
14
Tests.hs
14
Tests.hs
@ -102,7 +102,7 @@ balancecommandtests =
|
|||||||
\ $-2 income\n\
|
\ $-2 income\n\
|
||||||
\ $1 liabilities\n\
|
\ $1 liabilities\n\
|
||||||
\" --"
|
\" --"
|
||||||
(balancereport [] [] l)
|
(showBalanceReport [] [] l)
|
||||||
,
|
,
|
||||||
|
|
||||||
"balance report with showsubs" ~: do
|
"balance report with showsubs" ~: do
|
||||||
@ -119,7 +119,7 @@ balancecommandtests =
|
|||||||
\ $-1 salary\n\
|
\ $-1 salary\n\
|
||||||
\ $1 liabilities:debts\n\
|
\ $1 liabilities:debts\n\
|
||||||
\" --"
|
\" --"
|
||||||
(balancereport [ShowSubs] [] l)
|
(showBalanceReport [ShowSubs] [] l)
|
||||||
,
|
,
|
||||||
|
|
||||||
"balance report with account pattern o" ~: do
|
"balance report with account pattern o" ~: do
|
||||||
@ -130,7 +130,7 @@ balancecommandtests =
|
|||||||
\--------------------\n\
|
\--------------------\n\
|
||||||
\ $-1\n\
|
\ $-1\n\
|
||||||
\" --"
|
\" --"
|
||||||
(balancereport [] ["o"] l)
|
(showBalanceReport [] ["o"] l)
|
||||||
,
|
,
|
||||||
|
|
||||||
"balance report with account pattern o and showsubs" ~: do
|
"balance report with account pattern o and showsubs" ~: do
|
||||||
@ -143,7 +143,7 @@ balancecommandtests =
|
|||||||
\--------------------\n\
|
\--------------------\n\
|
||||||
\ $-1\n\
|
\ $-1\n\
|
||||||
\" --"
|
\" --"
|
||||||
(balancereport [ShowSubs] ["o"] l)
|
(showBalanceReport [ShowSubs] ["o"] l)
|
||||||
,
|
,
|
||||||
|
|
||||||
"balance report with account pattern a" ~: do
|
"balance report with account pattern a" ~: do
|
||||||
@ -157,7 +157,7 @@ balancecommandtests =
|
|||||||
\--------------------\n\
|
\--------------------\n\
|
||||||
\ $-1\n\
|
\ $-1\n\
|
||||||
\" --"
|
\" --"
|
||||||
(balancereport [] ["a"] l)
|
(showBalanceReport [] ["a"] l)
|
||||||
,
|
,
|
||||||
|
|
||||||
"balance report with account pattern e" ~: do
|
"balance report with account pattern e" ~: do
|
||||||
@ -169,7 +169,7 @@ balancecommandtests =
|
|||||||
\ $-2 income\n\
|
\ $-2 income\n\
|
||||||
\ $1 liabilities:debts\n\
|
\ $1 liabilities:debts\n\
|
||||||
\" --"
|
\" --"
|
||||||
(balancereport [] ["e"] l)
|
(showBalanceReport [] ["e"] l)
|
||||||
,
|
,
|
||||||
|
|
||||||
"balance report with unmatched parent of two matched subaccounts" ~: do
|
"balance report with unmatched parent of two matched subaccounts" ~: do
|
||||||
@ -180,7 +180,7 @@ balancecommandtests =
|
|||||||
\--------------------\n\
|
\--------------------\n\
|
||||||
\ $-1\n\
|
\ $-1\n\
|
||||||
\" --"
|
\" --"
|
||||||
(balancereport [] ["cash","saving"] l)
|
(showBalanceReport [] ["cash","saving"] l)
|
||||||
]
|
]
|
||||||
|
|
||||||
-- | Assert a parsed thing equals some expected thing, or print a parse error.
|
-- | Assert a parsed thing equals some expected thing, or print a parse error.
|
||||||
|
|||||||
18
Utils.hs
18
Utils.hs
@ -1,22 +1,6 @@
|
|||||||
{-|
|
{-|
|
||||||
|
|
||||||
Utilities for top-level modules. See also "Ledger.Utils".
|
Utilities for top-level modules and/or ghci. 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
|
|
||||||
|
|
||||||
-}
|
-}
|
||||||
|
|
||||||
|
|||||||
37
hledger.hs
37
hledger.hs
@ -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
|
simple ledger-compatible register & balance reports from a plain text
|
||||||
ledger file, and demonstrates a functional implementation of ledger.
|
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
|
where
|
||||||
import qualified Data.Map as Map (lookup)
|
import qualified Data.Map as Map (lookup)
|
||||||
import Options
|
|
||||||
import Ledger
|
import Ledger
|
||||||
|
import Utils
|
||||||
|
import Options
|
||||||
import BalanceCommand
|
import BalanceCommand
|
||||||
import PrintCommand
|
import PrintCommand
|
||||||
import RegisterCommand
|
import RegisterCommand
|
||||||
import Tests
|
import Tests
|
||||||
import Utils
|
|
||||||
|
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
@ -32,9 +57,9 @@ main = do
|
|||||||
run cmd opts args
|
run cmd opts args
|
||||||
| Help `elem` opts = putStr usage
|
| Help `elem` opts = putStr usage
|
||||||
| Version `elem` opts = putStr version
|
| Version `elem` opts = putStr version
|
||||||
| cmd `isPrefixOf` "balance" = parseLedgerAndDo opts args printbalance
|
| cmd `isPrefixOf` "balance" = parseLedgerAndDo opts args balance
|
||||||
| cmd `isPrefixOf` "print" = parseLedgerAndDo opts args printentries
|
| cmd `isPrefixOf` "print" = parseLedgerAndDo opts args print'
|
||||||
| cmd `isPrefixOf` "register" = parseLedgerAndDo opts args printregister
|
| cmd `isPrefixOf` "register" = parseLedgerAndDo opts args register
|
||||||
| cmd `isPrefixOf` "test" = runtests >> return ()
|
| cmd `isPrefixOf` "test" = runtests >> return ()
|
||||||
| otherwise = putStr usage
|
| otherwise = putStr usage
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user