From a1b316f08111837e6aa908593beba1cd30a5870f Mon Sep 17 00:00:00 2001 From: Chris Howey Date: Sat, 13 Apr 2013 21:33:02 -0500 Subject: [PATCH] Add register --- ledger.go | 20 ++++++++++++++++++++ main.go | 2 ++ 2 files changed, 22 insertions(+) diff --git a/ledger.go b/ledger.go index 88fc7823..a5824c30 100644 --- a/ledger.go +++ b/ledger.go @@ -162,6 +162,26 @@ func printLedger(w io.Writer, generalLedger []*Transaction, columns int) { } } +func printRegister(generalLedger []*Transaction, filter string, columns int) { + runningBalance := new(big.Rat) + for _, trans := range generalLedger { + for _, accChange := range trans.AccountChanges { + if strings.Contains(accChange.Name, filter) { + runningBalance.Add(runningBalance, accChange.Balance) + writtenBytes, _ := fmt.Printf("%s %s", trans.Date.Format(TransactionDateFormat), trans.Payee) + outBalanceString := accChange.Balance.FloatString(2) + outRunningBalanceString := runningBalance.FloatString(2) + spaceCount := columns - writtenBytes - 2 - len(outBalanceString) - len(outRunningBalanceString) + if spaceCount < 0 { + spaceCount = 0 + } + fmt.Printf("%s%s %s", strings.Repeat(" ", spaceCount), outBalanceString, outRunningBalanceString) + fmt.Println("") + } + } + } +} + func balanceTransaction(input *Transaction) error { balance := new(big.Rat) var emptyAccPtr *Account diff --git a/main.go b/main.go index 6e2ae941..43a64640 100644 --- a/main.go +++ b/main.go @@ -66,5 +66,7 @@ func main() { printBalances(getBalances(generalLedger, containsFilter), showEmptyAccounts, transactionDepth, columnWidth) case "print": printLedger(os.Stdout, generalLedger, columnWidth) + case "register": + printRegister(generalLedger, containsFilter, columnWidth) } }