github.com/wtfutil/wtf@v0.43.0/modules/weatherservices/arpansagovau/widget.go (about) 1 package arpansagovau 2 3 import ( 4 "fmt" 5 6 "github.com/rivo/tview" 7 "github.com/wtfutil/wtf/view" 8 ) 9 10 type Widget struct { 11 view.TextWidget 12 13 location *location 14 lastError error 15 settings *Settings 16 } 17 18 func NewWidget(tviewApp *tview.Application, redrawChan chan bool, settings *Settings) *Widget { 19 locationData, err := getLocationData(settings.city) 20 widget := Widget{ 21 TextWidget: view.NewTextWidget(tviewApp, redrawChan, nil, settings.Common), 22 23 location: locationData, 24 lastError: err, 25 settings: settings, 26 } 27 28 widget.View.SetWrap(true) 29 30 return &widget 31 } 32 33 func (widget *Widget) content() (string, string, bool) { 34 35 locationData, err := getLocationData(widget.settings.city) 36 widget.location = locationData 37 widget.lastError = err 38 39 if widget.lastError != nil { 40 return widget.CommonSettings().Title, fmt.Sprintf("Err: %s", widget.lastError.Error()), true 41 } 42 43 return widget.CommonSettings().Title, formatLocationData(widget.location), true 44 } 45 46 func (widget *Widget) Refresh() { 47 widget.Redraw(widget.content) 48 } 49 50 func formatLocationData(location *location) string { 51 var level string 52 var color string 53 var content string 54 55 if location.name == "" { 56 return "[red]No data?" 57 } 58 59 if location.status != "ok" { 60 content = "[red]Data unavailable for " 61 content += location.name 62 return content 63 } 64 65 switch { 66 case location.index < 2.5: 67 color = "[green]" 68 level = " (LOW)" 69 case location.index >= 2.5 && location.index < 5.5: 70 color = "[yellow]" 71 level = " (MODERATE)" 72 case location.index >= 5.5 && location.index < 7.5: 73 color = "[orange]" 74 level = " (HIGH)" 75 case location.index >= 7.5 && location.index < 10.5: 76 color = "[red]" 77 level = " (VERY HIGH)" 78 case location.index >= 10.5: 79 color = "[fuchsia]" 80 level = " (EXTREME)" 81 } 82 83 content = "Location: " 84 content += location.name 85 content += "\nUV index: " 86 content += color 87 content += fmt.Sprintf("%.2f", location.index) 88 content += level 89 content += "[white]\nLocal time: " 90 content += location.time 91 content += " " 92 content += location.date 93 content += "\nDetector status: " 94 content += location.status 95 96 return content 97 }