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

122 lines
3.2 KiB
Go

package cointop
import (
"fmt"
5 years ago
"math"
"time"
5 years ago
types "github.com/miguelmota/cointop/cointop/common/api/types"
"github.com/miguelmota/cointop/cointop/common/color"
"github.com/miguelmota/cointop/cointop/common/filecache"
"github.com/miguelmota/cointop/cointop/common/humanize"
"github.com/miguelmota/cointop/cointop/common/pad"
)
func (ct *Cointop) updateMarketbar() error {
maxX := ct.width()
5 years ago
logo := "cointop"
if ct.colorschemename == "cointop" {
logo = fmt.Sprintf("%s%s%s%s", color.Green(""), color.Cyan(""), color.Green(""), color.Cyan("cointop"))
}
5 years ago
var content string
5 years ago
if ct.portfoliovisible {
total := ct.getPortfolioTotal()
totalstr := humanize.Commaf(total)
5 years ago
if !(ct.currencyconversion == "BTC" || ct.currencyconversion == "ETH" || total < 1) {
total = math.Round(total*1e2) / 1e2
totalstr = humanize.Commaf2(total)
}
5 years ago
timeframe := ct.selectedchartrange
chartname := ct.selectedCoinName()
var charttitle string
if chartname == "" {
chartname = "Portfolio"
5 years ago
charttitle = ct.colorscheme.MarketBarLabelActive(chartname)
5 years ago
} else {
5 years ago
charttitle = fmt.Sprintf("Portfolio - %s", ct.colorscheme.MarketBarLabelActive(chartname))
5 years ago
}
var percentChange24H float64
for _, p := range ct.getPortfolioSlice() {
n := ((p.Balance / total) * p.PercentChange24H)
if math.IsNaN(n) {
continue
}
percentChange24H += n
}
5 years ago
color24h := ct.colorscheme.MarketbarSprintf()
arrow := ""
if percentChange24H > 0 {
5 years ago
color24h = ct.colorscheme.MarketbarChangeUpSprintf()
arrow = "▲"
}
if percentChange24H < 0 {
5 years ago
color24h = ct.colorscheme.MarketbarChangeDownSprintf()
arrow = "▼"
}
5 years ago
content = fmt.Sprintf(
"[ Chart: %s %s ] Total Portfolio Value: %s • 24H: %s",
5 years ago
charttitle,
timeframe,
5 years ago
ct.colorscheme.MarketBarLabelActive(fmt.Sprintf("%s%s", ct.currencySymbol(), totalstr)),
color24h(fmt.Sprintf("%.2f%%%s", percentChange24H, arrow)),
5 years ago
)
} else {
5 years ago
var market types.GlobalMarketData
var err error
cachekey := ct.cacheKey("market")
5 years ago
cached, found := ct.cache.Get(cachekey)
5 years ago
if found {
// cache hit
var ok bool
market, ok = cached.(types.GlobalMarketData)
if ok {
ct.debuglog("soft cache hit")
}
5 years ago
}
if market.TotalMarketCapUSD == 0 {
5 years ago
market, err = ct.api.GetGlobalMarketData(ct.currencyconversion)
if err != nil {
5 years ago
filecache.Get(cachekey, &market)
5 years ago
}
ct.cache.Set(cachekey, market, 10*time.Second)
go func() {
5 years ago
filecache.Set(cachekey, market, 24*time.Hour)
5 years ago
}()
}
5 years ago
timeframe := ct.selectedchartrange
chartname := ct.selectedCoinName()
if chartname == "" {
chartname = "Global"
}
5 years ago
content = fmt.Sprintf(
"[ Chart: %s %s ] Global ▶ Market Cap: %s • 24H Volume: %s • BTC Dominance: %.2f%%",
5 years ago
ct.colorscheme.MarketBarLabelActive(chartname),
5 years ago
timeframe,
fmt.Sprintf("%s%s", ct.currencySymbol(), humanize.Commaf(market.TotalMarketCapUSD)),
fmt.Sprintf("%s%s", ct.currencySymbol(), humanize.Commaf(market.Total24HVolumeUSD)),
5 years ago
market.BitcoinPercentageOfMarketCap,
)
}
5 years ago
content = fmt.Sprintf("%s %s", logo, content)
content = pad.Right(content, maxX, " ")
5 years ago
content = ct.colorscheme.Marketbar(content)
5 years ago
ct.update(func() {
ct.marketbarview.Clear()
5 years ago
fmt.Fprintln(ct.marketbarview, content)
})
5 years ago
return nil
}