github.com/wtfutil/wtf@v0.43.0/modules/newrelic/widget.go (about) 1 package newrelic 2 3 import ( 4 "sort" 5 6 "github.com/rivo/tview" 7 "github.com/wtfutil/wtf/utils" 8 "github.com/wtfutil/wtf/view" 9 ) 10 11 type Widget struct { 12 view.MultiSourceWidget 13 view.TextWidget 14 15 Clients []*Client2 16 17 settings *Settings 18 } 19 20 func NewWidget(tviewApp *tview.Application, redrawChan chan bool, pages *tview.Pages, settings *Settings) *Widget { 21 widget := Widget{ 22 MultiSourceWidget: view.NewMultiSourceWidget(settings.Common, "applicationID", "applicationIDs"), 23 TextWidget: view.NewTextWidget(tviewApp, redrawChan, pages, settings.Common), 24 25 settings: settings, 26 } 27 28 widget.initializeKeyboardControls() 29 30 for _, id := range utils.ToInts(widget.settings.applicationIDs) { 31 widget.Clients = append(widget.Clients, NewClient(widget.settings.apiKey, id)) 32 } 33 34 sort.Slice(widget.Clients, func(i, j int) bool { 35 return widget.Clients[i].applicationId < widget.Clients[j].applicationId 36 }) 37 38 widget.SetDisplayFunction(widget.Refresh) 39 40 return &widget 41 } 42 43 /* -------------------- Exported Functions -------------------- */ 44 45 func (widget *Widget) Refresh() { 46 widget.Redraw(widget.content) 47 } 48 49 /* -------------------- Unexported Functions -------------------- */ 50 51 /* -------------------- Unexported Functions -------------------- */ 52 53 func (widget *Widget) currentData() *Client2 { 54 if len(widget.Clients) == 0 { 55 return nil 56 } 57 58 if widget.Idx < 0 || widget.Idx >= len(widget.Clients) { 59 return nil 60 } 61 62 return widget.Clients[widget.Idx] 63 }