diff --git a/cmd/ledger/print.go b/cmd/ledger/print.go index aec5675b..f93f98ae 100644 --- a/cmd/ledger/print.go +++ b/cmd/ledger/print.go @@ -62,10 +62,16 @@ func PrintBalances(accountList []*ledger.Account, printZeroBalances bool, depth, // PrintTransaction prints a transaction formatted to fit in specified column width. func PrintTransaction(trans *ledger.Transaction, columns int) { + for _, c := range trans.Comments { + fmt.Println(c) + } fmt.Printf("%s %s\n", trans.Date.Format(transactionDateFormat), trans.Payee) for _, accChange := range trans.AccountChanges { outBalanceString := accChange.Balance.FloatString(displayPrecision) spaceCount := columns - 4 - utf8.RuneCountInString(accChange.Name) - utf8.RuneCountInString(outBalanceString) + if spaceCount < 1 { + spaceCount = 1 + } fmt.Printf(" %s%s%s\n", accChange.Name, strings.Repeat(" ", spaceCount), outBalanceString) } fmt.Println("") diff --git a/parse.go b/parse.go index 616ca352..bdaca734 100644 --- a/parse.go +++ b/parse.go @@ -70,6 +70,7 @@ func parseLedger(ledgerReader io.Reader, callback func(t *Transaction, err error var line string var filename string var lineCount int + var comments []string errorMsg := func(msg string) (stop bool) { return callback(nil, fmt.Errorf("%s:%d: %s", filename, lineCount, msg)) @@ -90,6 +91,7 @@ func parseLedger(ledgerReader io.Reader, callback func(t *Transaction, err error // handle comments if commentIdx := strings.Index(trimmedLine, ";"); commentIdx >= 0 { + comments = append(comments, trimmedLine[commentIdx:]) trimmedLine = trimmedLine[:commentIdx] if len(trimmedLine) == 0 { continue @@ -102,11 +104,13 @@ func parseLedger(ledgerReader io.Reader, callback func(t *Transaction, err error if transErr != nil { errorMsg("Unable to balance transaction, " + transErr.Error()) } + trans.Comments = comments callback(trans, nil) + comments = nil trans = nil } } else if trans == nil { - lineSplit := strings.SplitN(line, " ", 2) + lineSplit := strings.SplitN(trimmedLine, " ", 2) if len(lineSplit) != 2 { if errorMsg("Unable to parse payee line: " + line) { return @@ -148,6 +152,7 @@ func parseLedger(ledgerReader io.Reader, callback func(t *Transaction, err error if transErr != nil { errorMsg("Unable to balance transaction, " + transErr.Error()) } + trans.Comments = comments callback(trans, nil) } } diff --git a/parse_test.go b/parse_test.go index bacdddec..19ccd250 100644 --- a/parse_test.go +++ b/parse_test.go @@ -78,6 +78,10 @@ var testCases = []testCase{ big.NewRat(10.0, 1), }, }, + Comments: []string{ + "; Handle tabs between account and amount", + "; Also handle accounts with spaces", + }, }, &Transaction{ Payee: "Payee", @@ -144,6 +148,9 @@ var testCases = []testCase{ big.NewRat(-123.0, 1), }, }, + Comments: []string{ + "; Expense/test 123", + }, }, }, nil, @@ -173,6 +180,10 @@ var testCases = []testCase{ big.NewRat(0, 1), }, }, + Comments: []string{ + "; comment", + "; comment in trans", + }, }, }, nil, @@ -207,6 +218,9 @@ var testCases = []testCase{ big.NewRat(-158, 1), }, }, + Comments: []string{ + "; comment", + }, }, }, nil, diff --git a/types.go b/types.go index 4f417338..9a16bd25 100644 --- a/types.go +++ b/types.go @@ -19,4 +19,5 @@ type Transaction struct { Payee string Date time.Time AccountChanges []Account + Comments []string }