Skip to content

Commit

Permalink
Update holdings config
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelmota committed Jan 29, 2021
1 parent 82eb713 commit 23fe262
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 24 deletions.
6 changes: 4 additions & 2 deletions cointop/cointop.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type State struct {
perPage int
portfolio *Portfolio
portfolioUpdateMenuVisible bool
portfolioTableColumns []string
refreshRate time.Duration
running bool
searchFieldVisible bool
Expand Down Expand Up @@ -230,8 +231,9 @@ func NewCointop(config *Config) (*Cointop, error) {
portfolio: &Portfolio{
Entries: make(map[string]*PortfolioEntry, 0),
},
chartHeight: 10,
tableOffsetX: 0,
portfolioTableColumns: DefaultPortfolioTableHeaders,
chartHeight: 10,
tableOffsetX: 0,
priceAlerts: &PriceAlerts{
Entries: make([]*PriceAlert, 0),
SoundEnabled: true,
Expand Down
62 changes: 51 additions & 11 deletions cointop/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ func (ct *Cointop) SetupConfig() error {
if err := ct.loadTableColumnsFromConfig(); err != nil {
return err
}
if err := ct.loadPortfolioColumnsFromConfig(); err != nil {
return err
}
if err := ct.loadShortcutsFromConfig(); err != nil {
return err
}
Expand Down Expand Up @@ -227,13 +230,24 @@ func (ct *Cointop) configToToml() ([]byte, error) {
}

portfolioIfc := map[string]interface{}{}
var holdingsIfc [][]string
for name := range ct.State.portfolio.Entries {
entry, ok := ct.State.portfolio.Entries[name]
if !ok || entry.Coin == "" {
continue
}
var i interface{} = entry.Holdings
portfolioIfc[entry.Coin] = i
var amount string = strconv.FormatFloat(entry.Holdings, 'f', -1, 64)
var coinName string = entry.Coin
var tuple []string = []string{coinName, amount}
holdingsIfc = append(holdingsIfc, tuple)
}
portfolioIfc["holdings"] = holdingsIfc

if reflect.DeepEqual(DefaultPortfolioTableHeaders, ct.State.portfolioTableColumns) {
portfolioIfc["columns"] = []string{}
} else {
var columnsIfc interface{} = ct.State.portfolioTableColumns
portfolioIfc["columns"] = columnsIfc
}

var currencyIfc interface{} = ct.State.currencyConversion
Expand Down Expand Up @@ -324,6 +338,11 @@ func (ct *Cointop) loadTableColumnsFromConfig() error {
return nil
}

// LoadPortfolioColumnsFromConfig loads preferred portfolio table columns from config file to struct
func (ct *Cointop) loadPortfolioColumnsFromConfig() error {
return nil
}

// LoadShortcutsFromConfig loads keyboard shortcuts from config file to struct
func (ct *Cointop) loadShortcutsFromConfig() error {
ct.debuglog("loadShortcutsFromConfig()")
Expand Down Expand Up @@ -486,19 +505,40 @@ func (ct *Cointop) loadFavoritesFromConfig() error {
// LoadPortfolioFromConfig loads portfolio data from config file to struct
func (ct *Cointop) loadPortfolioFromConfig() error {
ct.debuglog("loadPortfolioFromConfig()")
for name, holdingsIfc := range ct.config.Portfolio {
var holdings float64
var ok bool
if holdings, ok = holdingsIfc.(float64); !ok {
if holdingsInt, ok := holdingsIfc.(int64); ok {
holdings = float64(holdingsInt)

for key, valueIfc := range ct.config.Portfolio {
if key == "columns" {
}
if key == "holdings" {
holdingsIfc, ok := valueIfc.([][]interface{})
if !ok {
continue
}
fmt.Println(holdingsIfc)
}
}
/*
for name, holdingsIfc := range ct.config.Portfolio {
if name == "columns" {
continue
}
if name == "holdings" {
continue
}
if err := ct.SetPortfolioEntry(name, holdings); err != nil {
return err
var holdings float64
var ok bool
if holdings, ok = holdingsIfc.(float64); !ok {
if holdingsInt, ok := holdingsIfc.(int64); ok {
holdings = float64(holdingsInt)
}
}
if err := ct.SetPortfolioEntry(name, holdings); err != nil {
return err
}
}
}
*/

return nil
}
Expand Down
36 changes: 25 additions & 11 deletions cointop/portfolio.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,33 @@ import (
"github.com/miguelmota/cointop/pkg/table"
)

// DefaultPortfolioTableHeaders are the default portfolio table header columns
var DefaultPortfolioTableHeaders = []string{
"rank",
"name",
"symbol",
"price",
"holdings",
"balance",
"24h_change",
"percent_holdings",
"last_updated",
}

// ValidPortfolioTableHeader returns the portfolio table headers
func (ct *Cointop) ValidPortfolioTableHeader(name string) bool {
for _, v := range DefaultPortfolioTableHeaders {
if v == name {
return true
}
}

return false
}

// GetPortfolioTableHeaders returns the portfolio table headers
func (ct *Cointop) GetPortfolioTableHeaders() []string {
return []string{
"rank",
"name",
"symbol",
"price",
"holdings",
"balance",
"24h_change",
"percent_holdings",
"last_updated",
}
return ct.State.portfolioTableColumns
}

// GetPortfolioTable returns the table for displaying portfolio holdings
Expand Down

0 comments on commit 23fe262

Please sign in to comment.