From 42ba85c4e12bab6719e5b76d17ddcedba15b6fcc Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Wed, 4 Jul 2007 09:28:07 +0000 Subject: [PATCH] print command --- Account.hs | 3 --- Entry.hs | 54 +++++++++++++++++++++++++++++++++++++++------ EntryTransaction.hs | 5 +++-- Ledger.hs | 15 +++++++++++++ Makefile | 4 +++- NOTES | 6 +++-- Options.hs | 2 +- RawLedger.hs | 16 -------------- Transaction.hs | 2 ++ hledger.hs | 17 ++++++++++---- 10 files changed, 88 insertions(+), 36 deletions(-) diff --git a/Account.hs b/Account.hs index c016d1ec9..786fe765b 100644 --- a/Account.hs +++ b/Account.hs @@ -1,7 +1,5 @@ module Account where -import qualified Data.Map as Map - import Utils import Types import AccountName @@ -9,7 +7,6 @@ import Amount import Entry import Transaction import EntryTransaction -import RawLedger instance Show Account where diff --git a/Entry.hs b/Entry.hs index b0974ce4d..def8f3085 100644 --- a/Entry.hs +++ b/Entry.hs @@ -4,22 +4,25 @@ where import Utils import Types import Transaction +import Amount -instance Show Entry where show = showEntry +instance Show Entry where show = showEntryDescription +-- for register report +-- -- a register entry is displayed as two or more lines like this: -- date description account amount balance -- DDDDDDDDDD dddddddddddddddddddd aaaaaaaaaaaaaaaaaaaaaa AAAAAAAAAAA AAAAAAAAAAAA -- aaaaaaaaaaaaaaaaaaaaaa AAAAAAAAAAA AAAAAAAAAAAA -- ... ... ... --- dateWidth = 10 --- descWidth = 20 --- acctWidth = 22 --- amtWidth = 11 --- balWidth = 12 +-- datewidth = 10 +-- descwidth = 20 +-- acctwidth = 22 +-- amtwidth = 11 +-- balwidth = 12 -showEntry e = (showDate $ edate e) ++ " " ++ (showDescription $ edescription e) ++ " " +showEntryDescription e = (showDate $ edate e) ++ " " ++ (showDescription $ edescription e) ++ " " showDate d = printf "%-10s" d showDescription s = printf "%-20s" (elideRight 20 s) @@ -31,6 +34,43 @@ autofillEntry e = Entry (edate e) (estatus e) (ecode e) (edescription e) (autofillTransactions (etransactions e)) +-- the print command shows cleaned up ledger file entries, something like: +-- +-- yyyy/mm/dd[ *][ CODE] description......... [ ; comment.............] +-- account name 1..................... ...$amount1[ ; comment.............] +-- account name 2..................... ..$-amount1[ ; comment.............] +-- +-- codewidth = 10 +-- descwidth = 20 +-- acctwidth = 35 +-- amtwidth = 11 +-- commentwidth = 20 + +showEntry :: Entry -> String +showEntry e = + unlines $ ["", description] ++ (showtxns $ etransactions e) + where + description = concat [date, status, code, desc] + date = showDate $ edate e + status = if estatus e then " *" else "" + code = if (length $ ecode e) > 0 then " "++(printf "%-10s" $ ecode e) else "" + desc = " " ++ (elideRight 20 $ edescription e) + showtxns (t1:t2:[]) = [showtxn t1, showtxnnoamt t2] + showtxns ts = map showtxn ts + showtxn t = showacct t ++ " " ++ (showamount $ tamount t) + showtxnnoamt t = showacct t ++ " " + showacct t = " " ++ (showaccountname $ taccount t) + showamount = printf "%11s" . showAmountRounded + showaccountname = printf "%-35s" . elideRight 35 + +showEntries :: [Entry] -> String +showEntries = concatMap showEntry + +entrySetPrecision :: Int -> Entry -> Entry +entrySetPrecision p (Entry d s c desc ts) = + Entry d s c desc $ map (transactionSetPrecision p) ts + + -- modifier & periodic entries instance Show ModifierEntry where diff --git a/EntryTransaction.hs b/EntryTransaction.hs index 1247d6319..396365117 100644 --- a/EntryTransaction.hs +++ b/EntryTransaction.hs @@ -46,6 +46,8 @@ matchTransactionDescription r t = Nothing -> False otherwise -> True +-- for register command + showTransactionsWithBalances :: [EntryTransaction] -> Amount -> String showTransactionsWithBalances [] _ = [] showTransactionsWithBalances ts b = @@ -62,7 +64,7 @@ showTransactionsWithBalances ts b = showTransactionDescriptionAndBalance :: EntryTransaction -> Amount -> String showTransactionDescriptionAndBalance t b = - (showEntry $ entry t) ++ (showTransaction $ transaction t) ++ (showBalance b) + (showEntryDescription $ entry t) ++ (showTransaction $ transaction t) ++ (showBalance b) showTransactionAndBalance :: EntryTransaction -> Amount -> String showTransactionAndBalance t b = @@ -77,4 +79,3 @@ transactionsWithAccountName a ts = [t | t <- ts, account t == a] transactionsWithOrBelowAccountName :: AccountName -> [EntryTransaction] -> [EntryTransaction] transactionsWithOrBelowAccountName a ts = [t | t <- ts, account t == a || a `isAccountNamePrefixOf` (account t)] - diff --git a/Ledger.hs b/Ledger.hs index 527ec4cbe..0104e17af 100644 --- a/Ledger.hs +++ b/Ledger.hs @@ -11,6 +11,19 @@ import EntryTransaction import RawLedger +rawLedgerTransactions :: RawLedger -> [EntryTransaction] +rawLedgerTransactions l = entryTransactionsFrom $ entries l + +rawLedgerAccountNamesUsed :: RawLedger -> [AccountName] +rawLedgerAccountNamesUsed l = accountNamesFromTransactions $ entryTransactionsFrom $ entries l + +rawLedgerAccountNames :: RawLedger -> [AccountName] +rawLedgerAccountNames = sort . expandAccountNames . rawLedgerAccountNamesUsed + +rawLedgerAccountNameTree :: RawLedger -> Tree AccountName +rawLedgerAccountNameTree l = accountNameTreeFrom $ rawLedgerAccountNames l + + instance Show Ledger where show l = printf "Ledger with %d entries, %d accounts" ((length $ entries $ rawledger l) + @@ -18,6 +31,8 @@ instance Show Ledger where (length $ periodic_entries $ rawledger l)) (length $ accountnames l) +-- at startup, we augment the parsed ledger entries with an account map +-- and other things useful for performance cacheLedger :: RawLedger -> Ledger cacheLedger l = let diff --git a/Makefile b/Makefile index 4ee905a62..9f3d7483e 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ TIME=`date +"%Y%m%d%H%M"` build: Tags $(BUILD) -buildopt: clean +buildopt opt: clean $(BUILDOPT) profile: build @@ -28,8 +28,10 @@ compare: rm -f 1 2 ledger -s balance >1 ledger register >>1 + ledger print >>1 ./hledger.hs -s balance >2 ./hledger.hs register >>2 + ./hledger.hs print >>2 diff 1 2 haddock: diff --git a/NOTES b/NOTES index 31e1d03e3..684fce6e6 100644 --- a/NOTES +++ b/NOTES @@ -5,6 +5,7 @@ hledger project notes *** rename EntryTransaction/Transaction ** ledger features *** print command +**** need to save & print comments *** handle mixed amounts, non-money currencies **** handle precision per currency *** handle time logs @@ -23,8 +24,8 @@ hledger project notes *** read gnucash files *** other ledger args, directives ** new features -*** simpler timelog format -*** auto-generate missing clock-out +*** alternate timelog format +*** infer clock-out *** graph automation *** entry and smart data entry *** incorporate timeclock features @@ -40,6 +41,7 @@ hledger project notes *** differences **** ledger shows comments after descriptions as part of description **** ledger does not sort register by date +**** ledger does not support -f- (no space) ** marketing *** set up as a cabal/hackage project following wiki howto http://en.wikibooks.org/wiki/Haskell/Packaging diff --git a/Options.hs b/Options.hs index 8f5487eac..c9d265b96 100644 --- a/Options.hs +++ b/Options.hs @@ -9,7 +9,7 @@ import Utils usagehdr = "Usage: hledger [OPTIONS] "++commands++" [ACCTPATTERNS] [-- DESCPATTERNS]\nOptions:" -commands = "register|balance" +commands = "register|balance|print" defaultcmd = "register" options :: [OptDescr Flag] diff --git a/RawLedger.hs b/RawLedger.hs index ca50c6821..6555c81cb 100644 --- a/RawLedger.hs +++ b/RawLedger.hs @@ -6,7 +6,6 @@ import Utils import Types import AccountName import Entry -import EntryTransaction instance Show RawLedger where @@ -14,18 +13,3 @@ instance Show RawLedger where ((length $ entries l) + (length $ modifier_entries l) + (length $ periodic_entries l)) - -rawLedgerTransactions :: RawLedger -> [EntryTransaction] -rawLedgerTransactions l = entryTransactionsFrom $ entries l - -rawLedgerAccountNamesUsed :: RawLedger -> [AccountName] -rawLedgerAccountNamesUsed l = accountNamesFromTransactions $ entryTransactionsFrom $ entries l - -rawLedgerAccountNames :: RawLedger -> [AccountName] -rawLedgerAccountNames = sort . expandAccountNames . rawLedgerAccountNamesUsed - -rawLedgerAccountNameTree :: RawLedger -> Tree AccountName -rawLedgerAccountNameTree l = accountNameTreeFrom $ rawLedgerAccountNames l - - - diff --git a/Transaction.hs b/Transaction.hs index e1adaaf29..4e68c87a6 100644 --- a/Transaction.hs +++ b/Transaction.hs @@ -33,3 +33,5 @@ autofillTransactions ts = sumTransactions :: [Transaction] -> Amount sumTransactions = sum . map tamount +transactionSetPrecision :: Int -> Transaction -> Transaction +transactionSetPrecision p (Transaction a amt) = Transaction a amt{precision=p} diff --git a/hledger.hs b/hledger.hs index cd81b7d9a..4d68f036c 100644 --- a/hledger.hs +++ b/hledger.hs @@ -28,6 +28,7 @@ main = do | Help `elem` opts = putStr usage | cmd `isPrefixOf` "register" = register opts acctpats descpats | cmd `isPrefixOf` "balance" = balance opts acctpats descpats + | cmd `isPrefixOf` "print" = printcmd opts | cmd `isPrefixOf` "test" = test | otherwise = putStr usage @@ -41,18 +42,26 @@ test = do register :: [Flag] -> [String] -> [String] -> IO () register opts acctpats descpats = do - doWithLedger opts printRegister + doWithLedger opts printregister where - printRegister l = + printregister l = putStr $ showTransactionsWithBalances (sortBy (comparing date) (ledgerTransactionsMatching (acctpats,descpats) l)) nullamt{precision=lprecision l} +printcmd :: [Flag] -> IO () +printcmd opts = do + doWithLedger opts printentries + where + printentries l = putStr $ showEntries $ setprecision $ entries $ rawledger l + where + setprecision = map (entrySetPrecision (lprecision l)) + balance :: [Flag] -> [String] -> [String] -> IO () balance opts acctpats _ = do - doWithLedger opts printBalance + doWithLedger opts printbalance where - printBalance l = + printbalance l = putStr $ showLedgerAccounts l acctpats showsubs maxdepth where showsubs = (ShowSubs `elem` opts)