1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
wtf/modules/covid/widget.go
David Bouchare 2f2df04478
[modules] Add new covid tracker module (#1037)
* Add draft of covid module

* Work on pointers

* Add country stats

* Remove recovered, stays at 0

* Handle response code

* One struct for both

* List of countries

* Add test

* Add test for countries

* Fix typos

* Format numbers based on language/locale
2020-12-29 13:33:14 -08:00

88 lines
2.2 KiB
Go

package covid
import (
"fmt"
"github.com/rivo/tview"
"github.com/wtfutil/wtf/view"
"golang.org/x/text/language"
"golang.org/x/text/message"
)
// Widget is the struct that defines this module widget
type Widget struct {
view.TextWidget
settings *Settings
err error
}
// NewWidget creates a new widget for this module
func NewWidget(app *tview.Application, settings *Settings) *Widget {
widget := &Widget{
TextWidget: view.NewTextWidget(app, nil, settings.Common),
settings: settings,
}
widget.View.SetScrollable(true)
return widget
}
// Refresh checks if this module widget is disabled
func (widget *Widget) Refresh() {
if widget.Disabled() {
return
}
widget.Redraw(widget.content)
}
// Render renders this module widget
func (widget *Widget) Render() {
widget.Redraw(widget.content)
}
// Display stats based on the user's locale
func (widget *Widget) displayStats(cases int) string {
prntr := message.NewPrinter(language.English)
str := fmt.Sprintf("%s", prntr.Sprintf("%d", cases))
return str
}
func (widget *Widget) content() (string, string, bool) {
title := defaultTitle
if widget.CommonSettings().Title != "" {
title = widget.CommonSettings().Title
}
cases, err := LatestCases()
var covidStats string
if err != nil {
widget.err = err
} else {
// Display global stats
covidStats = fmt.Sprintf("[%s]Global[white]\n", widget.settings.Colors.Subheading)
covidStats += fmt.Sprintf("%s: %s\n", "Confirmed", widget.displayStats(cases.Latest.Confirmed))
covidStats += fmt.Sprintf("%s: %s\n", "Deaths", widget.displayStats(cases.Latest.Deaths))
}
// Retrieve country stats if country codes are set in the config
if len(widget.settings.countries) > 0 {
countryCases, err := widget.LatestCountryCases(widget.settings.countries)
if err != nil {
widget.err = err
} else {
for i, name := range countryCases {
covidStats += fmt.Sprintf("[%s]Country[white]: %s\n", widget.settings.Colors.Subheading, widget.settings.countries[i])
covidStats += fmt.Sprintf("%s: %s\n", "Confirmed", widget.displayStats(name.Latest.Confirmed))
covidStats += fmt.Sprintf("%s: %s\n", "Deaths", widget.displayStats(name.Latest.Deaths))
}
}
}
return title, covidStats, true
}