Add configurable favorites table columns

pull/94/head
Miguel Mota 3 years ago
parent 184ebfb497
commit 673d05a928

@ -46,6 +46,9 @@ func (ct *Cointop) GetCoinsTable() *table.Table {
t := table.NewTable().SetWidth(maxX)
var rows [][]*table.RowCell
headers := ct.GetCoinsTableHeaders()
if ct.IsFavoritesVisible() {
headers = ct.GetFavoritesTableHeaders()
}
ct.ClearSyncMap(ct.State.tableColumnWidths)
ct.ClearSyncMap(ct.State.tableColumnAlignLeft)
for _, coin := range ct.State.coins {

@ -52,6 +52,7 @@ type State struct {
favoritesBySymbol map[string]bool
favorites map[string]bool
favoritesTableColumns []string
helpVisible bool
hideMarketbar bool
hideChart bool
@ -219,19 +220,20 @@ func NewCointop(config *Config) (*Cointop, error) {
coinsTableColumns: DefaultCoinTableHeaders,
currencyConversion: "USD",
// DEPRECATED: favorites by 'symbol' is deprecated because of collisions. Kept for backward compatibility.
favoritesBySymbol: make(map[string]bool),
favorites: make(map[string]bool),
hideMarketbar: config.HideMarketbar,
hideChart: config.HideChart,
hideStatusbar: config.HideStatusbar,
marketBarHeight: 1,
onlyTable: config.OnlyTable,
refreshRate: 60 * time.Second,
selectedChartRange: "7D",
shortcutKeys: DefaultShortcuts(),
sortBy: "rank",
page: 0,
perPage: int(perPage),
favoritesBySymbol: make(map[string]bool),
favorites: make(map[string]bool),
favoritesTableColumns: DefaultCoinTableHeaders,
hideMarketbar: config.HideMarketbar,
hideChart: config.HideChart,
hideStatusbar: config.HideStatusbar,
marketBarHeight: 1,
onlyTable: config.OnlyTable,
refreshRate: 60 * time.Second,
selectedChartRange: "7D",
shortcutKeys: DefaultShortcuts(),
sortBy: "rank",
page: 0,
perPage: int(perPage),
portfolio: &Portfolio{
Entries: make(map[string]*PortfolioEntry),
},

@ -31,18 +31,18 @@ var possibleConfigPaths = []string{
}
type config struct {
Shortcuts map[string]interface{} `toml:"shortcuts"`
Favorites map[string][]interface{} `toml:"favorites"`
Portfolio map[string]interface{} `toml:"portfolio"`
PriceAlerts map[string]interface{} `toml:"price_alerts"`
Currency interface{} `toml:"currency"`
DefaultView interface{} `toml:"default_view"`
CoinMarketCap map[string]interface{} `toml:"coinmarketcap"`
API interface{} `toml:"api"`
Colorscheme interface{} `toml:"colorscheme"`
RefreshRate interface{} `toml:"refresh_rate"`
CacheDir interface{} `toml:"cache_dir"`
Table map[string]interface{} `toml:"table"`
Shortcuts map[string]interface{} `toml:"shortcuts"`
Favorites map[string]interface{} `toml:"favorites"`
Portfolio map[string]interface{} `toml:"portfolio"`
PriceAlerts map[string]interface{} `toml:"price_alerts"`
Currency interface{} `toml:"currency"`
DefaultView interface{} `toml:"default_view"`
CoinMarketCap map[string]interface{} `toml:"coinmarketcap"`
API interface{} `toml:"api"`
Colorscheme interface{} `toml:"colorscheme"`
RefreshRate interface{} `toml:"refresh_rate"`
CacheDir interface{} `toml:"cache_dir"`
Table map[string]interface{} `toml:"table"`
}
// SetupConfig loads config file
@ -219,12 +219,15 @@ func (ct *Cointop) configToToml() ([]byte, error) {
})
var favoritesBySymbolIfc []interface{}
favoritesMapIfc := map[string][]interface{}{
favoritesMapIfc := map[string]interface{}{
// DEPRECATED: favorites by 'symbol' is deprecated because of collisions. Kept for backward compatibility.
"symbols": favoritesBySymbolIfc,
"names": favoritesIfc,
}
var favoritesColumnsIfc interface{} = ct.State.favoritesTableColumns
favoritesMapIfc["columns"] = favoritesColumnsIfc
portfolioIfc := map[string]interface{}{}
var holdingsIfc [][]string
for name := range ct.State.portfolio.Entries {
@ -470,20 +473,40 @@ func (ct *Cointop) loadAPIChoiceFromConfig() error {
// LoadFavoritesFromConfig loads favorites data from config file to struct
func (ct *Cointop) loadFavoritesFromConfig() error {
ct.debuglog("loadFavoritesFromConfig()")
for k, arr := range ct.config.Favorites {
for k, valueIfc := range ct.config.Favorites {
ifcs, ok := valueIfc.([]interface{})
if !ok {
continue
}
switch k {
// DEPRECATED: favorites by 'symbol' is deprecated because of collisions. Kept for backward compatibility.
if k == "symbols" {
for _, ifc := range arr {
case "symbols":
for _, ifc := range ifcs {
if v, ok := ifc.(string); ok {
ct.State.favoritesBySymbol[strings.ToUpper(v)] = true
}
}
} else if k == "names" {
for _, ifc := range arr {
case "names":
for _, ifc := range ifcs {
if v, ok := ifc.(string); ok {
ct.State.favorites[v] = true
}
}
case "columns":
var columns []string
for _, ifc := range ifcs {
col, ok := ifc.(string)
if !ok {
continue
}
if !ct.ValidCoinsTableHeader(col) {
return fmt.Errorf("invalid table header name %q. Valid names are: %s", col, strings.Join(DefaultCoinTableHeaders, ","))
}
columns = append(columns, col)
}
if len(columns) > 0 {
ct.State.favoritesTableColumns = columns
}
}
}
return nil

@ -4,6 +4,11 @@ import (
"sort"
)
// GetFavoritesTableHeaders returns the favorites table headers
func (ct *Cointop) GetFavoritesTableHeaders() []string {
return ct.State.favoritesTableColumns
}
// ToggleFavorite toggles coin as favorite
func (ct *Cointop) ToggleFavorite() error {
ct.debuglog("toggleFavorite()")

Loading…
Cancel
Save