diff --git a/docs/configuration.md b/docs/configuration.md index 92c5d8c..44f0b90 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -1719,6 +1719,8 @@ Preview: | ---- | ---- | -------- | | markets | array | yes | | sort-by | string | no | +| chart-link-template | string | no | +| symbol-link-template | string | no | ##### `markets` An array of markets for which to display information about. @@ -1726,7 +1728,21 @@ An array of markets for which to display information about. ##### `sort-by` By default the markets are displayed in the order they were defined. You can customize their ordering by setting the `sort-by` property to `change` for descending order based on the stock's percentage change (e.g. 1% would be sorted higher than -1%) or `absolute-change` for descending order based on the stock's absolute price change (e.g. -1% would be sorted higher than +0.5%). -###### Properties for each stock +##### `chart-link-template` +A template for the link to go to when clicking on the chart that will be applied to all markets. The value `{SYMBOL}` will be replaced with the symbol of the market. You can override this on a per-market basis by specifying a `chart-link` property. Example: + +```yaml +chart-link-template: https://www.tradingview.com/chart/?symbol={SYMBOL} +``` + +##### `symbol-link-template` +A template for the link to go to when clicking on the symbol that will be applied to all markets. The value `{SYMBOL}` will be replaced with the symbol of the market. You can override this on a per-market basis by specifying a `symbol-link` property. Example: + +```yaml +symbol-link-template: https://www.google.com/search?tbm=nws&q={SYMBOL} +``` + +###### Properties for each market | Name | Type | Required | | ---- | ---- | -------- | | symbol | string | yes | @@ -1743,9 +1759,11 @@ The symbol, as seen in Yahoo Finance. The name that will be displayed under the symbol. `symbol-link` + The link to go to when clicking on the symbol. `chart-link` + The link to go to when clicking on the chart. ### Twitch Channels diff --git a/internal/glance/utils.go b/internal/glance/utils.go index 105cd0d..8455bfe 100644 --- a/internal/glance/utils.go +++ b/internal/glance/utils.go @@ -178,3 +178,11 @@ func itemAtIndexOrDefault[T any](items []T, index int, def T) T { return items[index] } + +func ternary[T any](condition bool, a, b T) T { + if condition { + return a + } + + return b +} diff --git a/internal/glance/widget-markets.go b/internal/glance/widget-markets.go index 48df6fc..63eda1a 100644 --- a/internal/glance/widget-markets.go +++ b/internal/glance/widget-markets.go @@ -8,17 +8,20 @@ import ( "math" "net/http" "sort" + "strings" "time" ) var marketsWidgetTemplate = mustParseTemplate("markets.html", "widget-base.html") type marketsWidget struct { - widgetBase `yaml:",inline"` - StocksRequests []marketRequest `yaml:"stocks"` - MarketRequests []marketRequest `yaml:"markets"` - Sort string `yaml:"sort-by"` - Markets marketList `yaml:"-"` + widgetBase `yaml:",inline"` + StocksRequests []marketRequest `yaml:"stocks"` + MarketRequests []marketRequest `yaml:"markets"` + ChartLinkTemplate string `yaml:"chart-link-template"` + SymbolLinkTemplate string `yaml:"symbol-link-template"` + Sort string `yaml:"sort-by"` + Markets marketList `yaml:"-"` } func (widget *marketsWidget) initialize() error { @@ -29,6 +32,18 @@ func (widget *marketsWidget) initialize() error { widget.MarketRequests = widget.StocksRequests } + for i := range widget.MarketRequests { + m := &widget.MarketRequests[i] + + if widget.ChartLinkTemplate != "" && m.ChartLink == "" { + m.ChartLink = strings.ReplaceAll(widget.ChartLinkTemplate, "{SYMBOL}", m.Symbol) + } + + if widget.SymbolLinkTemplate != "" && m.SymbolLink == "" { + m.SymbolLink = strings.ReplaceAll(widget.SymbolLinkTemplate, "{SYMBOL}", m.Symbol) + } + } + return nil } @@ -41,9 +56,7 @@ func (widget *marketsWidget) update(ctx context.Context) { if widget.Sort == "absolute-change" { markets.sortByAbsChange() - } - - if widget.Sort == "change" { + } else if widget.Sort == "change" { markets.sortByChange() } @@ -55,7 +68,7 @@ func (widget *marketsWidget) Render() template.HTML { } type marketRequest struct { - Name string `yaml:"name"` + CustomName string `yaml:"name"` Symbol string `yaml:"symbol"` ChartLink string `yaml:"chart-link"` SymbolLink string `yaml:"symbol-link"` @@ -63,6 +76,7 @@ type marketRequest struct { type market struct { marketRequest + Name string Currency string Price float64 PercentChange float64 @@ -91,6 +105,7 @@ type marketResponseJson struct { Symbol string `json:"symbol"` RegularMarketPrice float64 `json:"regularMarketPrice"` ChartPreviousClose float64 `json:"chartPreviousClose"` + ShortName string `json:"shortName"` } `json:"meta"` Indicators struct { Quote []struct { @@ -160,6 +175,10 @@ func fetchMarketsDataFromYahoo(marketRequests []marketRequest) (marketList, erro marketRequest: marketRequests[i], Price: response.Chart.Result[0].Meta.RegularMarketPrice, Currency: currency, + Name: ternary(marketRequests[i].CustomName == "", + response.Chart.Result[0].Meta.ShortName, + marketRequests[i].CustomName, + ), PercentChange: percentChange( response.Chart.Result[0].Meta.RegularMarketPrice, previous,