Compare commits

...

7 Commits

Author SHA1 Message Date
Bel LaPointe
37f6f47375 pretty print vs cumulative
All checks were successful
cicd / ci (push) Successful in 1m52s
2024-12-12 22:56:29 -07:00
Bel LaPointe
1a517eb8f2 temp 2024-12-12 22:53:40 -07:00
Bel LaPointe
c3f7800dec can get retirement contributions with grepping 2024-12-12 22:44:38 -07:00
Bel LaPointe
e25d52b141 prettier printing 2024-12-12 22:33:21 -07:00
Bel LaPointe
7a828f8463 reg only prints accounts matching LIKES and not NOTLIKEs 2024-12-12 22:29:30 -07:00
Bel LaPointe
117c300533 reg prints balance per xaction now 2024-12-12 22:28:40 -07:00
Bel LaPointe
4d484b8aa4 reg prints balance per xaction now 2024-12-12 22:27:44 -07:00
4 changed files with 87 additions and 20 deletions

View File

@@ -7,5 +7,7 @@ type Config struct {
Sort string Sort string
NoRounding bool NoRounding bool
Depth int Depth int
Reverse bool
NoExchanging bool
} }
} }

View File

@@ -19,6 +19,8 @@ func Main() {
fs.StringVar(&config.Query.Sort, "S", "", "sort ie date") fs.StringVar(&config.Query.Sort, "S", "", "sort ie date")
fs.BoolVar(&config.Query.NoRounding, "no-rounding", false, "no rounding") fs.BoolVar(&config.Query.NoRounding, "no-rounding", false, "no rounding")
fs.IntVar(&config.Query.Depth, "depth", 0, "depth grouping") fs.IntVar(&config.Query.Depth, "depth", 0, "depth grouping")
fs.BoolVar(&config.Query.Reverse, "r", false, "reverse printed accounts")
fs.BoolVar(&config.Query.NoExchanging, "no-exchanging", true, "omit currency exchanges")
if err := fs.Parse(os.Args[1:]); err != nil { if err := fs.Parse(os.Args[1:]); err != nil {
panic(err) panic(err)
} }
@@ -86,33 +88,50 @@ func Main() {
balances = balances.NotLike(notLikePattern) balances = balances.NotLike(notLikePattern)
} }
FPrintBalances(os.Stdout, balances) FPrintBalances(os.Stdout, "", balances, nil)
case "reg": case "reg":
register := deltas.Register() transactions := deltas.Transactions()
likes := []ledger.Like{}
if likePattern != "" { if likePattern != "" {
register = register.Like(likePattern) like := ledger.LikeName(likePattern)
transactions = transactions.Like(like)
if config.Query.Reverse {
like = ledger.LikeNot(like)
}
likes = append(likes, like)
} }
if notLikePattern != "" { if notLikePattern != "" {
register = register.NotLike(notLikePattern) like := ledger.NotLikeName(notLikePattern)
transactions = transactions.NotLike(like)
if config.Query.Reverse {
like = ledger.LikeNot(like)
}
likes = append(likes, like)
} }
var prev ledger.Balances for i, transaction := range transactions {
for _, date := range register.Dates() { balances := ledger.Deltas(transaction).Like(likes...).Balances()
balances := register[date] shouldPrint := false
shouldPrint = shouldPrint || len(balances) > 2
if newBalances := balances.Sub(prev).Nonzero(); len(newBalances) > 0 { if config.Query.NoExchanging {
fmt.Println(date) shouldPrint = shouldPrint || len(balances) > 1
FPrintBalances(os.Stdout, newBalances) for _, v := range balances {
shouldPrint = shouldPrint || len(v) == 1
}
} else {
shouldPrint = shouldPrint || len(balances) > 0
}
if shouldPrint {
fmt.Printf("%s\t%s\n", transaction[0].Date, transaction[0].Description)
FPrintBalances(os.Stdout, "\t\t", balances, transactions[:i+1].Deltas().Like(likes...).Balances())
} }
prev = balances
} }
default: default:
panic("unknown command " + positional[0]) panic("unknown command " + positional[0])
} }
} }
func FPrintBalances(w io.Writer, balances ledger.Balances) { func FPrintBalances(w io.Writer, linePrefix string, balances, cumulatives ledger.Balances) {
keys := []string{} keys := []string{}
for k := range balances { for k := range balances {
keys = append(keys, k) keys = append(keys, k)
@@ -126,7 +145,7 @@ func FPrintBalances(w io.Writer, balances ledger.Balances) {
} }
} }
format := fmt.Sprintf("%%-%ds\t%%s%%.2f\n", max) format := fmt.Sprintf("%s%%-%ds\t%%s%%.2f (%%s%%.2f)\n", linePrefix, max)
for _, key := range keys { for _, key := range keys {
currencies := []ledger.Currency{} currencies := []ledger.Currency{}
for currency := range balances[key] { for currency := range balances[key] {
@@ -139,7 +158,15 @@ func FPrintBalances(w io.Writer, balances ledger.Balances) {
if printableCurrency != "$" { if printableCurrency != "$" {
printableCurrency += " " printableCurrency += " "
} }
fmt.Fprintf(w, format, key, printableCurrency, balances[key][currency])
cumulative := balances[key][currency]
if balance, ok := cumulatives[key]; !ok {
} else if value, ok := balance[currency]; !ok {
} else {
cumulative = value
}
fmt.Fprintf(w, format, key, printableCurrency, balances[key][currency], printableCurrency, cumulative)
} }
} }
} }

View File

@@ -41,6 +41,16 @@ func LikeNotName(pattern string) Like {
} }
} }
func LikeNot(like Like) Like {
return func(d Delta) bool {
return !like(d)
}
}
func NotLikeName(pattern string) Like {
return LikeNot(LikeName(pattern))
}
func LikeName(pattern string) Like { func LikeName(pattern string) Like {
return func(d Delta) bool { return func(d Delta) bool {
return like(pattern, d.Name) return like(pattern, d.Name)

View File

@@ -18,6 +18,14 @@ type Transaction Deltas
type Transactions []Transaction type Transactions []Transaction
func (transactions Transactions) Deltas() Deltas {
result := make(Deltas, 0, len(transactions))
for _, transaction := range transactions {
result = append(result, transaction...)
}
return result
}
func (deltas Deltas) Transactions() Transactions { func (deltas Deltas) Transactions() Transactions {
m := make(map[string]Transaction) m := make(map[string]Transaction)
for _, d := range deltas { for _, d := range deltas {
@@ -42,6 +50,26 @@ func (deltas Deltas) Transactions() Transactions {
return result return result
} }
func (transactions Transactions) NotLike(like ...Like) Transactions {
result := make(Transactions, 0, len(transactions))
for _, transaction := range transactions {
if matching := (Deltas)(transaction).Like(like...); len(matching) == 0 {
result = append(result, transaction)
}
}
return result
}
func (transactions Transactions) Like(like ...Like) Transactions {
result := make(Transactions, 0, len(transactions))
for _, transaction := range transactions {
if matching := (Deltas)(transaction).Like(like...); len(matching) > 0 {
result = append(result, transaction)
}
}
return result
}
func (transaction Transaction) Payee() string { func (transaction Transaction) Payee() string {
balances := Deltas(transaction).Balances() balances := Deltas(transaction).Balances()