1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
wtf/modules/finnhub/widget.go
2020-10-19 20:51:39 +01:00

64 lines
1.2 KiB
Go

package finnhub
import (
"fmt"
"github.com/jedib0t/go-pretty/table"
"github.com/rivo/tview"
"github.com/wtfutil/wtf/view"
)
// Widget ..
type Widget struct {
view.TextWidget
*Client
settings *Settings
}
// NewWidget ..
func NewWidget(app *tview.Application, settings *Settings) *Widget {
widget := Widget{
TextWidget: view.NewTextWidget(app, settings.common),
Client: NewClient(settings.symbols, settings.apiKey),
settings: settings,
}
return &widget
}
/* -------------------- Exported Functions -------------------- */
func (widget *Widget) Refresh() {
if widget.Disabled() {
return
}
widget.Redraw(widget.content)
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) content() (string, string, bool) {
quotes, err := widget.Client.Getquote()
title := widget.CommonSettings().Title
t := table.NewWriter()
t.AppendHeader(table.Row{"#", "Stock", "Current Price", "Open Price", "Change"})
wrap := false
if err != nil {
wrap = true
} else {
for idx, q := range quotes {
t.AppendRows([]table.Row{
{idx, q.Stock, q.C, q.O, fmt.Sprintf("%.4f", (q.C-q.O)/q.C)},
})
}
}
str := fmt.Sprintf(t.Render())
return title, str, wrap
}