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

95 lines
2.3 KiB
Go

package cointop
import (
"fmt"
6 years ago
"math"
"time"
types "github.com/miguelmota/cointop/pkg/api/types"
"github.com/miguelmota/cointop/pkg/color"
"github.com/miguelmota/cointop/pkg/fcache"
"github.com/miguelmota/cointop/pkg/humanize"
"github.com/miguelmota/cointop/pkg/pad"
)
func (ct *Cointop) updateMarketbar() error {
maxX := ct.width()
6 years ago
logo := fmt.Sprintf("%s%s%s%s", color.Green(""), color.Cyan(""), color.Green(""), color.Cyan("cointop"))
var content string
6 years ago
if ct.portfoliovisible {
total := ct.getPortfolioTotal()
if !(ct.currencyconversion == "BTC" || ct.currencyconversion == "ETH" || total < 1) {
total = math.Round(total*1e2) / 1e2
}
6 years ago
timeframe := ct.selectedchartrange
chartname := ct.selectedCoinName()
var charttitle string
if chartname == "" {
chartname = "Portfolio"
charttitle = color.Cyan(chartname)
} else {
charttitle = fmt.Sprintf("Portfolio - %s", color.Cyan(chartname))
}
content = fmt.Sprintf(
"[ Chart: %s %s ] Current Portfolio Value: %s%s",
charttitle,
timeframe,
ct.currencySymbol(),
humanize.Commaf(total),
)
} else {
6 years ago
var market types.GlobalMarketData
var err error
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")
}
} else {
market, err = ct.api.GetGlobalMarketData(ct.currencyconversion)
if err != nil {
return err
}
ct.cache.Set(cachekey, market, 10*time.Second)
go func() {
_ = fcache.Set(cachekey, market, 24*time.Hour)
}()
}
6 years ago
timeframe := ct.selectedchartrange
chartname := ct.selectedCoinName()
if chartname == "" {
chartname = "Global"
}
6 years ago
content = fmt.Sprintf(
"[ Chart: %s %s ] Global ▶ Market Cap: $%s • 24H Volume: $%s • BTC Dominance: %.2f%% • Active Currencies: %s • Active Markets: %s",
color.Cyan(chartname),
timeframe,
humanize.Commaf(market.TotalMarketCapUSD),
humanize.Commaf(market.Total24HVolumeUSD),
market.BitcoinPercentageOfMarketCap,
humanize.Commaf(float64(market.ActiveCurrencies)),
humanize.Commaf(float64(market.ActiveMarkets)),
)
}
6 years ago
content = fmt.Sprintf("%s %s", logo, content)
content = pad.Right(content, maxX, " ")
ct.update(func() {
ct.marketbarview.Clear()
6 years ago
fmt.Fprintln(ct.marketbarview, content)
})
6 years ago
return nil
}