github.com/wtfutil/wtf@v0.43.0/modules/cryptocurrency/cryptolive/widget.go (about) 1 package cryptolive 2 3 import ( 4 "fmt" 5 "sync" 6 7 "github.com/rivo/tview" 8 "github.com/wtfutil/wtf/modules/cryptocurrency/cryptolive/price" 9 "github.com/wtfutil/wtf/modules/cryptocurrency/cryptolive/toplist" 10 "github.com/wtfutil/wtf/view" 11 ) 12 13 // Widget define wtf widget to register widget later 14 type Widget struct { 15 view.TextWidget 16 17 priceWidget *price.Widget 18 toplistWidget *toplist.Widget 19 settings *Settings 20 } 21 22 // NewWidget Make new instance of widget 23 func NewWidget(tviewApp *tview.Application, redrawChan chan bool, settings *Settings) *Widget { 24 widget := Widget{ 25 TextWidget: view.NewTextWidget(tviewApp, redrawChan, nil, settings.Common), 26 27 priceWidget: price.NewWidget(settings.priceSettings), 28 toplistWidget: toplist.NewWidget(settings.toplistSettings), 29 settings: settings, 30 } 31 32 widget.priceWidget.RefreshInterval = widget.RefreshInterval() 33 widget.toplistWidget.RefreshInterval = widget.RefreshInterval() 34 35 return &widget 36 } 37 38 /* -------------------- Exported Functions -------------------- */ 39 40 // Refresh & update after interval time 41 func (widget *Widget) Refresh() { 42 var wg sync.WaitGroup 43 44 wg.Add(2) 45 widget.priceWidget.Refresh(&wg) 46 widget.toplistWidget.Refresh(&wg) 47 wg.Wait() 48 49 widget.Redraw(widget.content) 50 } 51 52 /* -------------------- Unexported Functions -------------------- */ 53 54 func (widget *Widget) content() (string, string, bool) { 55 str := "" 56 str += widget.priceWidget.Result 57 str += widget.toplistWidget.Result 58 59 return widget.CommonSettings().Title, fmt.Sprintf("\n%s", str), false 60 }