1
0
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:
Toon Schoenmakers
2019-11-12 18:06:45 +01:00
parent f873baea7f
commit 244a86cb7e
4 changed files with 161 additions and 0 deletions

View 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
}