1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
Antoine Meillet 9fb57845ed
Add Yahoo Finance module (#1066)
* Move finnhub to a stocks folder

As I am preparing an other stocks data provider, let's move `finnhub` to
a stocks folder that will host the others providers.

* Use go-pretty v6

Will be used by the new stock provider module, so let's just upgrade
this one to reduce the number of dependencies.

* Add Yahoo Finance module

Yahoo Finance provides an API for which `piquette/finance-go` is a
powerful client. This new module leverages this module to integrate all
indices provided by Yahoo Finance (international stocks, crypto,
options, currencies...)

Sample config:
```yaml
    yfinance:
      title: "Stocks 🚀"
      symbols:
        - "MSFT"
        - "GC=F"
        - "ORA.PA"
      sort: true
      enabled: true
      refreshInterval: 60
      position:
        top: 1
        left: 0
        height: 1
        width: 1
```
2021-03-27 13:42:28 -07:00

75 lines
1.7 KiB
Go

package yfinance
import (
"fmt"
"sort"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/rivo/tview"
"github.com/wtfutil/wtf/view"
)
// Widget is the container for your module's data
type Widget struct {
view.TextWidget
settings *Settings
}
// NewWidget creates and returns an instance of Widget
func NewWidget(tviewApp *tview.Application, settings *Settings) *Widget {
widget := Widget{
TextWidget: view.NewTextWidget(tviewApp, nil, settings.common),
settings: settings,
}
return &widget
}
/* -------------------- Exported Functions -------------------- */
// Refresh updates the onscreen contents of the widget
func (widget *Widget) Refresh() {
// The last call should always be to the display function
widget.display()
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) content() string {
yquotes := quotes(widget.settings.symbols)
colors := map[string]string{
"bigup": widget.settings.colors.bigup,
"up": widget.settings.colors.up,
"drop": widget.settings.colors.drop,
"bigdrop": widget.settings.colors.bigdrop,
}
if widget.settings.sort {
sort.SliceStable(yquotes, func(i, j int) bool { return yquotes[i].MarketChangePct > yquotes[j].MarketChangePct })
}
t := table.NewWriter()
t.SetStyle(tableStyle())
for _, yq := range yquotes {
t.AppendRow([]interface{}{
GetMarketIcon(yq.MarketState),
yq.Symbol,
fmt.Sprintf("%8.2f %s", yq.MarketPrice, yq.Currency),
GetTrendIcon(yq.Trend),
fmt.Sprintf("[%s]%+6.2f (%+5.2f%%)", colors[yq.Trend], yq.MarketChange, yq.MarketChangePct),
})
}
return t.Render()
}
func (widget *Widget) display() {
widget.Redraw(func() (string, string, bool) {
return widget.CommonSettings().Title, widget.content(), false
})
}