From 599d2a226aa9b8deee999b3a4fda7b60fbbe302d Mon Sep 17 00:00:00 2001 From: Miguel Mota Date: Tue, 25 Jun 2019 23:13:31 -0700 Subject: [PATCH] Fetch full price if avaiable in coingecko api. Closes #37 --- cmd/cointop.go | 22 ++++++++--------- .../common/api/impl/coingecko/coingecko.go | 24 ++++++++++++++++++- cointop/common/api/util/util.go | 1 + cointop/layout.go | 3 ++- 4 files changed, 37 insertions(+), 13 deletions(-) diff --git a/cmd/cointop.go b/cmd/cointop.go index 9aa3f17..5494a7a 100644 --- a/cmd/cointop.go +++ b/cmd/cointop.go @@ -12,18 +12,18 @@ func Run() { var v, ver, test, clean, reset, hideMarketbar, hideChart, hideStatusbar, onlyTable bool var config, cmcAPIKey, apiChoice, colorscheme string flag.BoolVar(&v, "v", false, "Version") - flag.BoolVar(&ver, "version", false, "Version") - flag.BoolVar(&test, "test", false, "Run test") - flag.BoolVar(&clean, "clean", false, "Clean cache") - flag.BoolVar(&reset, "reset", false, "Reset config") - flag.BoolVar(&hideMarketbar, "hide-marketbar", false, "Hide marketbar") - flag.BoolVar(&hideChart, "hide-chart", false, "Hide chart view") - flag.BoolVar(&hideStatusbar, "hide-statusbar", false, "Hide statusbar") - flag.BoolVar(&onlyTable, "only-table", false, "Show only the table") - flag.StringVar(&config, "config", "", "Config filepath") - flag.StringVar(&cmcAPIKey, "coinmarketcap-api-key", "", "CoinMarketCap API key") + flag.BoolVar(&ver, "version", false, "Display current version") + flag.BoolVar(&test, "test", false, "Run test (for Homebrew)") + flag.BoolVar(&clean, "clean", false, "Wipe clean the cache") + flag.BoolVar(&reset, "reset", false, "Reset the config. Make sure to backup any relevant changes first!") + flag.BoolVar(&hideMarketbar, "hide-marketbar", false, "Hide the top marketbar") + flag.BoolVar(&hideChart, "hide-chart", false, "Hide the chart view") + flag.BoolVar(&hideStatusbar, "hide-statusbar", false, "Hide the bottom statusbar") + flag.BoolVar(&onlyTable, "only-table", false, "Show only the table. Hides the chart and top and bottom bars.") + flag.StringVar(&config, "config", "", "Config filepath. Default is ~/.cointop/config.toml") + flag.StringVar(&cmcAPIKey, "coinmarketcap-api-key", "", "Set the CoinMarketCap API key") flag.StringVar(&apiChoice, "api", cointop.CoinGecko, "API choice") - flag.StringVar(&colorscheme, "colorscheme", "", "Colorscheme name") + flag.StringVar(&colorscheme, "colorscheme", "", "Colorscheme to use. Default is \"cointop\", For instructions, visit https://github.com/cointop-sh/colors") flag.Parse() if v || ver { diff --git a/cointop/common/api/impl/coingecko/coingecko.go b/cointop/common/api/impl/coingecko/coingecko.go index 610d4ab..9690a12 100644 --- a/cointop/common/api/impl/coingecko/coingecko.go +++ b/cointop/common/api/impl/coingecko/coingecko.go @@ -57,7 +57,29 @@ func (s *Service) getLimitedCoinData(convert string, offset int) ([]apitypes.Coi } if list != nil { + // for fetching "simple prices" + currencies := make([]string, len(*list)) + for i, item := range *list { + currencies[i] = item.Name + } + + // NOTE: "simple" prices include decimal places so we use these prices + // if available but if there's an error then simply use "current price" + // which may not have a decimal place. + prices, err := s.client.SimplePrice(currencies, []string{convertTo}) + for _, item := range *list { + price := item.CurrentPrice + + if prices != nil && err == nil { + pricesObj := *prices + if coinObj, ok := pricesObj[strings.ToLower(item.Name)]; ok { + if p, ok := coinObj[convertTo]; ok { + price = float64(p) + } + } + } + var percentChange1H float64 var percentChange24H float64 var percentChange7D float64 @@ -88,7 +110,7 @@ func (s *Service) getLimitedCoinData(convert string, offset int) ([]apitypes.Coi AvailableSupply: util.FormatSupply(availableSupply), TotalSupply: util.FormatSupply(totalSupply), MarketCap: util.FormatMarketCap(item.MarketCap), - Price: util.FormatPrice(item.CurrentPrice, convert), + Price: util.FormatPrice(price, convert), PercentChange1H: util.FormatPercentChange(percentChange1H), PercentChange24H: util.FormatPercentChange(percentChange24H), PercentChange7D: util.FormatPercentChange(percentChange7D), diff --git a/cointop/common/api/util/util.go b/cointop/common/api/util/util.go index c6e7606..624dfe9 100644 --- a/cointop/common/api/util/util.go +++ b/cointop/common/api/util/util.go @@ -59,6 +59,7 @@ func FormatRank(rank interface{}) int { // FormatPrice formats the price value func FormatPrice(price float64, convert string) float64 { + convert = strings.ToUpper(convert) pricestr := fmt.Sprintf("%.2f", price) if convert == "ETH" || convert == "BTC" || price < 1 { pricestr = fmt.Sprintf("%.5f", price) diff --git a/cointop/layout.go b/cointop/layout.go index ad500b0..37e1924 100644 --- a/cointop/layout.go +++ b/cointop/layout.go @@ -12,6 +12,7 @@ func (ct *Cointop) layout(g *gocui.Gui) error { maxX, maxY := ct.size() topOffset := 0 + headerHeight := 1 marketbarHeight := 1 chartHeight := 10 statusbarHeight := 1 @@ -86,7 +87,7 @@ func (ct *Cointop) layout(g *gocui.Gui) error { go ct.updateHeaders() } - topOffset = topOffset + 1 + topOffset = topOffset + headerHeight if v, err := g.SetView(ct.tableviewname, 0, topOffset, ct.maxtablewidth, maxY-statusbarHeight); err != nil { if err != gocui.ErrUnknownView { return err