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
logrus.Debugf("\n\nITERATION %d\n\n", tl.Iteration)
if tl.Iteration == 0 {
for _, date := range tl.dates() {
logrus.Debugf("Processing Log Day %s", date.String())
logDay := tl.LogDays[date]
logDay.StartingBalance = make([]Account, len(endBalance))
logDay.EndingBalance = make([]Account, len(endBalance))
copy(logDay.StartingBalance, endBalance)
copy(logDay.EndingBalance, endBalance)
categorized := categorizeTransactions(logDay.Transactions)
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
for _, date := range tl.dates() {
logrus.Debugf("Processing Log Day %s", date.String())
logDay := tl.LogDays[date]
logDay.StartingBalance = make([]Account, len(endBalance))
logDay.EndingBalance = make([]Account, len(endBalance))
copy(logDay.StartingBalance, endBalance)
copy(logDay.EndingBalance, endBalance)
categorized := categorizeTransactions(logDay.Transactions)
if processTransactionsByID(tl, categorized["credits"]) {
shifted = true
}
} else {
for _, date := range tl.dates() {
logrus.Debugf("Processing Log Day %s", date.String())
logDay := tl.LogDays[date]
logDay.StartingBalance = make([]Account, len(endBalance))
logDay.EndingBalance = make([]Account, len(endBalance))
copy(logDay.StartingBalance, endBalance)
copy(logDay.EndingBalance, endBalance)
categorized := categorizeTransactions(logDay.Transactions)
logrus.Debugln(categorized)
logrus.Debugln(logDay.Transactions)
if processTransactionsByID(tl, categorized["credits"]) {
shifted = true
}
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)
}
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
}
func printOutput(logDay LogDay) {
func getTerminalWidth() int {
cmd := exec.Command("stty", "size")
cmd.Stdin = os.Stdin
out, err := cmd.Output()
@@ -571,58 +549,96 @@ func printOutput(logDay LogDay) {
}
widthStr := string(out[:len(out)-1])
sttyOut := strings.Split(widthStr, " ")
fullWidth, err := strconv.Atoi(sttyOut[1])
fullWidth = fullWidth / 2
width := fullWidth - 2
width, err := strconv.Atoi(sttyOut[1])
if err != nil {
logrus.Fatal(err)
}
return width
}
delim := strings.Repeat("=", fullWidth)
divider := delim
delim2 := strings.Repeat("-", fullWidth-1)
divider2 := delim2 + "|"
func printWithMaxWidth(maxWidth int, suffix string, strs ...string) {
total := 0
whiteSpace := regexp.MustCompile(`^\s+$`)
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 {
spacer := strings.Repeat(" ", 19-len(fmt.Sprintf("%.2f", v.Balance)))
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)
printTransactionLine(v.Balance, v.Name, "", "|", fullWidth)
}
for _, charge := range logDay.transactions() {
spacer := strings.Repeat(" ", 18-len(fmt.Sprintf("%.2f", math.Abs(charge.Amount))))
outString := ""
if charge.Amount <= 0 {
outString = "-"
} else {
outString = "+"
prefix := "-"
if charge.Amount > 0 {
prefix = "+"
}
outString = fmt.Sprintf("%s$%.2f%s| %s", outString, math.Abs(charge.Amount), spacer, charge.Name)
if charge.Delayed {
late_tag := fmt.Sprintf("(%s - %s)", "Late", charge.OriginDate.Format("Jan 02 2006"))
rear_spacer := strings.Repeat(" ", width-len(late_tag)-len(outString))
fmt.Printf("%s%s%s |\n", outString, rear_spacer, late_tag)
suffix := fmt.Sprintf("(%s - %s) |", "Late", charge.OriginDate.Format("Jan 02 2006"))
printTransactionLine(charge.Amount, charge.Name, prefix, suffix, fullWidth)
} else if charge.Date != charge.OriginDate {
weekend_tag := fmt.Sprintf("(%s - %s)", "Weekend", charge.OriginDate.Format("Jan 02 2006"))
rear_spacer := strings.Repeat(" ", width-len(weekend_tag)-len(outString))
fmt.Printf("%s%s%s |\n", outString, rear_spacer, weekend_tag)
suffix := fmt.Sprintf("(%s - %s) |", "Weekend", charge.OriginDate.Format("Jan 02 2006"))
printTransactionLine(charge.Amount, charge.Name, prefix, suffix, fullWidth)
} else {
rear_spacer := strings.Repeat(" ", width-len(outString))
fmt.Printf("%s%s |\n", outString, rear_spacer)
printTransactionLine(charge.Amount, charge.Name, prefix, "|", fullWidth)
}
}
fmt.Println(divider2)
fmt.Println(divider)
for _, v := range logDay.EndingBalance {
spacer := strings.Repeat(" ", 19-len(fmt.Sprintf("%.2f", v.Balance)))
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)
printTransactionLine(v.Balance, v.Name, "", "|", fullWidth)
}
fmt.Println(divider + "\n")
fmt.Println(border + "\n")
}
func main() {