diff options
author | Simon Garrelou <simon.garrelou@gmail.com> | 2022-12-10 22:22:32 +0100 |
---|---|---|
committer | Simon Garrelou <simon.garrelou@gmail.com> | 2022-12-10 22:22:32 +0100 |
commit | 08182123b8165c8c8dc55d1fefc643f5c5c47035 (patch) | |
tree | 631891c25d5f294820e5b49a9bf467b7688d98c1 /src/page_playlists.go | |
parent | 96e0b7c9c2b058b479079fa41f10cf3513e95359 (diff) | |
download | termsonic-08182123b8165c8c8dc55d1fefc643f5c5c47035.tar.gz termsonic-08182123b8165c8c8dc55d1fefc643f5c5c47035.zip |
Add playlist support
Diffstat (limited to 'src/page_playlists.go')
-rw-r--r-- | src/page_playlists.go | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/src/page_playlists.go b/src/page_playlists.go new file mode 100644 index 0000000..303a9a7 --- /dev/null +++ b/src/page_playlists.go | |||
@@ -0,0 +1,98 @@ | |||
1 | package src | ||
2 | |||
3 | import ( | ||
4 | "fmt" | ||
5 | |||
6 | "github.com/gdamore/tcell/v2" | ||
7 | "github.com/rivo/tview" | ||
8 | ) | ||
9 | |||
10 | func (a *app) playlistsPage() tview.Primitive { | ||
11 | flex := tview.NewFlex().SetDirection(tview.FlexColumn) | ||
12 | |||
13 | a.playlistsList = tview.NewList(). | ||
14 | SetMainTextColor(tcell.ColorRed). | ||
15 | SetHighlightFullLine(true). | ||
16 | ShowSecondaryText(false) | ||
17 | a.playlistsList.SetBorder(true).SetBorderAttributes(tcell.AttrDim) | ||
18 | |||
19 | a.playlistSongs = tview.NewList(). | ||
20 | SetHighlightFullLine(true). | ||
21 | ShowSecondaryText(false) | ||
22 | a.playlistSongs.SetBorder(true).SetBorderAttributes(tcell.AttrDim) | ||
23 | |||
24 | // Change the left-right keys to switch between the panels | ||
25 | a.playlistsList.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { | ||
26 | if event.Key() == tcell.KeyLeft || event.Key() == tcell.KeyRight { | ||
27 | a.tv.SetFocus(a.playlistSongs) | ||
28 | a.updateFooter() | ||
29 | return nil | ||
30 | } | ||
31 | return event | ||
32 | }) | ||
33 | a.playlistSongs.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { | ||
34 | if event.Key() == tcell.KeyLeft || event.Key() == tcell.KeyRight { | ||
35 | a.tv.SetFocus(a.playlistsList) | ||
36 | a.updateFooter() | ||
37 | return nil | ||
38 | } | ||
39 | return event | ||
40 | }) | ||
41 | |||
42 | // Setup e & n keybinds | ||
43 | a.setupMusicControlKeys(flex.Box) | ||
44 | |||
45 | flex.AddItem(a.playlistsList, 0, 1, false) | ||
46 | flex.AddItem(a.playlistSongs, 0, 1, false) | ||
47 | |||
48 | return flex | ||
49 | } | ||
50 | |||
51 | func (a *app) refreshPlaylists() error { | ||
52 | playlists, err := a.sub.GetPlaylists(nil) | ||
53 | if err != nil { | ||
54 | return err | ||
55 | } | ||
56 | |||
57 | a.allPlaylists = playlists | ||
58 | |||
59 | a.playlistsList.Clear() | ||
60 | for _, pl := range playlists { | ||
61 | id := pl.ID | ||
62 | a.playlistsList.AddItem(pl.Name, "", 0, func() { | ||
63 | a.loadPlaylist(id) | ||
64 | a.tv.SetFocus(a.playlistSongs) | ||
65 | a.updateFooter() | ||
66 | }) | ||
67 | } | ||
68 | |||
69 | a.playlistsList.SetCurrentItem(0) | ||
70 | |||
71 | return nil | ||
72 | } | ||
73 | |||
74 | func (a *app) loadPlaylist(id string) error { | ||
75 | a.playlistSongs.Clear() | ||
76 | pl, err := a.sub.GetPlaylist(id) | ||
77 | if err != nil { | ||
78 | return err | ||
79 | } | ||
80 | |||
81 | a.currentPlaylist = pl | ||
82 | |||
83 | for _, s := range a.currentPlaylist.Entry { | ||
84 | a.playlistSongs.AddItem(fmt.Sprintf("%s - %s", s.Title, s.Artist), "", 0, func() { | ||
85 | sel := a.playlistSongs.GetCurrentItem() | ||
86 | a.playQueue.Clear() | ||
87 | for _, s := range a.currentPlaylist.Entry[sel:] { | ||
88 | a.playQueue.Append(s) | ||
89 | } | ||
90 | |||
91 | if err := a.playQueue.Play(); err != nil { | ||
92 | a.alert("Error: %v", err) | ||
93 | } | ||
94 | }) | ||
95 | } | ||
96 | |||
97 | return nil | ||
98 | } | ||