-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtuiInput.go
330 lines (304 loc) · 10.2 KB
/
tuiInput.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
320
321
322
323
324
325
326
327
328
329
330
package main
import (
ui "github.com/gizak/termui"
"strings"
"time"
)
func switchToSelectedPodcastScreen(configuration *Configuration) {
podcasts := getCurrentPagePodcasts()
cursor := getCurrentCursorPosition()
if cursor >= len(podcasts) || cursor < 0 {
return
}
currentSelectedPodcast = podcasts[cursor]
currentScreen = PodcastDetail
// reset cursor
setCurrentCursorPosition(0)
}
func searchPodcastsFromTui(configuration *Configuration) {
// search TODO use go()
setCurrentCursorPosition(0)
var err error
searchString := strings.Replace(userTextBuffer, "\n", "", -1)
searchString = strings.Trim(searchString, "\n\t")
searchString = strings.Replace(searchString, " ", "+", -1) //replace spaces with plus to not break everything
currentPodcastsInBuffers[currentScreen], err = searchItunes(searchString)
if err != nil {
userTextBuffer = "error searching! " + err.Error()
}
// If we have podcasts, transition to normal mode. Otherwise, stay in insert
if len(currentPodcastsInBuffers[currentScreen].([]Podcast)) > 0 {
searchFailed = false
currentMode = Normal
} else {
searchFailed = true
currentMode = Insert
}
}
func doNothingWithInput(configuration *Configuration) {}
func enterPressedHome(configuration *Configuration) {
// TODO refactor into two functions
if currentMode == Normal {
switchToSelectedPodcastScreen(configuration)
}
}
func enterPressedSearch(configuration *Configuration) {
if currentMode == Normal {
switchToSelectedPodcastScreen(configuration)
} else {
searchPodcastsFromTui(configuration)
}
}
func enterPressedDownloaded(configuration *Configuration) {
podcasts := getCurrentPagePodcastEpisodes()
cursor := getCurrentCursorPosition()
if cursor >= len(podcasts) || cursor < 0 {
return
}
SetPlaying(podcasts[cursor].StorageLocation)
sendPlayerMessage(PlayerPlay)
}
func enterPressedPodcastDetail(configuration *Configuration) {
podcasts := getCurrentPagePodcastEpisodes()
cursor := getCurrentCursorPosition()
if cursor >= len(podcasts) || cursor < 0 {
return
}
if podcastIsDownloaded(configuration, &podcasts[cursor]) {
location := getPodcastLocation(configuration, podcasts[cursor])
if location != "" {
SetPlaying(location)
sendPlayerMessage(PlayerPlay)
}
return
}
// TODO fix this race condition/bad configuration management.
go func() {
_ = downloadPodcast(configuration, currentSelectedPodcast, podcasts[cursor])
}()
}
func actionPressedSearch(configuration *Configuration) {
subscribedKey := -1
podcasts := getCurrentPagePodcasts()
cursor := getCurrentCursorPosition()
if cursor >= len(podcasts) || cursor < 0 {
return
}
selectedPodcast := podcasts[cursor]
// check if it's already part of the configuration
for key, value := range configuration.Subscribed {
if selectedPodcast.ArtistName == value.ArtistName && selectedPodcast.CollectionName == value.CollectionName {
subscribedKey = key
}
}
if subscribedKey != -1 {
configuration.Subscribed = append(configuration.Subscribed[:subscribedKey], configuration.Subscribed[subscribedKey+1:]...)
} else {
configuration.Subscribed = append(configuration.Subscribed, selectedPodcast) //now subscribe by adding it to the subscribed list
}
}
func deletePodcastSelectedByCursor(configuration *Configuration) {
cursor := getCurrentCursorPosition()
podcasts := getCurrentPagePodcastEpisodes()
if cursor >= len(podcasts) || cursor < 0 {
return
}
deleteDownloadedPodcast(configuration, podcasts[cursor])
// reset cursor if needed
if cursor == len(podcasts)-1 {
setCurrentCursorPosition(len(podcasts) - 2)
}
}
func escapePressedPodcastDetail(configuration *Configuration) {
transitionScreen(leftTransitions, currentScreen)
}
func upPressedGeneric(configuration *Configuration) {
cursor := getCurrentCursorPosition()
if cursor > 0 {
setCurrentCursorPosition(cursor - 1)
}
}
func upPressedSearch(configuration *Configuration) {
cursor := getCurrentCursorPosition()
if cursor > 0 {
setCurrentCursorPosition(cursor - 1)
} else {
currentMode = Insert
}
}
func downPressedGeneric(configuration *Configuration) {
cursor := getCurrentCursorPosition()
if cursor < currentListSize-1 {
setCurrentCursorPosition(cursor + 1)
}
}
func downPressedSearch(configuration *Configuration) {
cursor := getCurrentCursorPosition()
if currentMode == Insert && len(userTextBuffer) > 0 {
searchPodcastsFromTui(configuration)
} else if cursor < currentListSize-1 {
setCurrentCursorPosition(cursor + 1)
}
}
func searchPressedHome(configuration *Configuration) {
userTextBuffer = ""
currentMode = Insert
}
func searchPressedSearch(configuration *Configuration) {
userTextBuffer = ""
currentMode = Insert
}
func searchPressedDownloaded(configuration *Configuration) {
userTextBuffer = ""
currentMode = Insert
}
func deletePressedHome(configuration *Configuration) {
subscribedKey := -1
podcasts := getCurrentPagePodcasts()
cursor := getCurrentCursorPosition()
if cursor >= len(podcasts) || cursor < 0 {
return
}
for key, value := range configuration.Subscribed {
if podcasts[cursor].ArtistName == value.ArtistName && podcasts[cursor].CollectionName == value.CollectionName {
subscribedKey = key
}
}
if subscribedKey == -1 {
return
}
// reset cursor if needed
if cursor == len(configuration.Subscribed)-1 {
setCurrentCursorPosition(len(configuration.Subscribed) - 2)
}
configuration.Subscribed = append(configuration.Subscribed[:subscribedKey], configuration.Subscribed[subscribedKey+1:]...)
}
func handleEventsGlobal(configuration *Configuration, event ui.Event) bool {
if event.ID == "<Enter>" {
enterPressed[currentScreen](configuration)
} else if event.ID == "<Escape>" {
// reset mode on Escape as well as possibly do an action
currentMode = Normal
escapePressed[currentScreen](configuration)
} else if (event.ID == configuration.LeftKeybind && currentMode == Normal) || event.ID == "<Left>" {
transitionScreen(leftTransitions, currentScreen)
} else if (event.ID == configuration.RightKeybind && currentMode == Normal) || event.ID == "<Right>" {
transitionScreen(rightTransitions, currentScreen)
} else if (event.ID == configuration.UpKeybind && currentMode == Normal) || event.ID == "<Up>" {
upPressed[currentScreen](configuration)
} else if (event.ID == configuration.DownKeybind && currentMode == Normal) || event.ID == "<Down>" {
downPressed[currentScreen](configuration)
} else if (event.ID == configuration.FastForward && currentMode == Normal) || event.ID == "<Previous>" {
sendPlayerMessage(PlayerFastForward)
} else if (event.ID == configuration.Rewind && currentMode == Normal) || event.ID == "<Next>" {
sendPlayerMessage(PlayerRewind)
} else {
// nothing matches, return false
return false
}
// matches, return true
return true
}
// handleKeyboard handles the keyboard, then returns if a screen redraw is required
func handleKeyboard(configuration *Configuration, event ui.Event) bool {
if handleEventsGlobal(configuration, event) {
// handled
} else if currentMode == Insert {
if event.ID == "<Backspace>" || event.ID == "<Delete>" || event.ID == "C-8>" {
if len(userTextBuffer) > 0 {
userTextBuffer = userTextBuffer[:len(userTextBuffer)-1]
}
} else if event.ID == "<Space>" {
userTextBuffer += " "
} else if event.ID == "<Tab>" {
userTextBuffer += " "
} else if len(event.ID) > 0 && string([]rune(event.ID)[0]) == "<" {
// do not do anything if it's one of the other control keys
} else {
userTextBuffer += event.ID
}
} else if event.ID == configuration.PlayKeybind {
TogglePlayerState()
return true
} else if event.ID == configuration.SearchKeybind {
searchPressed[currentScreen](configuration)
} else if event.ID == configuration.ActionKeybind {
actionPressed[currentScreen](configuration)
} else if event.ID == configuration.DeleteKeybind {
deletePressed[currentScreen](configuration)
prepareDrawPage[currentScreen](configuration)
}
return false
}
func handleMouse(configuration *Configuration, event ui.Event) {
if event.ID == "<MouseWheelUp>" {
upPressed[currentScreen](configuration)
} else if event.ID == "<MouseWheelDown>" {
downPressed[currentScreen](configuration)
} else if event.ID == "<MouseLeft>" {
enterPressed[currentScreen](configuration)
} else if event.ID == "<MouseRight>" && currentScreen == PodcastDetail {
transitionScreen(leftTransitions, currentScreen)
}
}
func tuiMainLoop(configuration *Configuration) {
width := ui.TermWidth()
height := ui.TermHeight()
prepareDrawPage[currentScreen](configuration)
ui.Render(drawPage[currentScreen](configuration, width, height)...)
uiEvents := ui.PollEvents()
ticker := time.NewTicker(time.Second).C
for {
select {
case e := <-uiEvents:
{
savedScreen := currentScreen
if e.Type == ui.KeyboardEvent {
if e.ID == "<C-c>" {
goto exitMainLoop
} else {
if handleKeyboard(configuration, e) {
ui.Render(drawPage[currentScreen](configuration, width, height)...)
}
}
} else if e.Type == ui.MouseEvent {
handleMouse(configuration, e)
ui.Render(drawPage[currentScreen](configuration, width, height)...)
} else if e.Type == ui.ResizeEvent {
payload := e.Payload.(ui.Resize)
width = payload.Width
height = payload.Height
ui.Render(drawPage[currentScreen](configuration, width, height)...)
}
// refresh screen after keyboard input or redraw screen entirely + reset state if we have changed screens
if savedScreen != currentScreen {
prepareDrawPage[currentScreen](configuration)
// reset modes
if currentScreen == Search && (savedScreen != PodcastDetail) {
currentMode = Insert
} else {
currentMode = Normal
}
// save last screen
previousScreen = savedScreen
}
ui.Render(drawPage[currentScreen](configuration, width, height)...)
}
case <-ticker:
// refresh player or reset if needed
if GetPlayerState() == PlayerPlay || downloadInProgress() {
ui.Render(producePlayerWidget(configuration, width, height))
} else if GetPlayerState() == PlayerStop {
sendPlayerMessage(PlayerNothingPlaying)
ui.Render(producePlayerWidget(configuration, width, height))
}
if refreshPage[currentScreen] != nil {
ui.Render(refreshPage[currentScreen](configuration, width, height)...)
}
}
}
exitMainLoop:
DisposePlayer()
// save on exit
writeConfig(configuration)
}