mirror of
https://github.com/taigrr/wtf
synced 2026-03-21 17:42:18 -07:00
Implemented a module that shows exchange rates from exchangeratesapi.io
This commit is contained in:
69
modules/exchangerates/widget.go
Normal file
69
modules/exchangerates/widget.go
Normal file
@@ -0,0 +1,69 @@
|
||||
// Package exchangerates
|
||||
package exchangerates
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/rivo/tview"
|
||||
"github.com/wtfutil/wtf/view"
|
||||
)
|
||||
|
||||
type Widget struct {
|
||||
view.ScrollableWidget
|
||||
|
||||
settings *Settings
|
||||
rates map[string]map[string]float64
|
||||
err error
|
||||
}
|
||||
|
||||
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
ScrollableWidget: view.NewScrollableWidget(app, settings.common),
|
||||
|
||||
settings: settings,
|
||||
}
|
||||
|
||||
widget.SetRenderFunction(widget.Render)
|
||||
|
||||
return &widget
|
||||
}
|
||||
|
||||
/* -------------------- Exported Functions -------------------- */
|
||||
|
||||
func (widget *Widget) Refresh() {
|
||||
|
||||
rates, err := FetchExchangeRates(widget.settings)
|
||||
if err != nil {
|
||||
widget.err = err
|
||||
} else {
|
||||
widget.rates = rates
|
||||
}
|
||||
|
||||
// The last call should always be to the display function
|
||||
widget.Render()
|
||||
}
|
||||
|
||||
/* -------------------- Unexported Functions -------------------- */
|
||||
|
||||
func (widget *Widget) Render() {
|
||||
widget.Redraw(widget.content)
|
||||
}
|
||||
|
||||
func (widget *Widget) content() (string, string, bool) {
|
||||
var out string
|
||||
|
||||
if widget.err != nil {
|
||||
out = widget.err.Error()
|
||||
} else {
|
||||
for base, rates := range widget.rates {
|
||||
out += fmt.Sprintf("[%s]Rates from %s[white]\n", widget.settings.common.Colors.Subheading, base)
|
||||
idx := 0
|
||||
for cur, rate := range rates {
|
||||
out += fmt.Sprintf("[%s]%s - %f[white]\n", widget.CommonSettings().RowColor(idx), cur, rate)
|
||||
idx++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return widget.CommonSettings().Title, out, false
|
||||
}
|
||||
Reference in New Issue
Block a user