2
0
mirror of https://github.com/miguelmota/cointop synced 2024-11-10 13:10:26 +00:00

Add refresh rate flag. Closes #23

This commit is contained in:
Miguel Mota 2019-06-27 21:16:40 -07:00
parent 9c465bf3ca
commit 065ab017ec
No known key found for this signature in database
GPG Key ID: 67EC1161588A00F9
5 changed files with 52 additions and 5 deletions

View File

@ -402,6 +402,7 @@ currency = "USD"
defaultView = "" defaultView = ""
api = "coingecko" api = "coingecko"
colorscheme = "cointop" colorscheme = "cointop"
refresh_rate = 60
[shortcuts] [shortcuts]
"$" = "last_page" "$" = "last_page"
@ -597,7 +598,15 @@ Frequently asked questions:
- Q: How often is the data polled? - 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? - Q: I ran cointop for the first time and don't see any data?

View File

@ -19,13 +19,22 @@ func Run() {
flag.BoolVar(&hideMarketbar, "hide-marketbar", false, "Hide the top marketbar") flag.BoolVar(&hideMarketbar, "hide-marketbar", false, "Hide the top marketbar")
flag.BoolVar(&hideChart, "hide-chart", false, "Hide the chart view") flag.BoolVar(&hideChart, "hide-chart", false, "Hide the chart view")
flag.BoolVar(&hideStatusbar, "hide-statusbar", false, "Hide the bottom statusbar") 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(&config, "config", "", "Config filepath. Default is ~/.cointop/config.toml")
flag.StringVar(&cmcAPIKey, "coinmarketcap-api-key", "", "Set the 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 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.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() flag.Parse()
var refreshRate *uint
if refreshRateFlag != nil {
if *refreshRateFlag > -1 {
t := uint(*refreshRateFlag)
refreshRate = &t
}
}
if v || ver { if v || ver {
fmt.Printf("cointop v%s", cointop.Version()) fmt.Printf("cointop v%s", cointop.Version())
} else if test { } else if test {
@ -44,6 +53,7 @@ func Run() {
HideChart: hideChart, HideChart: hideChart,
HideStatusbar: hideStatusbar, HideStatusbar: hideStatusbar,
OnlyTable: onlyTable, OnlyTable: onlyTable,
RefreshRate: refreshRate,
}).Run() }).Run()
} }
} }

View File

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

View File

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

View File

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