-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
242 lines (215 loc) · 5.86 KB
/
main.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
package main
import (
"fmt"
"os"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/jessevdk/go-flags"
"github.com/muesli/termenv"
)
type focus int
const (
jellyfin focus = iota
bottombar
)
type screen int
const (
mainScreen screen = iota
downloadScreen
)
type model struct {
jellyfinViewModel jellyfinViewModel
bottombarModel bottombarModel
downloadModel downloadModel
focus focus
currentScreen screen
width int
height int
config *Config
lastRequest int
}
type infoMsg struct {
info string
}
func (m *model) InitModel() {
m.config = getConfig()
m.jellyfinViewModel.config = m.config
m.jellyfinViewModel.InitModel()
m.jellyfinViewModel.isActive = true
m.bottombarModel.InitModel()
m.bottombarModel.config = m.config
if args.Download {
m.currentScreen = downloadScreen
m.focus = jellyfin
m.downloadModel = downloadModel{width: m.width, height: m.height, config: m.config}
m.downloadModel.InitModel()
}
}
func (m model) Init() tea.Cmd {
if args.APIKey {
return tea.Batch(m.jellyfinViewModel.Init(), sendMessage(incorrectAPIKeyMsg("")))
}
if args.APIEndPoint {
return tea.Batch(m.jellyfinViewModel.Init(), sendMessage(incorrectAPIEndPointMsg("")))
}
if args.UserId {
return tea.Batch(m.jellyfinViewModel.Init(), sendMessage(incorrectUserIdMsg("")))
}
if args.Download {
return tea.Batch(m.jellyfinViewModel.Init(), m.downloadModel.Init())
}
return m.jellyfinViewModel.Init()
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if m.currentScreen == mainScreen {
var cmds = make([]tea.Cmd, 0)
shouldBePassed := true
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c":
return m, tea.Quit
case "tab":
if m.focus == jellyfin {
m.focus = bottombar
m.jellyfinViewModel.isActive = false
m.bottombarModel.isActive = true
} else {
m.jellyfinViewModel.isActive = true
m.bottombarModel.isActive = false
m.focus = jellyfin
}
return m, nil
default:
switch m.focus {
case jellyfin:
//var cmd tea.Cmd
//_ /*m.jellyfinViewModel*/, cmd := m.jellyfinViewModel.Update(msg)
return m.callJellyfinUpdate(msg)
case bottombar:
return m.callBottombarUpdate(msg)
}
}
case tea.WindowSizeMsg:
//h, v := docStyle.GetFrameSize()
m.width = msg.Width
m.height = msg.Height
jModel, jCmds := m.jellyfinViewModel.Update(tea.WindowSizeMsg{Width: m.width, Height: m.height - 2})
cmds = append(cmds, jCmds)
m.jellyfinViewModel = jModel
shouldBePassed = false
case infoMsg:
return m.callBottombarUpdate(msg)
case incorrectAPIEndPointMsg, incorrectUserIdMsg, incorrectAPIKeyMsg:
var str string
switch msg := msg.(type) {
case incorrectAPIEndPointMsg:
str = string(msg)
case incorrectUserIdMsg:
str = string(msg)
case incorrectAPIKeyMsg:
str = string(msg)
}
m.jellyfinViewModel.loadingMsg = str
m.focus = bottombar
m.bottombarModel.isActive = true
m.bottombarModel.buttonsActive = false
m.jellyfinViewModel.isActive = false
return m.callBottombarUpdate(msg)
case reloadItemsMsg:
m.focus = jellyfin
m.bottombarModel.isActive = false
m.jellyfinViewModel.isActive = true
m.callJellyfinUpdate(msg)
default:
if m.focus == jellyfin {
return m.callJellyfinUpdate(msg)
} else {
return m.callBottombarUpdate(msg)
}
case inputDoneMsg:
if msg.id != DownloadLocation {
m.focus = jellyfin
}
return m.callBottombarUpdate(msg)
case inputCancelMsg:
if len(m.jellyfinViewModel.lists) == 0 {
m.bottombarModel.buttonsActive = true
return m, sendMessage(reloadItemsMsg{})
}
case buttonPressedMsg:
switch ButtonId(msg) {
case DownloadAll:
m.currentScreen = downloadScreen
m.focus = jellyfin
m.downloadModel = downloadModel{width: m.width, height: m.height, config: m.config}
m.downloadModel.InitModel()
return m, m.downloadModel.Init()
default:
return m.callBottombarUpdate(msg)
}
}
if shouldBePassed {
jModel, jCmds := m.jellyfinViewModel.Update(msg)
cmds = append(cmds, jCmds)
m.jellyfinViewModel = jModel
}
return m, tea.Batch(cmds...)
} else {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "esc":
m.downloadModel.CancelAll()
m.currentScreen = mainScreen
m.jellyfinViewModel.isActive = true
m.bottombarModel.isActive = false
return m, m.jellyfinViewModel.UpdateItems
default:
return m.callDownloadUpdate(msg)
}
case tea.WindowSizeMsg:
m.width = msg.Width
m.height = msg.Height
m.jellyfinViewModel, _ = m.jellyfinViewModel.Update(tea.WindowSizeMsg{Width: m.width, Height: m.height - 2})
return m.callDownloadUpdate(msg)
default:
return m.callDownloadUpdate(msg)
}
}
}
func (m model) View() string {
if m.currentScreen == mainScreen {
jellyfinView := m.jellyfinViewModel.View()
bottombarView := m.bottombarModel.View()
return lipgloss.JoinVertical(lipgloss.Left, jellyfinView, bottombarView)
} else {
return m.downloadModel.View()
}
}
var program *tea.Program
type ProgramArgs struct {
Download bool `short:"d" long:"download" description:"Start downloading selected items"`
APIKey bool `short:"k" long:"apikey" description:"Ask for API key"`
UserId bool `short:"u" long:"userid" description:"Ask for UserId"`
APIEndPoint bool `short:"e" long:"endpoint" description:"Ask for API Endpoint"`
}
var args ProgramArgs = ProgramArgs{}
func main() {
lipgloss.SetHasDarkBackground(termenv.HasDarkBackground())
_, err := flags.Parse(&args)
if err != nil {
if flags.WroteHelp(err) {
os.Exit(0)
} else {
panic(err)
}
}
m := model{}
m.InitModel()
program = tea.NewProgram(m, tea.WithAltScreen())
if err := program.Start(); err != nil {
fmt.Println("Error running program:", err)
program.Kill()
}
}