Rewrote print functions

This commit is contained in:
2023-03-11 19:30:48 -05:00
parent 3dade3e3b0
commit c7fd50b347

180
main.go
View File

@@ -491,53 +491,31 @@ func (tl *TransactionLog) processTransactions(accounts []Account) bool {
endBalance := accounts endBalance := accounts
logrus.Debugf("\n\nITERATION %d\n\n", tl.Iteration) logrus.Debugf("\n\nITERATION %d\n\n", tl.Iteration)
if tl.Iteration == 0 { for _, date := range tl.dates() {
for _, date := range tl.dates() { logrus.Debugf("Processing Log Day %s", date.String())
logrus.Debugf("Processing Log Day %s", date.String()) logDay := tl.LogDays[date]
logDay := tl.LogDays[date] logDay.StartingBalance = make([]Account, len(endBalance))
logDay.StartingBalance = make([]Account, len(endBalance)) logDay.EndingBalance = make([]Account, len(endBalance))
logDay.EndingBalance = make([]Account, len(endBalance)) copy(logDay.StartingBalance, endBalance)
copy(logDay.StartingBalance, endBalance) copy(logDay.EndingBalance, endBalance)
copy(logDay.EndingBalance, endBalance) categorized := categorizeTransactions(logDay.Transactions)
categorized := categorizeTransactions(logDay.Transactions) if processTransactionsByID(tl, categorized["credits"]) {
logrus.Debugln(categorized)
processTransactionsByID(tl, categorized["credits"])
logrus.Debugf("Starting Balance %s: %f", date, logDay.StartingBalance[0].Balance)
logrus.Debugf("Ending Balance %s: %f", date, logDay.EndingBalance[0].Balance)
endBalance = logDay.EndingBalance
shifted = true shifted = true
} }
} else { if processTransactionsByID(tl, categorized["late"]) {
for _, date := range tl.dates() { shifted = true
logrus.Debugf("Processing Log Day %s", date.String()) }
logDay := tl.LogDays[date] if processTransactionsByID(tl, categorized["standard"]) {
logDay.StartingBalance = make([]Account, len(endBalance)) shifted = true
logDay.EndingBalance = make([]Account, len(endBalance)) }
copy(logDay.StartingBalance, endBalance) logrus.Debugf("Starting Balance %s: %f", date, logDay.StartingBalance[0].Balance)
copy(logDay.EndingBalance, endBalance) logrus.Debugf("Ending Balance %s: %f", date, logDay.EndingBalance[0].Balance)
categorized := categorizeTransactions(logDay.Transactions) endBalance = logDay.EndingBalance
logrus.Debugln(categorized) if shifted {
logrus.Debugln(logDay.Transactions) break
if processTransactionsByID(tl, categorized["credits"]) { }
shifted = true if len(logDay.Transactions) == 0 {
} tl.deleteDay(date)
if processTransactionsByID(tl, categorized["late"]) {
shifted = true
}
if processTransactionsByID(tl, categorized["standard"]) {
shifted = true
}
logrus.Debugf("Starting Balance %s: %f", date, logDay.StartingBalance[0].Balance)
logrus.Debugf("Ending Balance %s: %f", date, logDay.EndingBalance[0].Balance)
endBalance = logDay.EndingBalance
if shifted {
break
}
if len(logDay.Transactions) == 0 {
tl.deleteDay(date)
}
} }
} }
@@ -562,7 +540,7 @@ func processTransactionsByID(transactionLog *TransactionLog, transactionIDs []st
return shifted return shifted
} }
func printOutput(logDay LogDay) { func getTerminalWidth() int {
cmd := exec.Command("stty", "size") cmd := exec.Command("stty", "size")
cmd.Stdin = os.Stdin cmd.Stdin = os.Stdin
out, err := cmd.Output() out, err := cmd.Output()
@@ -571,58 +549,96 @@ func printOutput(logDay LogDay) {
} }
widthStr := string(out[:len(out)-1]) widthStr := string(out[:len(out)-1])
sttyOut := strings.Split(widthStr, " ") sttyOut := strings.Split(widthStr, " ")
fullWidth, err := strconv.Atoi(sttyOut[1]) width, err := strconv.Atoi(sttyOut[1])
fullWidth = fullWidth / 2
width := fullWidth - 2
if err != nil { if err != nil {
logrus.Fatal(err) logrus.Fatal(err)
} }
return width
}
delim := strings.Repeat("=", fullWidth) func printWithMaxWidth(maxWidth int, suffix string, strs ...string) {
divider := delim total := 0
delim2 := strings.Repeat("-", fullWidth-1) whiteSpace := regexp.MustCompile(`^\s+$`)
divider2 := delim2 + "|" for _, str := range strs {
line := ""
if whiteSpace.MatchString(str) {
fmt.Print(str)
total += len(str)
continue
}
words := strings.Fields(str)
for _, word := range words {
total += len(word)
if len(line)+len(word)+1 >= maxWidth {
fmt.Println(line)
line = ""
}
if line == "" {
line = word
} else {
line = line + " " + word
total += 1
}
}
fmt.Print(line)
}
repeatCount := maxWidth - total - len(suffix)
if repeatCount < 0 {
repeatCount = 0
}
terminate := strings.Repeat(" ", repeatCount) + suffix
fmt.Println(terminate)
}
func printTransactionLine(amount float64, name, prefix, suffix string, maxWidth int) {
segment := fmt.Sprintf("%s$%.2f", prefix, math.Abs(amount))
spacer := strings.Repeat(" ", (maxWidth/4)-len(segment))
printWithMaxWidth(maxWidth, suffix, segment, spacer, fmt.Sprintf("| %s", name))
}
func printOutput(logDay LogDay) {
fullWidth := getTerminalWidth()
if fullWidth > 120 {
fullWidth = 120
}
border := strings.Repeat("=", fullWidth)
divider := strings.Repeat("-", fullWidth-1) + "|"
fmt.Println(border)
printWithMaxWidth(fullWidth, "|", logDay.Date.Format("Jan 02 2006"))
fmt.Println(divider)
outString := fmt.Sprint(logDay.Date.Format("Jan 02 2006"))
rear_spacer := strings.Repeat(" ", width-len(outString))
fmt.Printf("%s%s |\n", outString, rear_spacer)
for _, v := range logDay.StartingBalance { for _, v := range logDay.StartingBalance {
spacer := strings.Repeat(" ", 19-len(fmt.Sprintf("%.2f", v.Balance))) printTransactionLine(v.Balance, v.Name, "", "|", fullWidth)
outString := fmt.Sprintf("$%.2f%s| %s", v.Balance, spacer, v.Name)
rear_spacer := strings.Repeat(" ", width-len(outString))
fmt.Printf("%s%s |\n", outString, rear_spacer)
} }
for _, charge := range logDay.transactions() { for _, charge := range logDay.transactions() {
spacer := strings.Repeat(" ", 18-len(fmt.Sprintf("%.2f", math.Abs(charge.Amount)))) prefix := "-"
outString := "" if charge.Amount > 0 {
if charge.Amount <= 0 { prefix = "+"
outString = "-"
} else {
outString = "+"
} }
outString = fmt.Sprintf("%s$%.2f%s| %s", outString, math.Abs(charge.Amount), spacer, charge.Name)
if charge.Delayed { if charge.Delayed {
late_tag := fmt.Sprintf("(%s - %s)", "Late", charge.OriginDate.Format("Jan 02 2006")) suffix := fmt.Sprintf("(%s - %s) |", "Late", charge.OriginDate.Format("Jan 02 2006"))
rear_spacer := strings.Repeat(" ", width-len(late_tag)-len(outString)) printTransactionLine(charge.Amount, charge.Name, prefix, suffix, fullWidth)
fmt.Printf("%s%s%s |\n", outString, rear_spacer, late_tag)
} else if charge.Date != charge.OriginDate { } else if charge.Date != charge.OriginDate {
weekend_tag := fmt.Sprintf("(%s - %s)", "Weekend", charge.OriginDate.Format("Jan 02 2006")) suffix := fmt.Sprintf("(%s - %s) |", "Weekend", charge.OriginDate.Format("Jan 02 2006"))
rear_spacer := strings.Repeat(" ", width-len(weekend_tag)-len(outString)) printTransactionLine(charge.Amount, charge.Name, prefix, suffix, fullWidth)
fmt.Printf("%s%s%s |\n", outString, rear_spacer, weekend_tag)
} else { } else {
rear_spacer := strings.Repeat(" ", width-len(outString)) printTransactionLine(charge.Amount, charge.Name, prefix, "|", fullWidth)
fmt.Printf("%s%s |\n", outString, rear_spacer)
} }
} }
fmt.Println(divider2)
fmt.Println(divider)
for _, v := range logDay.EndingBalance { for _, v := range logDay.EndingBalance {
spacer := strings.Repeat(" ", 19-len(fmt.Sprintf("%.2f", v.Balance))) printTransactionLine(v.Balance, v.Name, "", "|", fullWidth)
outString := fmt.Sprintf("$%.2f%s| %s", v.Balance, spacer, v.Name)
rear_spacer := strings.Repeat(" ", width-len(outString))
fmt.Printf("%s%s |\n", outString, rear_spacer)
} }
fmt.Println(divider + "\n") fmt.Println(border + "\n")
} }
func main() { func main() {