github.com/wtfutil/wtf@v0.43.0/modules/spotify/widget.go (about) 1 package spotify 2 3 import ( 4 "fmt" 5 "strings" 6 "time" 7 8 "github.com/rivo/tview" 9 "github.com/wtfutil/spotigopher/spotigopher" 10 "github.com/wtfutil/wtf/utils" 11 "github.com/wtfutil/wtf/view" 12 ) 13 14 // A Widget represents a Spotify widget 15 type Widget struct { 16 view.TextWidget 17 18 client spotigopher.SpotifyClient 19 settings *Settings 20 spotigopher.Info 21 } 22 23 // NewWidget creates a new instance of a widget 24 func NewWidget(tviewApp *tview.Application, redrawChan chan bool, pages *tview.Pages, settings *Settings) *Widget { 25 widget := Widget{ 26 TextWidget: view.NewTextWidget(tviewApp, redrawChan, pages, settings.Common), 27 28 Info: spotigopher.Info{}, 29 client: spotigopher.NewClient(), 30 31 settings: settings, 32 } 33 34 widget.settings.RefreshInterval = 5 * time.Second 35 36 widget.initializeKeyboardControls() 37 38 widget.View.SetWrap(true) 39 widget.View.SetWordWrap(true) 40 41 return &widget 42 } 43 44 func (w *Widget) refreshSpotifyInfos() error { 45 info, err := w.client.GetInfo() 46 w.Info = info 47 return err 48 } 49 50 func (w *Widget) Refresh() { 51 w.Redraw(w.createOutput) 52 } 53 54 func (w *Widget) createOutput() (string, string, bool) { 55 var content string 56 err := w.refreshSpotifyInfos() 57 if err != nil { 58 content = err.Error() 59 } else { 60 labelColor := w.settings.colors.label 61 textColor := w.settings.colors.text 62 63 artist := strings.Join(w.Info.Artist, ", ") 64 65 content = utils.CenterText(fmt.Sprintf("[%s]Now %v [%s]\n", labelColor, w.Info.Status, textColor), w.CommonSettings().Width) 66 content += utils.CenterText(fmt.Sprintf("[%s]Title:[%s] %v\n ", labelColor, textColor, w.Info.Title), w.CommonSettings().Width) 67 content += utils.CenterText(fmt.Sprintf("[%s]Artist:[%s] %v\n", labelColor, textColor, artist), w.CommonSettings().Width) 68 content += utils.CenterText(fmt.Sprintf("[%s]%v:[%s] %v\n", labelColor, w.Info.TrackNumber, textColor, w.Info.Album), w.CommonSettings().Width) 69 } 70 return w.CommonSettings().Title, content, true 71 }