diff --git a/cmd/cli/main.go b/cmd/cli/main.go index 897a139..6472c7f 100644 --- a/cmd/cli/main.go +++ b/cmd/cli/main.go @@ -98,6 +98,13 @@ func Main() { w := bufio.NewWriter(os.Stdout) defer w.Flush() + maxAccW := 0 + for _, delta := range deltas.Like(q).Group(group) { + if accW := len(delta.Name); accW > maxAccW { + maxAccW = accW + } + } + switch cmd[:3] { case "bal": balances := deltas.Balances(). @@ -105,7 +112,7 @@ func Main() { KindaLike(q). KindaGroup(group). Nonzero() - FPrintBalances(w, "", balances, nil, config.Query.USDOnly, config.Query.Normalize, time.Now().Format("2006-01-02"), false) + FPrintBalances(w, "", balances, nil, config.Query.USDOnly, config.Query.Normalize, time.Now().Format("2006-01-02"), false, maxAccW) case "reg": transactions := deltas.Transactions() cumulative := make(ledger.Balances) @@ -124,7 +131,7 @@ func Main() { if shouldPrint { cumulative.PushAll(balances) cumulative = cumulative.Nonzero() - FPrintBalancesFor(transaction[0].Description, w, "\t\t", balances, cumulative, config.Query.USDOnly, config.Query.Normalize, transaction[0].Date, config.Compact) + FPrintBalancesFor(transaction[0].Description, w, "\t\t", balances, cumulative, config.Query.USDOnly, config.Query.Normalize, transaction[0].Date, config.Compact, maxAccW) } } default: @@ -132,16 +139,16 @@ func Main() { } } -func FPrintBalancesFor(description string, w io.Writer, linePrefix string, balances, cumulatives ledger.Balances, usdOnly, normalized bool, date string, compact bool) { +func FPrintBalancesFor(description string, w io.Writer, linePrefix string, balances, cumulatives ledger.Balances, usdOnly, normalized bool, date string, compact bool, keyW int) { if compact { - FPrintBalances(w, date+"\t", balances, cumulatives, usdOnly, normalized, date, compact) + FPrintBalances(w, date+"\t", balances, cumulatives, usdOnly, normalized, date, compact, keyW) } else { fmt.Fprintf(w, "%s\t%s\n", date, description) - FPrintBalances(w, linePrefix, balances, cumulatives, usdOnly, normalized, date, compact) + FPrintBalances(w, linePrefix, balances, cumulatives, usdOnly, normalized, date, compact, keyW) } } -func FPrintBalances(w io.Writer, linePrefix string, balances, cumulatives ledger.Balances, usdOnly, normalized bool, date string, fullKey bool) { +func FPrintBalances(w io.Writer, linePrefix string, balances, cumulatives ledger.Balances, usdOnly, normalized bool, date string, fullKey bool, max int) { maxes := map[ledger.Currency]float64{} keys := []string{} for k, v := range balances { @@ -154,13 +161,6 @@ func FPrintBalances(w io.Writer, linePrefix string, balances, cumulatives ledger } slices.Sort(keys) - max := 0 - for _, k := range keys { - if n := len(k); n > max { - max = n - } - } - normalizer := ana.NewDefaultNormalizer() format := fmt.Sprintf("%s%%-%ds\t%%s%%.2f (%%s%%.2f)\n", linePrefix, max)