Skip to content

Commit

Permalink
Separate out different tables
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelmota committed Dec 4, 2020
1 parent 187f7a0 commit 415b4c1
Show file tree
Hide file tree
Showing 16 changed files with 387 additions and 292 deletions.
2 changes: 1 addition & 1 deletion cointop/chart.go
Expand Up @@ -61,7 +61,7 @@ func (ct *Cointop) UpdateChart() error {
chartLock.Lock()
defer chartLock.Unlock()

if ct.State.portfolioVisible {
if ct.IsPortfolioVisible() {
if err := ct.PortfolioChart(); err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cointop/coin.go
Expand Up @@ -26,7 +26,7 @@ type Coin struct {
// AllCoins returns a slice of all the coins
func (ct *Cointop) AllCoins() []*Coin {
ct.debuglog("AllCoins()")
if ct.State.filterByFavorites {
if ct.IsFavoritesVisible() {
var list []*Coin
for i := range ct.State.allCoins {
coin := ct.State.allCoins[i]
Expand All @@ -37,7 +37,7 @@ func (ct *Cointop) AllCoins() []*Coin {
return list
}

if ct.State.portfolioVisible {
if ct.IsPortfolioVisible() {
var list []*Coin
for i := range ct.State.allCoins {
coin := ct.State.allCoins[i]
Expand Down
168 changes: 168 additions & 0 deletions cointop/coins_table.go
@@ -0,0 +1,168 @@
package cointop

import (
"fmt"
"strconv"
"time"

"github.com/miguelmota/cointop/pkg/humanize"
"github.com/miguelmota/cointop/pkg/table"
)

// GetCoinsTableHeaders returns the coins table headers
func (ct *Cointop) GetCoinsTableHeaders() []string {
return []string{
"rank",
"name",
"symbol",
"price",
"marketcap",
"24hvolume",
"1hchange",
"24hchange",
"7dchange",
"totalsupply",
"availablesupply",
"lastupdated",
}
}

// GetCoinsTable returns the table for diplaying the coins
func (ct *Cointop) GetCoinsTable() *table.Table {
maxX := ct.width()
t := table.NewTable().SetWidth(maxX)
for _, coin := range ct.State.coins {
if coin == nil {
continue
}
star := ct.colorscheme.TableRow(" ")
if coin.Favorite {
star = ct.colorscheme.TableRowFavorite("*")
}
rank := fmt.Sprintf("%s%v", star, ct.colorscheme.TableRow(fmt.Sprintf("%6v ", coin.Rank)))
name := TruncateString(coin.Name, 20)
symbol := TruncateString(coin.Symbol, 6)
symbolpadding := 8
// NOTE: this is to adjust padding by 1 because when all name rows are
// yellow it messes the spacing (need to debug)
if ct.IsFavoritesVisible() {
symbolpadding++
}
namecolor := ct.colorscheme.TableRow
color1h := ct.colorscheme.TableColumnChange
color24h := ct.colorscheme.TableColumnChange
color7d := ct.colorscheme.TableColumnChange
if coin.Favorite {
namecolor = ct.colorscheme.TableRowFavorite
}
if coin.PercentChange1H > 0 {
color1h = ct.colorscheme.TableColumnChangeUp
}
if coin.PercentChange1H < 0 {
color1h = ct.colorscheme.TableColumnChangeDown
}
if coin.PercentChange24H > 0 {
color24h = ct.colorscheme.TableColumnChangeUp
}
if coin.PercentChange24H < 0 {
color24h = ct.colorscheme.TableColumnChangeDown
}
if coin.PercentChange7D > 0 {
color7d = ct.colorscheme.TableColumnChangeUp
}
if coin.PercentChange7D < 0 {
color7d = ct.colorscheme.TableColumnChangeDown
}
unix, _ := strconv.ParseInt(coin.LastUpdated, 10, 64)
lastUpdated := time.Unix(unix, 0).Format("15:04:05 Jan 02")
t.AddRowCells(
&table.RowCell{
LeftMargin: 0,
Width: 6,
LeftAlign: false,
Color: ct.colorscheme.Default,
Text: rank,
},
&table.RowCell{
LeftMargin: 1,
Width: 22,
LeftAlign: true,
Color: namecolor,
Text: name,
},
&table.RowCell{
LeftMargin: 1,
Width: symbolpadding,
LeftAlign: true,
Color: ct.colorscheme.TableRow,
Text: symbol,
},
&table.RowCell{
LeftMargin: 1,
Width: 12,
LeftAlign: false,
Color: ct.colorscheme.TableColumnPrice,
Text: humanize.Commaf(coin.Price),
},
&table.RowCell{
LeftMargin: 1,
Width: 18,
LeftAlign: false,
Color: ct.colorscheme.TableRow,
Text: humanize.Commaf(coin.MarketCap),
},
&table.RowCell{
LeftMargin: 1,
Width: 16,
LeftAlign: false,
Color: ct.colorscheme.TableRow,
Text: humanize.Commaf(coin.Volume24H),
},
&table.RowCell{
LeftMargin: 1,
Width: 11,
LeftAlign: false,
Color: color1h,
Text: fmt.Sprintf("%.2f%%", coin.PercentChange1H),
},
&table.RowCell{
LeftMargin: 1,
Width: 10,
LeftAlign: false,
Color: color24h,
Text: fmt.Sprintf("%.2f%%", coin.PercentChange24H),
},
&table.RowCell{
LeftMargin: 1,
Width: 10,
LeftAlign: false,
Color: color7d,
Text: fmt.Sprintf("%.2f%%", coin.PercentChange7D),
},
&table.RowCell{
LeftMargin: 1,
Width: 22,
LeftAlign: false,
Color: ct.colorscheme.TableRow,
Text: humanize.Commaf(coin.TotalSupply),
},
&table.RowCell{
LeftMargin: 1,
Width: 19,
LeftAlign: false,
Color: ct.colorscheme.TableRow,
Text: humanize.Commaf(coin.AvailableSupply),
},
&table.RowCell{
LeftMargin: 1,
Width: 18,
LeftAlign: false,
Color: ct.colorscheme.TableRow,
Text: lastUpdated,
},
// TODO: add %percent of cap
)
}

return t
}
10 changes: 2 additions & 8 deletions cointop/cointop.go
Expand Up @@ -53,7 +53,6 @@ type State struct {
favoritesBySymbol map[string]bool

favorites map[string]bool
filterByFavorites bool
helpVisible bool
hideMarketbar bool
hideChart bool
Expand All @@ -62,13 +61,14 @@ type State struct {
page int
perPage int
portfolio *Portfolio
portfolioVisible bool
portfolioUpdateMenuVisible bool
refreshRate time.Duration
running bool
searchFieldVisible bool
selectedCoin *Coin
selectedChartRange string
selectedView string
lastSelectedView string
shortcutKeys map[string]string
sortDesc bool
sortBy string
Expand Down Expand Up @@ -107,12 +107,6 @@ type Cointop struct {
Views *Views
}

// CoinMarketCap is API choice
var CoinMarketCap = "coinmarketcap"

// CoinGecko is API choice
var CoinGecko = "coingecko"

// PortfolioEntry is portfolio entry
type PortfolioEntry struct {
Coin string
Expand Down
7 changes: 3 additions & 4 deletions cointop/config.go
Expand Up @@ -284,14 +284,13 @@ func (ct *Cointop) loadDefaultViewFromConfig() error {
defaultView = strings.ToLower(defaultView)
switch defaultView {
case "portfolio":
ct.State.portfolioVisible = true
ct.SetSelectedView(PortfolioView)
case "favorites":
ct.State.filterByFavorites = true
ct.SetSelectedView(FavoritesView)
case "default":
fallthrough
default:
ct.State.portfolioVisible = false
ct.State.filterByFavorites = false
ct.SetSelectedView(CoinsView)
defaultView = "default"
}
ct.State.defaultView = defaultView
Expand Down
16 changes: 16 additions & 0 deletions cointop/constants.go
@@ -0,0 +1,16 @@
package cointop

// CoinMarketCap is API choice
const CoinMarketCap = "coinmarketcap"

// CoinGecko is API choice
const CoinGecko = "coingecko"

// PortfolioView is portfolio table constant
const PortfolioView = "portfolio"

// CoinsView is coins table constant
const CoinsView = "coins"

// FavoritesView is favorites table constant
const FavoritesView = "favorites"
22 changes: 18 additions & 4 deletions cointop/favorites.go
@@ -1,6 +1,8 @@
package cointop

import "sort"
import (
"sort"
)

// ToggleFavorite toggles coin as favorite
func (ct *Cointop) ToggleFavorite() error {
Expand Down Expand Up @@ -28,11 +30,18 @@ func (ct *Cointop) ToggleFavorite() error {
return nil
}

// ToggleShowFavorites toggles the favorites view
// ToggleFavorites toggles the favorites view
func (ct *Cointop) ToggleFavorites() error {
ct.debuglog("toggleFavorites()")
ct.ToggleSelectedView(FavoritesView)
go ct.UpdateTable()
return nil
}

// ToggleShowFavorites shows the favorites view
func (ct *Cointop) ToggleShowFavorites() error {
ct.debuglog("toggleShowFavorites()")
ct.State.portfolioVisible = false
ct.State.filterByFavorites = !ct.State.filterByFavorites
ct.ToggleSelectedView(FavoritesView)
go ct.UpdateTable()
return nil
}
Expand All @@ -58,3 +67,8 @@ func (ct *Cointop) GetFavoritesSlice() []*Coin {

return sliced
}

// IsFavoritesVisible returns true if favorites view is visible
func (ct *Cointop) IsFavoritesVisible() bool {
return ct.State.selectedView == FavoritesView
}
4 changes: 3 additions & 1 deletion cointop/keybindings.go
Expand Up @@ -308,6 +308,8 @@ func (ct *Cointop) Keybindings(g *gocui.Gui) error {
view = ""
case "toggle_favorite":
fn = ct.Keyfn(ct.ToggleFavorite)
case "toggle_favorites":
fn = ct.Keyfn(ct.ToggleFavorites)
case "toggle_show_favorites":
fn = ct.Keyfn(ct.ToggleShowFavorites)
case "save":
Expand Down Expand Up @@ -419,7 +421,7 @@ func (ct *Cointop) Keyfn(fn func() error) func(g *gocui.Gui, v *gocui.View) erro
// handleHkey handles the h key
func (ct *Cointop) handleHkey(key interface{}) func(g *gocui.Gui, v *gocui.View) error {
return func(g *gocui.Gui, v *gocui.View) error {
if k, ok := key.(rune); ok && k == 'h' && ct.State.portfolioVisible {
if k, ok := key.(rune); ok && k == 'h' && ct.IsPortfolioVisible() {
ct.SortToggle("holdings", true)
} else {
ct.PrevPage()
Expand Down
4 changes: 2 additions & 2 deletions cointop/list.go
Expand Up @@ -157,9 +157,9 @@ func (ct *Cointop) processCoins(coins []types.Coin) {
// GetListCount returns count of coins list
func (ct *Cointop) GetListCount() int {
ct.debuglog("getListCount()")
if ct.State.filterByFavorites {
if ct.IsFavoritesVisible() {
return len(ct.State.favorites)
} else if ct.State.portfolioVisible {
} else if ct.IsPortfolioVisible() {
return len(ct.State.portfolio.Entries)
} else {
return len(ct.State.allCoins)
Expand Down
2 changes: 1 addition & 1 deletion cointop/marketbar.go
Expand Up @@ -31,7 +31,7 @@ func (ct *Cointop) UpdateMarketbar() error {
}
var content string

if ct.State.portfolioVisible {
if ct.IsPortfolioVisible() {
total := ct.GetPortfolioTotal()
totalstr := humanize.Commaf(total)
if !(ct.State.currencyConversion == "BTC" || ct.State.currencyConversion == "ETH" || total < 1) {
Expand Down

0 comments on commit 415b4c1

Please sign in to comment.