1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
Chris Cummer fd91a48f58 WTF-897 Exchange Rate improvements
Allows the user to set the precision for their exchange rate values.

Config setting:
```
exchangerates:
  precision: 3
```

Default is `7`.

Also aligns converted values along the decimal place for improved
aesthetics.

Signed-off-by: Chris Cummer <chriscummer@me.com>
2020-07-27 17:21:36 -07:00

87 lines
1.8 KiB
Go

package exchangerates
import (
"fmt"
"regexp"
"github.com/rivo/tview"
"github.com/wtfutil/wtf/view"
"github.com/wtfutil/wtf/wtf"
)
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()
}
func (widget *Widget) Render() {
widget.Redraw(widget.content)
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) content() (string, string, bool) {
if widget.err != nil {
return widget.CommonSettings().Title, widget.err.Error(), false
}
out := ""
idx := 0
for base, rates := range widget.settings.rates {
for _, cur := range rates {
rate := widget.rates[base][cur]
out += fmt.Sprintf(
"[%s]1 %s = %s %s[white]\n",
widget.CommonSettings().RowColor(idx),
base,
widget.formatConversionRate(rate),
cur,
)
idx++
}
}
return widget.CommonSettings().Title, out, false
}
// formatConversionRate takes the raw conversion float and formats it to the precision the
// user specifies in their config (or to the default value)
func (widget *Widget) formatConversionRate(rate float64) string {
rate = wtf.TruncateFloat64(rate, widget.settings.precision)
r, _ := regexp.Compile(`\.?0*$`)
return r.ReplaceAllString(fmt.Sprintf("%10.7f", rate), "")
}