Skip to content

Commit

Permalink
preserve comments in parsed structure
Browse files Browse the repository at this point in the history
  • Loading branch information
howeyc committed Nov 23, 2020
1 parent d1e3a0e commit f214710
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
6 changes: 6 additions & 0 deletions cmd/ledger/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("")
Expand Down
7 changes: 6 additions & 1 deletion parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
}
Expand Down
14 changes: 14 additions & 0 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -144,6 +148,9 @@ var testCases = []testCase{
big.NewRat(-123.0, 1),
},
},
Comments: []string{
"; Expense/test 123",
},
},
},
nil,
Expand Down Expand Up @@ -173,6 +180,10 @@ var testCases = []testCase{
big.NewRat(0, 1),
},
},
Comments: []string{
"; comment",
"; comment in trans",
},
},
},
nil,
Expand Down Expand Up @@ -207,6 +218,9 @@ var testCases = []testCase{
big.NewRat(-158, 1),
},
},
Comments: []string{
"; comment",
},
},
},
nil,
Expand Down
1 change: 1 addition & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ type Transaction struct {
Payee string
Date time.Time
AccountChanges []Account
Comments []string
}

0 comments on commit f214710

Please sign in to comment.