aboutsummaryrefslogtreecommitdiff
path: root/src/app.go
blob: 173132c5baf26e2251c89de9e05b3e9beb6f3197 (plain)
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
package src

import (
	"fmt"
	"math/rand"
	"os"
	"time"

	"git.sixfoisneuf.fr/termsonic/music"
	"github.com/delucks/go-subsonic"
	"github.com/gdamore/tcell/v2"
	"github.com/rivo/tview"
)

type app struct {
	// General GUI
	tv               *tview.Application
	pages            *tview.Pages
	headerSections   *tview.TextView
	headerNowPlaying *tview.TextView
	footer           *tview.TextView
	cfg              *Config

	// Artists page
	artistsLoaded bool
	artistsTree   *tview.TreeView
	songsList     *tview.List
	currentSongs  []*subsonic.Child

	// Play queue page
	playQueueList *tview.List

	// Playlist page
	playlistsLoaded bool
	playlistsList   *tview.List
	playlistSongs   *tview.List
	allPlaylists    []*subsonic.Playlist
	currentPlaylist *subsonic.Playlist

	// Subsonic variables
	sub       *subsonic.Client
	playQueue *music.Queue
}

func Run(cfg *Config) {
	a := &app{
		cfg:       cfg,
		playQueue: music.NewQueue(nil),
	}

	a.tv = tview.NewApplication()
	a.pages = tview.NewPages()
	a.footer = tview.NewTextView().
		SetDynamicColors(true)

	a.pages.SetBorder(true)
	a.pages.AddPage("config", a.configPage(), true, false)
	a.pages.AddPage("artists", a.artistsPage(), true, false)
	a.pages.AddPage("playqueue", a.queuePage(), true, false)
	a.pages.AddPage("playlists", a.playlistsPage(), true, false)

	mainLayout := tview.NewFlex().
		SetDirection(tview.FlexRow).
		AddItem(a.buildHeader(), 1, 1, false).
		AddItem(a.pages, 0, 3, true).
		AddItem(a.footer, 1, 1, false)

	if testConfig(a.cfg) != nil {
		a.switchToPage("config")
	} else {
		a.sub, _ = buildSubsonicClient(a.cfg)
		a.playQueue.SetClient(a.sub)

		fmt.Printf("Loading artists...")
		if err := a.refreshArtists(); err != nil {
			fmt.Println("ERR")
			a.alert("Loading artists: %v", err)
		} else {
			fmt.Println("OK")
			a.artistsLoaded = true
		}

		fmt.Printf("Loading playlists...")
		if err := a.refreshPlaylists(); err != nil {
			fmt.Println("ERR")
			a.alert("Loading playlists: %v", err)
		} else {
			fmt.Println("OK")
			a.playlistsLoaded = true
		}

		a.switchToPage("artists")
	}

	// Keyboard shortcuts
	a.tv.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
		switch event.Key() {
		case tcell.KeyF1:
			a.switchToPage("artists")
			return nil
		case tcell.KeyF2:
			a.switchToPage("playqueue")
			return nil
		case tcell.KeyF3:
			a.switchToPage("playlists")
			return nil
		case tcell.KeyF4:
			a.switchToPage("config")
			return nil
		}

		switch event.Rune() {
		case 'q':
			a.tv.Stop()
		}

		return event
	})

	if err := a.tv.SetRoot(mainLayout, true).EnableMouse(true).SetFocus(mainLayout).Run(); err != nil {
		fmt.Printf("Error running termsonic: %v", err)
		os.Exit(1)
	}
}

func (a *app) switchToPage(name string) {
	switch name {
	case "artists":
		if a.sub == nil {
			return
		}
		if !a.artistsLoaded {
			if err := a.refreshArtists(); err != nil {
				a.alert("Error: %v", err)
			}
			a.artistsLoaded = true
		}
		a.pages.SwitchToPage("artists")
		a.headerSections.Highlight("artists")
		a.tv.SetFocus(a.artistsTree)
		a.pages.SetBorder(false)
	case "playqueue":
		if a.sub == nil {
			return
		}
		a.pages.SwitchToPage("playqueue")
		a.headerSections.Highlight("playqueue")
		a.tv.SetFocus(a.playQueueList)
		a.pages.SetBorder(true)
	case "playlists":
		if a.sub == nil {
			return
		}
		if !a.playlistsLoaded {
			if err := a.refreshPlaylists(); err != nil {
				a.alert("Error: %v", err)
			}
			a.playlistsLoaded = true
		}
		a.pages.SwitchToPage("playlists")
		a.headerSections.Highlight("playlists")
		a.tv.SetFocus(a.playlistsList)
		a.pages.SetBorder(false)
	case "config":
		a.pages.SwitchToPage("config")
		a.headerSections.Highlight("config")
		a.pages.SetBorder(true)
	}

	a.updateFooter()
}

func randomize(t []*subsonic.Child) []*subsonic.Child {
	t2 := make([]*subsonic.Child, len(t))
	copy(t2, t)

	rand.Seed(time.Now().UnixNano())
	rand.Shuffle(len(t2), func(i, j int) { t2[i], t2[j] = t2[j], t2[i] })

	return t2
}