forked from wildeyedskies/stmp
-
Notifications
You must be signed in to change notification settings - Fork 7
/
page_search.go
338 lines (306 loc) · 8.95 KB
/
page_search.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
331
332
333
334
335
336
337
338
// Copyright 2023 The STMPS Authors
// SPDX-License-Identifier: GPL-3.0-only
package main
import (
"fmt"
"sort"
"strings"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
"github.com/spezifisch/stmps/logger"
"github.com/spezifisch/stmps/subsonic"
)
type SearchPage struct {
Root *tview.Flex
AddToPlaylistModal tview.Primitive
columnsFlex *tview.Flex
artistList *tview.List
albumList *tview.List
songList *tview.List
searchField *tview.InputField
artists []*subsonic.Artist
albums []*subsonic.Album
songs []*subsonic.SubsonicEntity
// external refs
ui *Ui
logger logger.LoggerInterface
}
func (ui *Ui) createSearchPage() *SearchPage {
searchPage := SearchPage{
ui: ui,
logger: ui.logger,
}
// artist list
searchPage.artistList = tview.NewList().
ShowSecondaryText(false)
searchPage.artistList.Box.
SetTitle(" artist matches ").
SetTitleAlign(tview.AlignLeft).
SetBorder(true)
// album list
searchPage.albumList = tview.NewList().
ShowSecondaryText(false)
searchPage.albumList.Box.
SetTitle(" album matches ").
SetTitleAlign(tview.AlignLeft).
SetBorder(true)
// song list
searchPage.songList = tview.NewList().
ShowSecondaryText(false)
searchPage.songList.Box.
SetTitle(" song matches ").
SetTitleAlign(tview.AlignLeft).
SetBorder(true)
// search bar
searchPage.searchField = tview.NewInputField().
SetLabel("search:").
SetFieldBackgroundColor(tcell.ColorBlack).
SetDoneFunc(func(key tcell.Key) {
searchPage.aproposFocus()
})
searchPage.columnsFlex = tview.NewFlex().SetDirection(tview.FlexColumn).
AddItem(searchPage.artistList, 0, 1, true).
AddItem(searchPage.albumList, 0, 1, false).
AddItem(searchPage.songList, 0, 1, false)
searchPage.Root = tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(searchPage.columnsFlex, 0, 1, true).
AddItem(searchPage.searchField, 1, 1, false)
searchPage.artistList.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyLeft:
ui.app.SetFocus(searchPage.songList)
return nil
case tcell.KeyRight:
ui.app.SetFocus(searchPage.albumList)
return nil
case tcell.KeyEnter:
if len(searchPage.artists) != 0 {
idx := searchPage.artistList.GetCurrentItem()
searchPage.addArtistToQueue(searchPage.artists[idx])
return nil
}
return event
}
switch event.Rune() {
case 'a':
if len(searchPage.artists) != 0 {
idx := searchPage.artistList.GetCurrentItem()
searchPage.logger.Printf("artistList adding (%d) %s", idx, searchPage.artists[idx].Name)
searchPage.addArtistToQueue(searchPage.artists[idx])
return nil
}
return event
case '/':
searchPage.ui.app.SetFocus(searchPage.searchField)
return nil
}
return event
})
searchPage.albumList.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyLeft:
ui.app.SetFocus(searchPage.artistList)
return nil
case tcell.KeyRight:
ui.app.SetFocus(searchPage.songList)
return nil
case tcell.KeyEnter:
if len(searchPage.albums) != 0 {
idx := searchPage.albumList.GetCurrentItem()
searchPage.addAlbumToQueue(searchPage.albums[idx])
return nil
}
return event
}
switch event.Rune() {
case 'a':
if len(searchPage.albums) != 0 {
idx := searchPage.albumList.GetCurrentItem()
searchPage.logger.Printf("albumList adding (%d) %s", idx, searchPage.albums[idx].Name)
searchPage.addAlbumToQueue(searchPage.albums[idx])
return nil
}
return event
case '/':
searchPage.ui.app.SetFocus(searchPage.searchField)
return nil
}
return event
})
searchPage.songList.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyLeft:
ui.app.SetFocus(searchPage.albumList)
return nil
case tcell.KeyRight:
ui.app.SetFocus(searchPage.artistList)
return nil
case tcell.KeyEnter:
if len(searchPage.artists) != 0 {
idx := searchPage.songList.GetCurrentItem()
ui.addSongToQueue(searchPage.songs[idx])
ui.queuePage.UpdateQueue()
return nil
}
return event
}
switch event.Rune() {
case 'a':
if len(searchPage.artists) != 0 {
idx := searchPage.songList.GetCurrentItem()
ui.addSongToQueue(searchPage.songs[idx])
ui.queuePage.updateQueue()
return nil
}
return event
case '/':
searchPage.ui.app.SetFocus(searchPage.searchField)
return nil
}
return event
})
search := make(chan string, 5)
searchPage.searchField.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyUp, tcell.KeyESC:
searchPage.aproposFocus()
case tcell.KeyEnter:
search <- ""
searchPage.artistList.Clear()
searchPage.artists = make([]*subsonic.Artist, 0)
searchPage.albumList.Clear()
searchPage.albums = make([]*subsonic.Album, 0)
searchPage.songList.Clear()
searchPage.songs = make([]*subsonic.SubsonicEntity, 0)
queryStr := searchPage.searchField.GetText()
search <- queryStr
default:
return event
}
return nil
})
go searchPage.search(search)
return &searchPage
}
func (s *SearchPage) search(search chan string) {
var query string
var artOff, albOff, songOff int
more := make(chan bool, 5)
for {
// quit searching if we receive an interrupt
select {
case query = <-search:
artOff = 0
albOff = 0
songOff = 0
s.logger.Printf("searching for %q [%d, %d, %d]", query, artOff, albOff, songOff)
for len(more) > 0 {
<-more
}
if query == "" {
continue
}
case <-more:
s.logger.Printf("fetching more %q [%d, %d, %d]", query, artOff, albOff, songOff)
}
res, err := s.ui.connection.Search(query, artOff, albOff, songOff)
if err != nil {
s.logger.PrintError("SearchPage.search", err)
return
}
// Quit searching if there are no more results
if len(res.SearchResults.Artist) == 0 &&
len(res.SearchResults.Album) == 0 &&
len(res.SearchResults.Song) == 0 {
continue
}
query = strings.ToLower(query)
s.ui.app.QueueUpdate(func() {
for _, artist := range res.SearchResults.Artist {
if strings.Contains(strings.ToLower(artist.Name), query) {
s.artistList.AddItem(tview.Escape(artist.Name), "", 0, nil)
s.artists = append(s.artists, &artist)
}
}
s.artistList.Box.SetTitle(fmt.Sprintf(" artist matches (%d) ", len(s.artists)))
for _, album := range res.SearchResults.Album {
if strings.Contains(strings.ToLower(album.Name), query) {
s.albumList.AddItem(tview.Escape(album.Name), "", 0, nil)
s.albums = append(s.albums, &album)
}
}
s.albumList.Box.SetTitle(fmt.Sprintf(" album matches (%d) ", len(s.albums)))
for _, song := range res.SearchResults.Song {
if strings.Contains(strings.ToLower(song.Title), query) {
s.songList.AddItem(tview.Escape(song.Title), "", 0, nil)
s.songs = append(s.songs, &song)
}
}
s.songList.Box.SetTitle(fmt.Sprintf(" song matches (%d) ", len(s.songs)))
})
artOff += len(res.SearchResults.Artist)
albOff += len(res.SearchResults.Album)
songOff += len(res.SearchResults.Song)
more <- true
}
}
func (s *SearchPage) addArtistToQueue(entity subsonic.Ider) {
response, err := s.ui.connection.GetArtist(entity.ID())
if err != nil {
s.logger.Printf("addArtistToQueue: GetArtist %s -- %s", entity.ID(), err.Error())
return
}
artistId := response.Artist.Id
for _, album := range response.Artist.Album {
response, err = s.ui.connection.GetAlbum(album.Id)
if err != nil {
s.logger.Printf("error getting album %s while adding artist to queue", album.Id)
return
}
sort.Sort(response.Album.Song)
// We make sure we add only albums who's artists match the artist
// being added; this prevents collection albums with many different
// artists that show up in the Album column having _all_ of the songs
// on the album -- even ones that don't match the artist -- from
// being added when the user adds an album from the search results.
for _, e := range response.Album.Song {
// Depending on the server implementation, the server may or may not
// respond with a list of artists. If either the Artist field matches,
// or the artist name is in a list of artists, then we add the song.
if e.ArtistId == artistId {
s.ui.addSongToQueue(&e)
continue
}
for _, art := range e.Artists {
if art.Id == artistId {
s.ui.addSongToQueue(&e)
break
}
}
}
}
s.ui.queuePage.UpdateQueue()
}
func (s *SearchPage) addAlbumToQueue(entity subsonic.Ider) {
response, err := s.ui.connection.GetAlbum(entity.ID())
if err != nil {
s.logger.Printf("addToQueue: GetMusicDirectory %s -- %s", entity.ID(), err.Error())
return
}
sort.Sort(response.Album.Song)
for _, e := range response.Album.Song {
s.ui.addSongToQueue(&e)
}
s.ui.queuePage.UpdateQueue()
}
func (s *SearchPage) aproposFocus() {
if len(s.artists) != 0 {
s.ui.app.SetFocus(s.artistList)
} else if len(s.albums) != 0 {
s.ui.app.SetFocus(s.albumList)
} else if len(s.songs) != 0 {
s.ui.app.SetFocus(s.songList)
} else {
s.ui.app.SetFocus(s.artistList)
}
}