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

import (
	"fmt"
	"os"

	"github.com/delucks/go-subsonic"
	"github.com/rivo/tview"
)

type app struct {
	tv     *tview.Application
	pages  *tview.Pages
	header *tview.TextView
	footer *tview.TextView
	cfg    *Config

	sub *subsonic.Client
}

func Run(cfg *Config) {
	a := &app{
		cfg: cfg,
	}

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

	a.header = tview.NewTextView().
		SetRegions(true).
		SetChangedFunc(func() {
			a.tv.Draw()
		}).
		SetHighlightedFunc(func(added, removed, remaining []string) {
			hl := added[0]
			cur, _ := a.pages.GetFrontPage()

			if hl != cur {
				switchToPage(a, hl)
			}
		})
	fmt.Fprintf(a.header, `["artists"]F1: Artists[""] | ["playlists"]F2: Playlists[""] | ["config"]F3: Configuration[""]`)

	a.pages.AddPage("config", configPage(a), true, false)
	a.pages.AddPage("artists", artistsPage(a), true, false)

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

	switchToPage(a, "config")
	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 switchToPage(a *app, name string) {
	if name == "artists" {
		a.pages.SwitchToPage("artists")
		a.header.Highlight("artists")
	} else if name == "playlists" {
		a.pages.SwitchToPage("playlists")
		a.header.Highlight("playlists")
	} else if name == "config" {
		a.pages.SwitchToPage("config")
		a.header.Highlight("config")
	}
}