1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
Toon Schoenmakers c8a14e7685 Keep an initial order of the currencies so they won't be in a different order after refreshing (#752)
This purely happend due to maps not keeping the order items are in. This way
it at least won't change throughout the runtime of wtfutil. Ideally it would
use and keep the order as specified by the user in the configuration but
right now there is no way to enforce this.
2019-11-16 19:47:52 -08:00

71 lines
1.5 KiB
Go

// 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.settings.rates {
out += fmt.Sprintf("[%s]Rates from %s[white]\n", widget.settings.common.Colors.Subheading, base)
idx := 0
for _, cur := range rates {
rate := widget.rates[base][cur]
out += fmt.Sprintf("[%s]%s - %f[white]\n", widget.CommonSettings().RowColor(idx), cur, rate)
idx++
}
}
}
return widget.CommonSettings().Title, out, false
}