diff --git a/internal/glance/templates.go b/internal/glance/templates.go index ec8d0f3..699772d 100644 --- a/internal/glance/templates.go +++ b/internal/glance/templates.go @@ -27,6 +27,9 @@ var globalTemplateFunctions = template.FuncMap{ "formatPrice": func(price float64) string { return intl.Sprintf("%.2f", price) }, + "formatPriceWithPrecision": func(precision int, price float64) string { + return intl.Sprintf("%."+strconv.Itoa(precision)+"f", price) + }, "dynamicRelativeTimeAttrs": dynamicRelativeTimeAttrs, "formatServerMegabytes": func(mb uint64) template.HTML { var value string diff --git a/internal/glance/templates/markets.html b/internal/glance/templates/markets.html index a979321..cd8da49 100644 --- a/internal/glance/templates/markets.html +++ b/internal/glance/templates/markets.html @@ -17,7 +17,7 @@
{{ printf "%+.2f" .PercentChange }}%
-
{{ .Currency }}{{ .Price | formatPrice }}
+
{{ .Currency }}{{ .Price | formatPriceWithPrecision .PriceHint }}
{{ end }} diff --git a/internal/glance/widget-markets.go b/internal/glance/widget-markets.go index a3bb325..b53b10a 100644 --- a/internal/glance/widget-markets.go +++ b/internal/glance/widget-markets.go @@ -79,6 +79,7 @@ type market struct { Name string Currency string Price float64 + PriceHint int PercentChange float64 SvgChartPoints string } @@ -106,6 +107,7 @@ type marketResponseJson struct { RegularMarketPrice float64 `json:"regularMarketPrice"` ChartPreviousClose float64 `json:"chartPreviousClose"` ShortName string `json:"shortName"` + PriceHint int `json:"priceHint"` } `json:"meta"` Indicators struct { Quote []struct { @@ -152,13 +154,14 @@ func fetchMarketsDataFromYahoo(marketRequests []marketRequest) (marketList, erro continue } - prices := response.Chart.Result[0].Indicators.Quote[0].Close + result := &response.Chart.Result[0] + prices := result.Indicators.Quote[0].Close if len(prices) > marketChartDays { prices = prices[len(prices)-marketChartDays:] } - previous := response.Chart.Result[0].Meta.RegularMarketPrice + previous := result.Meta.RegularMarketPrice if len(prices) >= 2 && prices[len(prices)-2] != 0 { previous = prices[len(prices)-2] @@ -166,21 +169,22 @@ func fetchMarketsDataFromYahoo(marketRequests []marketRequest) (marketList, erro points := svgPolylineCoordsFromYValues(100, 50, maybeCopySliceWithoutZeroValues(prices)) - currency, exists := currencyToSymbol[strings.ToUpper(response.Chart.Result[0].Meta.Currency)] + currency, exists := currencyToSymbol[strings.ToUpper(result.Meta.Currency)] if !exists { - currency = response.Chart.Result[0].Meta.Currency + currency = result.Meta.Currency } markets = append(markets, market{ marketRequest: marketRequests[i], - Price: response.Chart.Result[0].Meta.RegularMarketPrice, + Price: result.Meta.RegularMarketPrice, Currency: currency, + PriceHint: result.Meta.PriceHint, Name: ternary(marketRequests[i].CustomName == "", - response.Chart.Result[0].Meta.ShortName, + result.Meta.ShortName, marketRequests[i].CustomName, ), PercentChange: percentChange( - response.Chart.Result[0].Meta.RegularMarketPrice, + result.Meta.RegularMarketPrice, previous, ), SvgChartPoints: points,