You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cointop/cointop/marketbar.go

152 lines
3.8 KiB
Go

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package cointop
import (
"fmt"
"math"
"time"
types "github.com/miguelmota/cointop/pkg/api/types"
"github.com/miguelmota/cointop/pkg/color"
"github.com/miguelmota/cointop/pkg/humanize"
"github.com/miguelmota/cointop/pkg/pad"
"github.com/miguelmota/cointop/pkg/ui"
)
// MarketbarView is structure for marketbar view
type MarketbarView = ui.View
// NewMarketbarView returns a new marketbar view
func NewMarketbarView() *MarketbarView {
var view *MarketbarView = ui.NewView("marketbar")
return view
}
// UpdateMarketbar updates the market bar view
func (ct *Cointop) UpdateMarketbar() error {
ct.debuglog("updateMarketbar()")
maxX := ct.width()
logo := "cointop"
if ct.colorschemeName == "cointop" {
logo = fmt.Sprintf("%s%s%s%s", color.Green(""), color.Cyan(""), color.Green(""), color.Cyan("cointop"))
}
var content string
if ct.State.portfolioVisible {
total := ct.GetPortfolioTotal()
totalstr := humanize.Commaf(total)
if !(ct.State.currencyConversion == "BTC" || ct.State.currencyConversion == "ETH" || total < 1) {
total = math.Round(total*1e2) / 1e2
totalstr = humanize.Commaf2(total)
}
timeframe := ct.State.selectedChartRange
chartname := ct.SelectedCoinName()
var charttitle string
if chartname == "" {
chartname = "Portfolio"
charttitle = ct.colorscheme.MarketBarLabelActive(chartname)
} else {
charttitle = fmt.Sprintf("Portfolio - %s", ct.colorscheme.MarketBarLabelActive(chartname))
}
var percentChange24H float64
for _, p := range ct.GetPortfolioSlice() {
n := ((p.Balance / total) * p.PercentChange24H)
if math.IsNaN(n) {
continue
}
percentChange24H += n
}
color24h := ct.colorscheme.MarketbarSprintf()
arrow := ""
if percentChange24H > 0 {
color24h = ct.colorscheme.MarketbarChangeUpSprintf()
arrow = "▲"
}
if percentChange24H < 0 {
color24h = ct.colorscheme.MarketbarChangeDownSprintf()
arrow = "▼"
}
chartInfo := ""
if !ct.State.hideChart {
chartInfo = fmt.Sprintf(
"[ Chart: %s %s ] ",
charttitle,
timeframe,
)
}
content = fmt.Sprintf(
"%sTotal Portfolio Value: %s • 24H: %s",
chartInfo,
ct.colorscheme.MarketBarLabelActive(fmt.Sprintf("%s%s", ct.CurrencySymbol(), totalstr)),
color24h(fmt.Sprintf("%.2f%%%s", percentChange24H, arrow)),
)
} else {
var market types.GlobalMarketData
var err error
cachekey := ct.CacheKey("market")
cached, found := ct.cache.Get(cachekey)
if found {
// cache hit
var ok bool
market, ok = cached.(types.GlobalMarketData)
if ok {
ct.debuglog("soft cache hit")
}
}
if market.TotalMarketCapUSD == 0 {
market, err = ct.api.GetGlobalMarketData(ct.State.currencyConversion)
if err != nil {
if ct.filecache != nil {
ct.filecache.Get(cachekey, &market)
}
}
ct.cache.Set(cachekey, market, 10*time.Second)
if ct.filecache != nil {
go func() {
ct.filecache.Set(cachekey, market, 24*time.Hour)
}()
}
}
timeframe := ct.State.selectedChartRange
chartname := ct.SelectedCoinName()
if chartname == "" {
chartname = "Global"
}
chartInfo := ""
if !ct.State.hideChart {
chartInfo = fmt.Sprintf(
"[ Chart: %s %s ] ",
ct.colorscheme.MarketBarLabelActive(chartname),
timeframe,
)
}
content = fmt.Sprintf(
"%sGlobal ▶ Market Cap: %s • 24H Volume: %s • BTC Dominance: %.2f%%",
chartInfo,
fmt.Sprintf("%s%s", ct.CurrencySymbol(), humanize.Commaf0(market.TotalMarketCapUSD)),
fmt.Sprintf("%s%s", ct.CurrencySymbol(), humanize.Commaf0(market.Total24HVolumeUSD)),
market.BitcoinPercentageOfMarketCap,
)
}
content = fmt.Sprintf("%s %s", logo, content)
content = pad.Right(content, maxX, " ")
content = ct.colorscheme.Marketbar(content)
ct.UpdateUI(func() error {
return ct.Views.Marketbar.Update(content)
})
return nil
}