-
Notifications
You must be signed in to change notification settings - Fork 9
/
main_menu.go
233 lines (207 loc) · 7.02 KB
/
main_menu.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package scenes
import (
"log/slog"
"strings"
"time"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/spf13/viper"
"github.com/satisfactorymodding/ficsit-cli/tea/components"
"github.com/satisfactorymodding/ficsit-cli/tea/scenes/errors"
"github.com/satisfactorymodding/ficsit-cli/tea/scenes/installation"
"github.com/satisfactorymodding/ficsit-cli/tea/scenes/keys"
"github.com/satisfactorymodding/ficsit-cli/tea/scenes/mods"
"github.com/satisfactorymodding/ficsit-cli/tea/scenes/profile"
"github.com/satisfactorymodding/ficsit-cli/tea/utils"
)
var _ tea.Model = (*mainMenu)(nil)
type mainMenu struct {
root components.RootModel
list list.Model
error *components.ErrorComponent
banner string
}
const logoBanner = `
███████╗██╗ ██████╗███████╗██╗████████╗
██╔════╝██║██╔════╝██╔════╝██║╚══██╔══╝
█████╗ ██║██║ ███████╗██║ ██║
██╔══╝ ██║██║ ╚════██║██║ ██║
██║ ██║╚██████╗███████║██║ ██║
╚═╝ ╚═╝ ╚═════╝╚══════╝╚═╝ ╚═╝`
func NewMainMenu(root components.RootModel) tea.Model {
trimmedBanner := strings.TrimSpace(logoBanner)
var finalBanner strings.Builder
for i, s := range strings.Split(trimmedBanner, "\n") {
if i > 0 {
finalBanner.WriteRune('\n')
}
foreground := utils.LogoForegroundStyles[i]
background := utils.LogoBackgroundStyles[i]
for _, c := range s {
if c == '█' {
finalBanner.WriteString(foreground.Render("█"))
} else if c != ' ' {
finalBanner.WriteString(background.Render(string(c)))
} else {
finalBanner.WriteRune(c)
}
}
}
model := mainMenu{
root: root,
banner: finalBanner.String(),
}
items := []list.Item{
utils.SimpleItem[mainMenu]{
ItemTitle: "Installations",
Activate: func(msg tea.Msg, currentModel mainMenu) (tea.Model, tea.Cmd) {
newModel := installation.NewInstallations(root, currentModel)
return newModel, newModel.Init()
},
},
utils.SimpleItem[mainMenu]{
ItemTitle: "Toggle Vanilla",
Activate: func(msg tea.Msg, currentModel mainMenu) (tea.Model, tea.Cmd) {
if currentModel.root.GetCurrentInstallation() != nil {
currentModel.root.GetCurrentInstallation().Vanilla = !currentModel.root.GetCurrentInstallation().Vanilla
}
return currentModel, nil
},
},
utils.SimpleItem[mainMenu]{
ItemTitle: "Profiles",
Activate: func(msg tea.Msg, currentModel mainMenu) (tea.Model, tea.Cmd) {
newModel := profile.NewProfiles(root, currentModel)
return newModel, newModel.Init()
},
},
utils.SimpleItem[mainMenu]{
ItemTitle: "All Mods",
Activate: func(msg tea.Msg, currentModel mainMenu) (tea.Model, tea.Cmd) {
newModel := mods.NewMods(root, currentModel)
return newModel, newModel.Init()
},
},
utils.SimpleItem[mainMenu]{
ItemTitle: "Installed Mods",
Activate: func(msg tea.Msg, currentModel mainMenu) (tea.Model, tea.Cmd) {
newModel := mods.NewInstalledMods(root, currentModel)
return newModel, newModel.Init()
},
},
utils.SimpleItem[mainMenu]{
ItemTitle: "Update Mods",
Activate: func(msg tea.Msg, currentModel mainMenu) (tea.Model, tea.Cmd) {
newModel := mods.NewUpdateMods(root, currentModel)
return newModel, newModel.Init()
},
},
utils.SimpleItem[mainMenu]{
ItemTitle: "Apply Changes",
Activate: func(msg tea.Msg, currentModel mainMenu) (tea.Model, tea.Cmd) {
if err := root.GetGlobal().Save(); err != nil {
slog.Error(errors.ErrorFailedAddMod, slog.Any("err", err))
errorComponent, cmd := components.NewErrorComponent(err.Error(), time.Second*5)
currentModel.error = errorComponent
return currentModel, cmd
}
newModel := NewApply(root, currentModel)
return newModel, newModel.Init()
},
},
utils.SimpleItem[mainMenu]{
ItemTitle: "Save",
Activate: func(msg tea.Msg, currentModel mainMenu) (tea.Model, tea.Cmd) {
if err := root.GetGlobal().Save(); err != nil {
slog.Error(errors.ErrorFailedAddMod, slog.Any("err", err))
errorComponent, cmd := components.NewErrorComponent(err.Error(), time.Second*5)
currentModel.error = errorComponent
return currentModel, cmd
}
return nil, nil
},
},
utils.SimpleItem[mainMenu]{
ItemTitle: "Exit",
Activate: func(msg tea.Msg, currentModel mainMenu) (tea.Model, tea.Cmd) {
return nil, tea.Quit
},
},
}
model.list = list.New(items, utils.NewItemDelegate(), root.Size().Width, root.Size().Height-root.Height())
model.list.SetShowStatusBar(false)
model.list.SetFilteringEnabled(false)
model.list.Title = "Main Menu"
model.list.Styles = utils.ListStyles
model.list.AdditionalShortHelpKeys = func() []key.Binding {
return []key.Binding{
key.NewBinding(key.WithKeys("enter"), key.WithHelp("enter", "select")),
}
}
return model
}
func (m mainMenu) Init() tea.Cmd {
return nil
}
func (m mainMenu) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch keypress := msg.String(); keypress {
case keys.KeyControlC:
return m, tea.Quit
case "q":
return m, tea.Quit
case keys.KeyEnter:
i, ok := m.list.SelectedItem().(utils.SimpleItem[mainMenu])
if ok {
if i.Activate != nil {
newModel, cmd := i.Activate(msg, m)
if newModel != nil || cmd != nil {
if newModel == nil {
newModel = m
}
return newModel, cmd
}
return m, nil
}
}
return m, nil
default:
var cmd tea.Cmd
m.list, cmd = m.list.Update(msg)
return m, cmd
}
case tea.WindowSizeMsg:
top, right, bottom, left := lipgloss.NewStyle().Margin(2, 2).GetMargin()
m.list.SetSize(msg.Width-left-right, msg.Height-top-bottom)
m.root.SetSize(msg)
case components.ErrorComponentTimeoutMsg:
m.error = nil
}
return m, nil
}
func (m mainMenu) View() string {
header := m.root.View()
banner := lipgloss.NewStyle().Margin(2, 0, 0, 2).Render(m.banner)
commit := viper.GetString("commit")
if len(commit) > 8 {
commit = commit[:8]
}
version := "\n"
version += utils.LabelStyle.Render("Version: ")
version += viper.GetString("version") + " - " + commit
header = lipgloss.JoinVertical(lipgloss.Left, version, header)
totalHeight := lipgloss.Height(header) + len(m.list.Items()) + lipgloss.Height(banner) + 5
if totalHeight < m.root.Size().Height {
header = lipgloss.JoinVertical(lipgloss.Left, banner, header)
}
if m.error != nil {
err := m.error.View()
m.list.SetSize(m.list.Width(), m.root.Size().Height-lipgloss.Height(header)-lipgloss.Height(err))
return lipgloss.JoinVertical(lipgloss.Left, header, err, m.list.View())
}
m.list.SetSize(m.list.Width(), m.root.Size().Height-lipgloss.Height(header))
return lipgloss.JoinVertical(lipgloss.Left, header, m.list.View())
}