Compare commits

...

2 Commits

Author SHA1 Message Date
Bel LaPointe
dd20f066a3 WIP cmd/cli$ go run ../ cli $(printf " -f %s" $HOME/Sync/Core/ledger/eras/2022-/*.txt) -n -w ^Housey --depth 1 -usd bal
All checks were successful
cicd / ci (push) Successful in 1m32s
2025-04-03 21:39:36 -06:00
Bel LaPointe
697fc16b52 each delta has .with and LikeWith to find X related to Y 2025-04-03 17:28:57 -06:00
8 changed files with 70 additions and 11 deletions

View File

@@ -8,8 +8,9 @@ type Config struct {
Sort string
NoRounding bool
Depth int
Reverse bool
With string
NoExchanging bool
Normalize bool
USDOnly bool
}
}

View File

@@ -20,9 +20,10 @@ func Main() {
fs.Var(&config.Query.Period, "period", "period")
fs.StringVar(&config.Query.Sort, "S", "", "sort ie date")
fs.BoolVar(&config.Query.NoRounding, "no-rounding", false, "no rounding")
fs.StringVar(&config.Query.With, "w", "", "regexp for transactions")
fs.IntVar(&config.Query.Depth, "depth", 0, "depth grouping")
fs.BoolVar(&config.Query.Reverse, "r", false, "reverse printed accounts")
fs.BoolVar(&config.Query.Normalize, "n", false, "normalize with default normalizer")
fs.BoolVar(&config.Query.USDOnly, "usd", false, "filter to usd")
fs.BoolVar(&config.Query.NoExchanging, "no-exchanging", true, "omit currency exchanges")
fs.StringVar(&config.BPI, "bpi", "", "path to bpi")
if err := fs.Parse(os.Args[1:]); err != nil {
@@ -44,7 +45,7 @@ func Main() {
}
cmd := positional[0]
q, err := BuildQuery(positional[1:])
q, err := BuildQuery(config, positional[1:])
if err != nil {
panic(err)
}
@@ -93,7 +94,7 @@ func Main() {
KindaLike(q).
KindaGroup(group).
Nonzero()
FPrintBalances(os.Stdout, "", balances, nil)
FPrintBalances(os.Stdout, "", balances, nil, config.Query.USDOnly)
case "reg":
transactions := deltas.Transactions()
@@ -111,7 +112,7 @@ func Main() {
}
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(q).Group(group).Balances().WithBPIsAt(bpis, transaction[0].Date).Nonzero())
FPrintBalances(os.Stdout, "\t\t", balances, transactions[:i+1].Deltas().Like(q).Group(group).Balances().WithBPIsAt(bpis, transaction[0].Date).Nonzero(), config.Query.USDOnly)
}
}
default:
@@ -119,7 +120,7 @@ func Main() {
}
}
func FPrintBalances(w io.Writer, linePrefix string, balances, cumulatives ledger.Balances) {
func FPrintBalances(w io.Writer, linePrefix string, balances, cumulatives ledger.Balances, usdOnly bool) {
keys := []string{}
for k := range balances {
keys = append(keys, k)
@@ -146,6 +147,9 @@ func FPrintBalances(w io.Writer, linePrefix string, balances, cumulatives ledger
if printableCurrency != "$" {
printableCurrency += " "
}
if usdOnly && printableCurrency != "$" {
continue
}
cumulative := balances[key][currency]
if balance, ok := cumulatives[key]; !ok {

View File

@@ -9,7 +9,7 @@ import (
type Query struct{}
func BuildQuery(args args) (ledger.Like, error) {
func BuildQuery(config Config, args args) (ledger.Like, error) {
var result ledger.Like
var err error
func() {
@@ -18,12 +18,12 @@ func BuildQuery(args args) (ledger.Like, error) {
err = fmt.Errorf("panicked: %v", err)
}
}()
result, err = buildQuery(args)
result, err = buildQuery(config, args)
}()
return result, err
}
func buildQuery(args args) (ledger.Like, error) {
func buildQuery(config Config, args args) (ledger.Like, error) {
likeName := func(s string) ledger.Like {
return ledger.LikeName(s)
}
@@ -71,6 +71,10 @@ func buildQuery(args args) (ledger.Like, error) {
}
}
if config.Query.With != "" {
like = andLike(like, ledger.LikeWith(config.Query.With))
}
return like, nil
}

View File

@@ -20,6 +20,7 @@ type Delta struct {
isSet bool
fileName string
lineNo int
with []Delta
}
func newDelta(transaction string, payee bool, d, desc, name string, v float64, c string, isSet bool, fileName string, lineNo int) Delta {
@@ -38,12 +39,20 @@ func newDelta(transaction string, payee bool, d, desc, name string, v float64, c
}
}
func (delta Delta) withWith(other Delta) Delta {
other.with = nil
delta.with = append(delta.with, other)
return delta
}
func (delta Delta) equivalent(other Delta) bool {
delta.fileName = ""
delta.lineNo = 0
delta.with = nil
other.fileName = ""
other.lineNo = 0
return delta == other
other.with = nil
return fmt.Sprintf("%+v", delta) == fmt.Sprintf("%+v", other)
}
func (delta Delta) Debug() string {

View File

@@ -443,7 +443,7 @@ func TestFileDeltas(t *testing.T) {
t.Error(deltas[i].Transaction)
}
deltas[i].Transaction = ""
if want[i] != deltas[i] {
if !want[i].equivalent(deltas[i]) {
t.Errorf("[%d] \n\twant=%s, \n\t got=%s", i, want[i].Debug(), deltas[i].Debug())
}
}

View File

@@ -8,6 +8,21 @@ type Like func(Delta) bool
type Likes []Like
func LikeWith(pattern string) Like {
l := LikeName(pattern)
return func(d Delta) bool {
if l(d) {
return true
}
for _, d := range d.with {
if l(d) {
return true
}
}
return false
}
}
func LikeTransactions(deltas ...Delta) Like {
return func(d Delta) bool {
for i := range deltas {

View File

@@ -36,3 +36,20 @@ func TestLikesAll(t *testing.T) {
t.Error(likes.All(delta))
}
}
func TestLikeWith(t *testing.T) {
delta := Delta{
Name: "x",
with: []Delta{
Delta{Name: "y"},
Delta{Name: "z"},
},
}
if !LikeWith("x")(delta) {
t.Error("like with self not caught")
}
if !LikeWith("z")(delta) {
t.Error("like with reverse not caught")
}
}

View File

@@ -165,6 +165,15 @@ func (t transaction) deltas() Deltas {
))
}
}
for i := range result {
for j := range result {
if i != j {
result[i] = result[i].withWith(result[j])
}
}
}
return result
}