-
Notifications
You must be signed in to change notification settings - Fork 0
/
profiles.go
135 lines (104 loc) · 3.71 KB
/
profiles.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"fmt"
"strconv"
"time"
lib "github.com/charles-m-knox/finance-planner-lib"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
func setStatusNoChanges() {
FP.ProfileStatusText.SetText(fmt.Sprintf("[gray] %v", FP.T["ProfilesPageStatusTextNoChanges"]))
}
func getActiveProfileText(profile Profile) string {
if FP.SelectedProfile != nil && FP.SelectedProfile.Name == profile.Name {
return fmt.Sprintf("[white::bu]%v %v%v", profile.Name, FP.T["ProfilesPageProfileOpenMarker"], Reset)
}
return profile.Name
}
// populateProfilesPage clears out the profile list and proceeds to populate it
// with the current profiles in the config, including handlers for changing
// the FP.SelectedProfile.
func populateProfilesPage() {
FP.ProfileList.Clear()
for i := range FP.Config.Profiles {
profile := &(FP.Config.Profiles[i])
FP.ProfileList.AddItem(getActiveProfileText(*profile), "", 0, func() {
FP.SelectedProfile = profile
populateProfilesPage()
getTransactionsTable()
FP.App.SetFocus(FP.TransactionsTable)
})
}
}
// This should only ever be called once, upon application startup.
//
// returns a simple flex view with two columns:
// - a list of profiles (left side)
// - a quick summary of bills / stats for the highlighted profile (right side).
func getProfilesPage() *tview.Flex {
FP.ProfileList = tview.NewList()
FP.ProfileList.SetBorder(true)
FP.ProfileList.ShowSecondaryText(false).
SetSelectedBackgroundColor(tcell.NewRGBColor(50, 50, 50)).
SetSelectedTextColor(tcell.ColorWhite).
SetTitle(FP.T["ProfilesPageTitle"])
FP.ProfileStatusText = tview.NewTextView()
FP.ProfileStatusText.SetBorder(true)
FP.ProfileStatusText.SetDynamicColors(true)
setStatusNoChanges()
profilesLeftSide := tview.NewFlex().SetDirection(tview.FlexRow)
profilesLeftSide.AddItem(FP.ProfileList, 0, 1, true).
AddItem(FP.ProfileStatusText, 3, 0, true)
FP.TransactionsTable = tview.NewTable().SetFixed(1, 1)
FP.TransactionsInputField = tview.NewInputField()
FP.TransactionsTable.SetBorder(true)
FP.TransactionsInputField.SetBorder(true)
FP.TransactionsInputField.SetFieldBackgroundColor(tcell.ColorBlack)
FP.TransactionsInputField.SetLabel(fmt.Sprintf(
"%v%v%v",
FP.Colors["TransactionsInputFieldPassive"],
FP.T["ProfilesPageInputFieldAppearsHere"],
Reset,
))
FP.TransactionsSortMap = getTransactionsSortMap()
FP.WeekdaysMap = getWeekdaysMap()
populateProfilesPage()
getTransactionsTable()
transactionsPage := tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(FP.TransactionsTable, 0, 1, false).
AddItem(FP.TransactionsInputField, 3, 0, false)
return tview.NewFlex().SetDirection(tview.FlexColumn).
AddItem(profilesLeftSide, 0, 1, true).
AddItem(transactionsPage, 0, 10, false)
}
// sets sensible default values for the currently selected profile, if they are
// not defined. If there is no FP.SelectedProfile, this will do nothing.
func setSelectedProfileDefaults() {
if FP.SelectedProfile == nil {
return
}
now := time.Now()
yr := now.Add(time.Hour * 24 * 365)
if FP.SelectedProfile.StartYear == "" {
FP.SelectedProfile.StartYear = strconv.Itoa(now.Year())
}
if FP.SelectedProfile.StartMonth == "" {
FP.SelectedProfile.StartMonth = strconv.Itoa(int(now.Month()))
}
if FP.SelectedProfile.StartDay == "" {
FP.SelectedProfile.StartDay = strconv.Itoa(now.Day())
}
if FP.SelectedProfile.EndYear == "" {
FP.SelectedProfile.EndYear = strconv.Itoa(yr.Year())
}
if FP.SelectedProfile.EndMonth == "" {
FP.SelectedProfile.EndMonth = strconv.Itoa(int(yr.Month()))
}
if FP.SelectedProfile.EndDay == "" {
FP.SelectedProfile.EndDay = strconv.Itoa(yr.Day())
}
if FP.SelectedProfile.StartingBalance == "" {
FP.SelectedProfile.StartingBalance = lib.FormatAsCurrency(50000)
}
}