Add refresh rate flag. Closes #23

pull/38/head
Miguel Mota 5 years ago
parent 9c465bf3ca
commit 065ab017ec
No known key found for this signature in database
GPG Key ID: 67EC1161588A00F9

@ -402,6 +402,7 @@ currency = "USD"
defaultView = ""
api = "coingecko"
colorscheme = "cointop"
refresh_rate = 60
[shortcuts]
"$" = "last_page"
@ -597,7 +598,15 @@ Frequently asked questions:
- Q: How often is the data polled?
- A: Data gets polled once every minute by default. You can press <kbd>Ctrl</kbd>+<kbd>r</kbd> to force refresh.
- A: Data gets polled once 60 seconds by default. You can press <kbd>Ctrl</kbd>+<kbd>r</kbd> to force refresh. You can configure the refresh rate with the flag `--refresh-rate <seconds>`
- Q: How can I change the refresh rate?
- A: Run cointop with the flag `--refresh-rate 60` where the value is the number of seconds that it will fetch for data. You can also set the refresh rate in the config file:
```toml
refresh_rate = 60
```
- Q: I ran cointop for the first time and don't see any data?

@ -19,13 +19,22 @@ func Run() {
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.BoolVar(&onlyTable, "only-table", false, "Show only the table. Hides the chart and top and bottom bars")
refreshRateFlag := flag.Int("refresh-rate", -1, "Refresh rate in seconds. Set to 0 to not auto-refresh. Default is 60")
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 to use. Default is \"cointop\". To install standard themes, do:\n\ngit clone git@github.com:cointop-sh/colors.git ~/.cointop/colors\n\nFor additional instructions, visit: https://github.com/cointop-sh/colors")
flag.Parse()
var refreshRate *uint
if refreshRateFlag != nil {
if *refreshRateFlag > -1 {
t := uint(*refreshRateFlag)
refreshRate = &t
}
}
if v || ver {
fmt.Printf("cointop v%s", cointop.Version())
} else if test {
@ -44,6 +53,7 @@ func Run() {
HideChart: hideChart,
HideStatusbar: hideStatusbar,
OnlyTable: onlyTable,
RefreshRate: refreshRate,
}).Run()
}
}

@ -58,7 +58,8 @@ type Cointop struct {
page int
perpage int
refreshmux sync.Mutex
refreshticker *time.Ticker
refreshRate time.Duration
refreshTicker *time.Ticker
forcerefresh chan bool
selectedcoin *Coin
actionsmap map[string]bool
@ -127,6 +128,7 @@ type Config struct {
HideChart bool
HideStatusbar bool
OnlyTable bool
RefreshRate *uint
}
// apiKeys is api keys structure
@ -155,7 +157,6 @@ func NewCointop(config *Config) *Cointop {
apiChoice: CoinGecko,
allcoinsslugmap: make(map[string]*Coin),
allcoins: []*Coin{},
refreshticker: time.NewTicker(1 * time.Minute),
sortby: "rank",
page: 0,
perpage: 100,
@ -232,6 +233,7 @@ func NewCointop(config *Config) *Cointop {
hideChart: config.HideChart,
hideStatusbar: config.HideStatusbar,
onlyTable: config.OnlyTable,
refreshRate: 60 * time.Second,
}
err := ct.setupConfig()
@ -239,6 +241,17 @@ func NewCointop(config *Config) *Cointop {
log.Fatal(err)
}
if config.RefreshRate != nil {
ct.refreshRate = time.Duration(*config.RefreshRate) * time.Second
}
if ct.refreshRate == 0 {
ct.refreshTicker = time.NewTicker(time.Duration(1))
ct.refreshTicker.Stop()
} else {
ct.refreshTicker = time.NewTicker(ct.refreshRate)
}
// prompt for CoinMarketCap api key if not found
if config.CoinMarketCapAPIKey != "" {
ct.apiKeys.cmc = config.CoinMarketCapAPIKey

@ -6,6 +6,7 @@ import (
"io/ioutil"
"os"
"strings"
"time"
"github.com/BurntSushi/toml"
)
@ -21,6 +22,7 @@ type config struct {
CoinMarketCap map[string]interface{} `toml:"coinmarketcap"`
API interface{} `toml:"api"`
Colorscheme interface{} `toml:"colorscheme"`
RefreshRate interface{} `toml:"refresh_rate"`
}
func (ct *Cointop) setupConfig() error {
@ -54,6 +56,9 @@ func (ct *Cointop) setupConfig() error {
if err := ct.loadColorschemeFromConfig(); err != nil {
return err
}
if err := ct.loadRefreshRateFromConfig(); err != nil {
return err
}
return nil
}
@ -181,6 +186,7 @@ func (ct *Cointop) configToToml() ([]byte, error) {
var currencyIfc interface{} = ct.currencyconversion
var defaultViewIfc interface{} = ct.defaultView
var colorschemeIfc interface{} = ct.colorschemename
var refreshRateIfc interface{} = uint(ct.refreshRate.Seconds())
cmcIfc := map[string]interface{}{
"pro_api_key": ct.apiKeys.cmc,
@ -194,6 +200,7 @@ func (ct *Cointop) configToToml() ([]byte, error) {
Currency: currencyIfc,
DefaultView: defaultViewIfc,
Favorites: favoritesIfcs,
RefreshRate: refreshRateIfc,
Shortcuts: shortcutsIfcs,
Portfolio: portfolioIfc,
}
@ -268,6 +275,14 @@ func (ct *Cointop) loadColorschemeFromConfig() error {
return nil
}
func (ct *Cointop) loadRefreshRateFromConfig() error {
if refreshRate, ok := ct.config.RefreshRate.(int64); ok {
ct.refreshRate = time.Duration(uint(refreshRate)) * time.Second
}
return nil
}
func (ct *Cointop) getColorschemeColors() (map[string]interface{}, error) {
var colors map[string]interface{}
if ct.colorschemename == "" {

@ -213,7 +213,7 @@ func (ct *Cointop) intervalFetchData() {
select {
case <-ct.forcerefresh:
ct.refreshAll()
case <-ct.refreshticker.C:
case <-ct.refreshTicker.C:
ct.refreshAll()
}
}

Loading…
Cancel
Save