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

package cointop
import (
"fmt"
5 years ago
"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
}
4 years ago
// UpdateMarketbar updates the market bar view
func (ct *Cointop) UpdateMarketbar() error {
ct.debuglog("updateMarketbar()")
maxX := ct.width()
5 years ago
logo := "cointop"
5 years ago
if ct.colorschemeName == "cointop" {
5 years ago
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.State.portfolioVisible {
4 years ago
total := ct.GetPortfolioTotal()
totalstr := humanize.Commaf(total)
5 years ago
if !(ct.State.currencyConversion == "BTC" || ct.State.currencyConversion == "ETH" || total < 1) {
5 years ago
total = math.Round(total*1e2) / 1e2
totalstr = humanize.Commaf2(total)
}
5 years ago
5 years ago
timeframe := ct.State.selectedChartRange
4 years ago
chartname := ct.SelectedCoinName()
5 years ago
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
4 years ago
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 = "▼"
}
chartInfo := ""
5 years ago
if !ct.State.hideChart {
chartInfo = fmt.Sprintf(
"[ Chart: %s %s ] ",
charttitle,
timeframe,
)
}
5 years ago
content = fmt.Sprintf(
"%sTotal Portfolio Value: %s • 24H: %s",
chartInfo,
4 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.State.currencyConversion)
5 years ago
if err != nil {
if ct.filecache != nil {
ct.filecache.Get(cachekey, &market)
}
5 years ago
}
ct.cache.Set(cachekey, market, 10*time.Second)
if ct.filecache != nil {
go func() {
ct.filecache.Set(cachekey, market, 24*time.Hour)
}()
}
}
5 years ago
timeframe := ct.State.selectedChartRange
4 years ago
chartname := ct.SelectedCoinName()
5 years ago
if chartname == "" {
chartname = "Global"
}
chartInfo := ""
5 years ago
if !ct.State.hideChart {
chartInfo = fmt.Sprintf(
"[ Chart: %s %s ] ",
ct.colorscheme.MarketBarLabelActive(chartname),
timeframe,
)
}
5 years ago
content = fmt.Sprintf(
"%sGlobal ▶ Market Cap: %s • 24H Volume: %s • BTC Dominance: %.2f%%",
chartInfo,
4 years ago
fmt.Sprintf("%s%s", ct.CurrencySymbol(), humanize.Commaf0(market.TotalMarketCapUSD)),
fmt.Sprintf("%s%s", ct.CurrencySymbol(), humanize.Commaf0(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.UpdateUI(func() error {
return ct.Views.Marketbar.Update(content)
})
5 years ago
return nil
}