Fetch full price if avaiable in coingecko api. Closes #37

pull/38/head
Miguel Mota 5 years ago
parent 22e5e5a201
commit 599d2a226a
No known key found for this signature in database
GPG Key ID: 67EC1161588A00F9

@ -12,18 +12,18 @@ func Run() {
var v, ver, test, clean, reset, hideMarketbar, hideChart, hideStatusbar, onlyTable bool var v, ver, test, clean, reset, hideMarketbar, hideChart, hideStatusbar, onlyTable bool
var config, cmcAPIKey, apiChoice, colorscheme string var config, cmcAPIKey, apiChoice, colorscheme string
flag.BoolVar(&v, "v", false, "Version") flag.BoolVar(&v, "v", false, "Version")
flag.BoolVar(&ver, "version", false, "Version") flag.BoolVar(&ver, "version", false, "Display current version")
flag.BoolVar(&test, "test", false, "Run test") flag.BoolVar(&test, "test", false, "Run test (for Homebrew)")
flag.BoolVar(&clean, "clean", false, "Clean cache") flag.BoolVar(&clean, "clean", false, "Wipe clean the cache")
flag.BoolVar(&reset, "reset", false, "Reset config") flag.BoolVar(&reset, "reset", false, "Reset the config. Make sure to backup any relevant changes first!")
flag.BoolVar(&hideMarketbar, "hide-marketbar", false, "Hide marketbar") flag.BoolVar(&hideMarketbar, "hide-marketbar", false, "Hide the top marketbar")
flag.BoolVar(&hideChart, "hide-chart", false, "Hide chart view") flag.BoolVar(&hideChart, "hide-chart", false, "Hide the chart view")
flag.BoolVar(&hideStatusbar, "hide-statusbar", false, "Hide statusbar") flag.BoolVar(&hideStatusbar, "hide-statusbar", false, "Hide the bottom statusbar")
flag.BoolVar(&onlyTable, "only-table", false, "Show only the table") flag.BoolVar(&onlyTable, "only-table", false, "Show only the table. Hides the chart and top and bottom bars.")
flag.StringVar(&config, "config", "", "Config filepath") flag.StringVar(&config, "config", "", "Config filepath. Default is ~/.cointop/config.toml")
flag.StringVar(&cmcAPIKey, "coinmarketcap-api-key", "", "CoinMarketCap API key") flag.StringVar(&cmcAPIKey, "coinmarketcap-api-key", "", "Set the CoinMarketCap API key")
flag.StringVar(&apiChoice, "api", cointop.CoinGecko, "API choice") 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() flag.Parse()
if v || ver { if v || ver {

@ -57,7 +57,29 @@ func (s *Service) getLimitedCoinData(convert string, offset int) ([]apitypes.Coi
} }
if list != nil { 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 { 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 percentChange1H float64
var percentChange24H float64 var percentChange24H float64
var percentChange7D float64 var percentChange7D float64
@ -88,7 +110,7 @@ func (s *Service) getLimitedCoinData(convert string, offset int) ([]apitypes.Coi
AvailableSupply: util.FormatSupply(availableSupply), AvailableSupply: util.FormatSupply(availableSupply),
TotalSupply: util.FormatSupply(totalSupply), TotalSupply: util.FormatSupply(totalSupply),
MarketCap: util.FormatMarketCap(item.MarketCap), MarketCap: util.FormatMarketCap(item.MarketCap),
Price: util.FormatPrice(item.CurrentPrice, convert), Price: util.FormatPrice(price, convert),
PercentChange1H: util.FormatPercentChange(percentChange1H), PercentChange1H: util.FormatPercentChange(percentChange1H),
PercentChange24H: util.FormatPercentChange(percentChange24H), PercentChange24H: util.FormatPercentChange(percentChange24H),
PercentChange7D: util.FormatPercentChange(percentChange7D), PercentChange7D: util.FormatPercentChange(percentChange7D),

@ -59,6 +59,7 @@ func FormatRank(rank interface{}) int {
// FormatPrice formats the price value // FormatPrice formats the price value
func FormatPrice(price float64, convert string) float64 { func FormatPrice(price float64, convert string) float64 {
convert = strings.ToUpper(convert)
pricestr := fmt.Sprintf("%.2f", price) pricestr := fmt.Sprintf("%.2f", price)
if convert == "ETH" || convert == "BTC" || price < 1 { if convert == "ETH" || convert == "BTC" || price < 1 {
pricestr = fmt.Sprintf("%.5f", price) pricestr = fmt.Sprintf("%.5f", price)

@ -12,6 +12,7 @@ func (ct *Cointop) layout(g *gocui.Gui) error {
maxX, maxY := ct.size() maxX, maxY := ct.size()
topOffset := 0 topOffset := 0
headerHeight := 1
marketbarHeight := 1 marketbarHeight := 1
chartHeight := 10 chartHeight := 10
statusbarHeight := 1 statusbarHeight := 1
@ -86,7 +87,7 @@ func (ct *Cointop) layout(g *gocui.Gui) error {
go ct.updateHeaders() 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 v, err := g.SetView(ct.tableviewname, 0, topOffset, ct.maxtablewidth, maxY-statusbarHeight); err != nil {
if err != gocui.ErrUnknownView { if err != gocui.ErrUnknownView {
return err return err

Loading…
Cancel
Save