aboutsummaryrefslogtreecommitdiff
path: root/src/app.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/app.go')
-rw-r--r--src/app.go72
1 files changed, 72 insertions, 0 deletions
diff --git a/src/app.go b/src/app.go
new file mode 100644
index 0000000..c18c1f5
--- /dev/null
+++ b/src/app.go
@@ -0,0 +1,72 @@
1package src
2
3import (
4 "fmt"
5 "os"
6
7 "github.com/delucks/go-subsonic"
8 "github.com/rivo/tview"
9)
10
11type app struct {
12 tv *tview.Application
13 pages *tview.Pages
14 header *tview.TextView
15 footer *tview.TextView
16 cfg *Config
17
18 sub *subsonic.Client
19}
20
21func Run(cfg *Config) {
22 a := &app{
23 cfg: cfg,
24 }
25
26 a.tv = tview.NewApplication()
27 a.pages = tview.NewPages()
28 a.footer = tview.NewTextView()
29
30 a.header = tview.NewTextView().
31 SetRegions(true).
32 SetChangedFunc(func() {
33 a.tv.Draw()
34 }).
35 SetHighlightedFunc(func(added, removed, remaining []string) {
36 hl := added[0]
37 cur, _ := a.pages.GetFrontPage()
38
39 if hl != cur {
40 switchToPage(a, hl)
41 }
42 })
43 fmt.Fprintf(a.header, `["artists"]F1: Artists[""] | ["playlists"]F2: Playlists[""] | ["config"]F3: Configuration[""]`)
44
45 a.pages.AddPage("config", configPage(a), true, false)
46 a.pages.AddPage("artists", artistsPage(a), true, false)
47
48 mainLayout := tview.NewFlex().
49 SetDirection(tview.FlexRow).
50 AddItem(a.header, 0, 1, false).
51 AddItem(a.pages, 0, 3, true).
52 AddItem(a.footer, 0, 1, false)
53
54 switchToPage(a, "config")
55 if err := a.tv.SetRoot(mainLayout, true).EnableMouse(true).SetFocus(mainLayout).Run(); err != nil {
56 fmt.Printf("Error running termsonic: %v", err)
57 os.Exit(1)
58 }
59}
60
61func switchToPage(a *app, name string) {
62 if name == "artists" {
63 a.pages.SwitchToPage("artists")
64 a.header.Highlight("artists")
65 } else if name == "playlists" {
66 a.pages.SwitchToPage("playlists")
67 a.header.Highlight("playlists")
68 } else if name == "config" {
69 a.pages.SwitchToPage("config")
70 a.header.Highlight("config")
71 }
72}