1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
wtf/modules/finnhub/widget.go
Chris Cummer fd794707cd
☢️ WTF-1031 Support multiple simultaneous configurations (#1032)
* WTF-1031 Rename WtfApp.app to WtfApp.tviewApp

Signed-off-by: Chris Cummer <chriscummer@me.com>

* WTF-1031 Add scaffolding for main to support multiple WtfApp instances

Signed-off-by: Chris Cummer <chriscummer@me.com>

* WTF-1031 WIP

Signed-off-by: Chris Cummer <chriscummer@me.com>

* Remove common functionality from KeyboardWidget and into Base

Signed-off-by: Chris Cummer <chriscummer@me.com>

* Augment with some descriptive comments

Signed-off-by: Chris Cummer <chriscummer@me.com>

* Add full support for multiple app instances via the AppManager.

Still to do:

* Config support for multiple apps/multiple config files
* The ability to switch between apps

Signed-off-by: Chris Cummer <chriscummer@me.com>

* Move SetTerminal out of main and into its own file

Signed-off-by: Chris Cummer <chriscummer@me.com>
2020-12-21 03:25:41 -08:00

62 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(tviewApp *tview.Application, settings *Settings) *Widget {
widget := Widget{
Client: NewClient(settings.symbols, settings.apiKey),
TextWidget: view.NewTextWidget(tviewApp, nil, settings.Common),
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)},
})
}
}
return title, t.Render(), wrap
}