mirror of
https://github.com/glanceapp/glance.git
synced 2025-06-24 20:01:46 +02:00
Add markets widget link templates & automatic name
This commit is contained in:
parent
260bc15577
commit
5723fbdea5
@ -1719,6 +1719,8 @@ Preview:
|
|||||||
| ---- | ---- | -------- |
|
| ---- | ---- | -------- |
|
||||||
| markets | array | yes |
|
| markets | array | yes |
|
||||||
| sort-by | string | no |
|
| sort-by | string | no |
|
||||||
|
| chart-link-template | string | no |
|
||||||
|
| symbol-link-template | string | no |
|
||||||
|
|
||||||
##### `markets`
|
##### `markets`
|
||||||
An array of markets for which to display information about.
|
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`
|
##### `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%).
|
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 |
|
| Name | Type | Required |
|
||||||
| ---- | ---- | -------- |
|
| ---- | ---- | -------- |
|
||||||
| symbol | string | yes |
|
| symbol | string | yes |
|
||||||
@ -1743,9 +1759,11 @@ The symbol, as seen in Yahoo Finance.
|
|||||||
The name that will be displayed under the symbol.
|
The name that will be displayed under the symbol.
|
||||||
|
|
||||||
`symbol-link`
|
`symbol-link`
|
||||||
|
|
||||||
The link to go to when clicking on the symbol.
|
The link to go to when clicking on the symbol.
|
||||||
|
|
||||||
`chart-link`
|
`chart-link`
|
||||||
|
|
||||||
The link to go to when clicking on the chart.
|
The link to go to when clicking on the chart.
|
||||||
|
|
||||||
### Twitch Channels
|
### Twitch Channels
|
||||||
|
@ -178,3 +178,11 @@ func itemAtIndexOrDefault[T any](items []T, index int, def T) T {
|
|||||||
|
|
||||||
return items[index]
|
return items[index]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ternary[T any](condition bool, a, b T) T {
|
||||||
|
if condition {
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
@ -8,17 +8,20 @@ import (
|
|||||||
"math"
|
"math"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var marketsWidgetTemplate = mustParseTemplate("markets.html", "widget-base.html")
|
var marketsWidgetTemplate = mustParseTemplate("markets.html", "widget-base.html")
|
||||||
|
|
||||||
type marketsWidget struct {
|
type marketsWidget struct {
|
||||||
widgetBase `yaml:",inline"`
|
widgetBase `yaml:",inline"`
|
||||||
StocksRequests []marketRequest `yaml:"stocks"`
|
StocksRequests []marketRequest `yaml:"stocks"`
|
||||||
MarketRequests []marketRequest `yaml:"markets"`
|
MarketRequests []marketRequest `yaml:"markets"`
|
||||||
Sort string `yaml:"sort-by"`
|
ChartLinkTemplate string `yaml:"chart-link-template"`
|
||||||
Markets marketList `yaml:"-"`
|
SymbolLinkTemplate string `yaml:"symbol-link-template"`
|
||||||
|
Sort string `yaml:"sort-by"`
|
||||||
|
Markets marketList `yaml:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (widget *marketsWidget) initialize() error {
|
func (widget *marketsWidget) initialize() error {
|
||||||
@ -29,6 +32,18 @@ func (widget *marketsWidget) initialize() error {
|
|||||||
widget.MarketRequests = widget.StocksRequests
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,9 +56,7 @@ func (widget *marketsWidget) update(ctx context.Context) {
|
|||||||
|
|
||||||
if widget.Sort == "absolute-change" {
|
if widget.Sort == "absolute-change" {
|
||||||
markets.sortByAbsChange()
|
markets.sortByAbsChange()
|
||||||
}
|
} else if widget.Sort == "change" {
|
||||||
|
|
||||||
if widget.Sort == "change" {
|
|
||||||
markets.sortByChange()
|
markets.sortByChange()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,7 +68,7 @@ func (widget *marketsWidget) Render() template.HTML {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type marketRequest struct {
|
type marketRequest struct {
|
||||||
Name string `yaml:"name"`
|
CustomName string `yaml:"name"`
|
||||||
Symbol string `yaml:"symbol"`
|
Symbol string `yaml:"symbol"`
|
||||||
ChartLink string `yaml:"chart-link"`
|
ChartLink string `yaml:"chart-link"`
|
||||||
SymbolLink string `yaml:"symbol-link"`
|
SymbolLink string `yaml:"symbol-link"`
|
||||||
@ -63,6 +76,7 @@ type marketRequest struct {
|
|||||||
|
|
||||||
type market struct {
|
type market struct {
|
||||||
marketRequest
|
marketRequest
|
||||||
|
Name string
|
||||||
Currency string
|
Currency string
|
||||||
Price float64
|
Price float64
|
||||||
PercentChange float64
|
PercentChange float64
|
||||||
@ -91,6 +105,7 @@ type marketResponseJson struct {
|
|||||||
Symbol string `json:"symbol"`
|
Symbol string `json:"symbol"`
|
||||||
RegularMarketPrice float64 `json:"regularMarketPrice"`
|
RegularMarketPrice float64 `json:"regularMarketPrice"`
|
||||||
ChartPreviousClose float64 `json:"chartPreviousClose"`
|
ChartPreviousClose float64 `json:"chartPreviousClose"`
|
||||||
|
ShortName string `json:"shortName"`
|
||||||
} `json:"meta"`
|
} `json:"meta"`
|
||||||
Indicators struct {
|
Indicators struct {
|
||||||
Quote []struct {
|
Quote []struct {
|
||||||
@ -160,6 +175,10 @@ func fetchMarketsDataFromYahoo(marketRequests []marketRequest) (marketList, erro
|
|||||||
marketRequest: marketRequests[i],
|
marketRequest: marketRequests[i],
|
||||||
Price: response.Chart.Result[0].Meta.RegularMarketPrice,
|
Price: response.Chart.Result[0].Meta.RegularMarketPrice,
|
||||||
Currency: currency,
|
Currency: currency,
|
||||||
|
Name: ternary(marketRequests[i].CustomName == "",
|
||||||
|
response.Chart.Result[0].Meta.ShortName,
|
||||||
|
marketRequests[i].CustomName,
|
||||||
|
),
|
||||||
PercentChange: percentChange(
|
PercentChange: percentChange(
|
||||||
response.Chart.Result[0].Meta.RegularMarketPrice,
|
response.Chart.Result[0].Meta.RegularMarketPrice,
|
||||||
previous,
|
previous,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user