-
Notifications
You must be signed in to change notification settings - Fork 0
/
view.go
319 lines (268 loc) · 5.7 KB
/
view.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package main
import (
"fmt"
"log"
"strconv"
"github.com/charmbracelet/bubbles/textinput"
"github.com/charmbracelet/lipgloss"
tea "github.com/charmbracelet/bubbletea"
)
var normalStyle lipgloss.Style
var focusStyle lipgloss.Style
var deleteStyle lipgloss.Style
var inputStyle lipgloss.Style
type stageModel interface {
Update(*globalModel, tea.Msg) tea.Cmd
View() string
}
type globalModel struct {
stageModel stageModel
menu *menuModel
shows []Show
}
type showChoice struct {
name string
deleted bool
}
type menuModel struct {
choices []showChoice
cursor int
}
func newMenu(s []Show) menuModel {
choices := []showChoice{{name: "Add show"}}
for _, show := range s {
choices = append(choices, showChoice{name: show.name})
}
cursor := 1
if len(s) == 0 {
cursor = 0
}
return menuModel{
choices: choices,
cursor: cursor,
}
}
func (m *menuModel) Update(g *globalModel, msg tea.Msg) tea.Cmd {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "q":
return tea.Quit
case "j", "down":
m.cursor++
if m.cursor >= len(m.choices) {
m.cursor = 0
}
case "k", "up":
m.cursor--
if m.cursor < 0 {
m.cursor = len(m.choices) - 1
}
case "enter":
if m.cursor == 0 {
model := newAddModel()
g.stageModel = &model
} else {
model := newResultModel(&g.shows[m.cursor-1])
g.stageModel = &model
}
case "d":
if m.cursor > 0 {
err := g.shows[m.cursor-1].delete()
if err != nil {
log.Fatal(err)
}
m.choices[m.cursor].deleted = true
}
case "u":
if m.cursor > 0 {
err := g.shows[m.cursor-1].undelete()
if err != nil {
log.Fatal(err)
}
m.choices[m.cursor].deleted = false
}
}
}
return nil
}
func (m *menuModel) View() string {
s := "What do you want to do?\n\n"
for i, choice := range m.choices {
style := normalStyle
cursor := " "
if m.cursor == i {
cursor = ">"
style = focusStyle
}
if choice.deleted {
s += fmt.Sprintf(
deleteStyle.Render("%s (deleted: %s, press u to undo)"),
cursor, choice.name,
)
} else {
s += fmt.Sprintf(
style.Render("%s %s"),
cursor, choice.name,
)
}
s += "\n"
}
s += "\n(Press q to quit, d to delete)"
return s
}
type addModel struct {
textInput textinput.Model
message string
show Show
}
func newAddModel() addModel {
ti := textinput.New()
ti.Focus()
return addModel{
textInput: ti,
message: "Show name: ",
}
}
func (m *addModel) Update(g *globalModel, msg tea.Msg) tea.Cmd {
var cmd tea.Cmd = nil
m.textInput, cmd = m.textInput.Update(msg)
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "enter":
cmd = m.handleShowInput(g)
}
}
return cmd
}
func (m *addModel) handleShowInput(g *globalModel) tea.Cmd {
var err error
if m.textInput.Value() == "" {
return nil
}
if m.show.name == "" {
m.show.name = m.textInput.Value()
m.message = fmt.Sprintf("Season count: ")
} else if m.show.seasonCount == 0 {
m.show.seasonCount, err = strconv.Atoi(m.textInput.Value())
if err != nil {
return nil
}
m.message = fmt.Sprintf("Season 1 length: ")
} else if len(m.show.seasonLengths) < m.show.seasonCount {
l, err := strconv.Atoi(m.textInput.Value())
if err != nil {
return nil
}
m.show.seasonLengths = append(m.show.seasonLengths, l)
if len(m.show.seasonLengths) == m.show.seasonCount {
err = m.show.save()
if err != nil {
log.Fatal(err)
return nil
}
g.stageModel = g.menu
return readShows
} else {
m.message = fmt.Sprintf("Season %d length: ", len(m.show.seasonLengths)+1)
}
}
m.textInput.Reset()
return nil
}
func (m *addModel) View() string {
s := m.message + "\n\n"
return s + inputStyle.Render(m.textInput.View())
}
type resultModel struct {
cursor int
show *Show
episode Episode
error error
}
func newResultModel(s *Show) resultModel {
episode, err := s.getEpisode()
return resultModel{
cursor: 0,
show: s,
episode: episode,
error: err,
}
}
func (m *resultModel) Update(g *globalModel, msg tea.Msg) tea.Cmd {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "j", "down":
m.cursor = (m.cursor + 1) % 2
case "k", "up":
m.cursor = (m.cursor + 1) % 2
case "q":
return tea.Quit
case "enter":
if m.cursor == 0 {
m.show.watchEpisode(m.episode)
}
g.stageModel = g.menu
}
}
return nil
}
func (m *resultModel) View() string {
if m.error != nil {
return "Error: \n\n" + m.error.Error()
}
s := fmt.Sprintf(
"Season %d, episode %d\n\n",
m.episode.season,
m.episode.episode,
)
for i, choice := range []string{"Watched", "Later"} {
style := normalStyle
cursor := " "
if m.cursor == i {
style = focusStyle
cursor = ">"
}
s += fmt.Sprintf(style.Render("%s %s"), cursor, choice)
s += "\n"
}
return s
}
type showsLoaded struct {
shows []Show
}
func initialModel() globalModel {
normalStyle = lipgloss.NewStyle().Width(24).AlignHorizontal(lipgloss.Center)
focusStyle = normalStyle.Copy().Bold(true).Foreground(lipgloss.Color("#CF3476"))
deleteStyle = normalStyle.Copy().Italic(true)
inputStyle = focusStyle.Copy().AlignHorizontal(lipgloss.Left)
menu := newMenu([]Show{})
return globalModel{
stageModel: &menu,
menu: &menu,
}
}
func (m globalModel) Init() tea.Cmd {
return connectDB
}
func (m globalModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd = nil
switch msg := msg.(type) {
case showsLoaded:
m.shows = msg.shows
menu := newMenu(m.shows)
m.menu = &menu
m.stageModel = m.menu
case tea.KeyMsg:
if msg.String() == "ctrl+c" {
return m, tea.Quit
}
}
cmd = m.stageModel.Update(&m, msg)
return m, cmd
}
func (m globalModel) View() string {
return m.stageModel.View()
}