/
widget.go
71 lines (55 loc) · 1.86 KB
/
widget.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
package spotify
import (
"fmt"
"strings"
"time"
"github.com/rivo/tview"
"github.com/wtfutil/spotigopher/spotigopher"
"github.com/wtfutil/wtf/utils"
"github.com/wtfutil/wtf/view"
)
// A Widget represents a Spotify widget
type Widget struct {
view.TextWidget
client spotigopher.SpotifyClient
settings *Settings
spotigopher.Info
}
// NewWidget creates a new instance of a widget
func NewWidget(tviewApp *tview.Application, redrawChan chan bool, pages *tview.Pages, settings *Settings) *Widget {
widget := Widget{
TextWidget: view.NewTextWidget(tviewApp, redrawChan, pages, settings.Common),
Info: spotigopher.Info{},
client: spotigopher.NewClient(),
settings: settings,
}
widget.settings.RefreshInterval = 5 * time.Second
widget.initializeKeyboardControls()
widget.View.SetWrap(true)
widget.View.SetWordWrap(true)
return &widget
}
func (w *Widget) refreshSpotifyInfos() error {
info, err := w.client.GetInfo()
w.Info = info
return err
}
func (w *Widget) Refresh() {
w.Redraw(w.createOutput)
}
func (w *Widget) createOutput() (string, string, bool) {
var content string
err := w.refreshSpotifyInfos()
if err != nil {
content = err.Error()
} else {
labelColor := w.settings.colors.label
textColor := w.settings.colors.text
artist := strings.Join(w.Info.Artist, ", ")
content = utils.CenterText(fmt.Sprintf("[%s]Now %v [%s]\n", labelColor, w.Info.Status, textColor), w.CommonSettings().Width)
content += utils.CenterText(fmt.Sprintf("[%s]Title:[%s] %v\n ", labelColor, textColor, w.Info.Title), w.CommonSettings().Width)
content += utils.CenterText(fmt.Sprintf("[%s]Artist:[%s] %v\n", labelColor, textColor, artist), w.CommonSettings().Width)
content += utils.CenterText(fmt.Sprintf("[%s]%v:[%s] %v\n", labelColor, w.Info.TrackNumber, textColor, w.Info.Album), w.CommonSettings().Width)
}
return w.CommonSettings().Title, content, true
}