Merge pull request #281 from vuon9/feature/sort-by-view

Ability to have sort col by view
pull/303/head
Miguel Mota 2 years ago committed by GitHub
commit 6e7130c31d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -35,6 +35,11 @@ type Views struct {
Input *InputView
}
type sortConstraint struct {
sortBy string
sortDesc bool
}
// State is the state preferences of cointop
type State struct {
allCoins []*Coin
@ -77,8 +82,7 @@ type State struct {
selectedView string
lastSelectedView string
shortcutKeys map[string]string
sortDesc bool
sortBy string
viewSorts map[string]*sortConstraint
tableOffsetX int
onlyTable bool
onlyChart bool
@ -280,9 +284,12 @@ func NewCointop(config *Config) (*Cointop, error) {
refreshRate: 60 * time.Second,
selectedChartRange: DefaultChartRange,
shortcutKeys: DefaultShortcuts(),
sortBy: DefaultSortBy,
selectedView: CoinsView,
page: 0,
perPage: int(perPage),
viewSorts: map[string]*sortConstraint{
CoinsView: {DefaultSortBy, false},
},
portfolio: &Portfolio{
Entries: make(map[string]*PortfolioEntry),
},
@ -449,7 +456,7 @@ func NewCointop(config *Config) (*Cointop, error) {
if max > 100 {
max = 100
}
ct.Sort(ct.State.sortBy, ct.State.sortDesc, ct.State.allCoins, false)
ct.Sort(ct.State.viewSorts[ct.State.selectedView], ct.State.allCoins, false)
ct.State.coins = ct.State.allCoins[0:max]
}

@ -174,7 +174,7 @@ func (ct *Cointop) processCoins(coins []types.Coin) {
}
time.AfterFunc(10*time.Millisecond, func() {
ct.Sort(ct.State.sortBy, ct.State.sortDesc, ct.State.coins, true)
ct.Sort(ct.State.viewSorts[ct.State.selectedView], ct.State.coins, true)
ct.UpdateTable()
})
}

@ -631,7 +631,7 @@ func (ct *Cointop) SetPortfolioHoldings() error {
// PortfolioEntry returns a portfolio entry
func (ct *Cointop) PortfolioEntry(c *Coin) (*PortfolioEntry, bool) {
//log.Debug("PortfolioEntry()") // too many
// log.Debug("PortfolioEntry()") // too many
if c == nil {
return &PortfolioEntry{}, true
}
@ -830,7 +830,7 @@ func (ct *Cointop) PrintHoldingsTable(options *TablePrintOptions) error {
return fmt.Errorf("the option %q is not a valid column name", sortBy)
}
ct.Sort(sortBy, sortDesc, holdings, true)
ct.Sort(&sortConstraint{sortBy: sortBy, sortDesc: sortDesc}, holdings, true)
}
if _, ok := outputFormats[format]; !ok {

@ -11,12 +11,12 @@ import (
var sortlock sync.Mutex
// Sort sorts the list of coins
func (ct *Cointop) Sort(sortBy string, desc bool, list []*Coin, renderHeaders bool) {
func (ct *Cointop) Sort(sortCons *sortConstraint, list []*Coin, renderHeaders bool) {
log.Debug("Sort()")
sortlock.Lock()
defer sortlock.Unlock()
ct.State.sortBy = sortBy
ct.State.sortDesc = desc
ct.State.viewSorts[ct.State.selectedView] = sortCons
if list == nil {
return
}
@ -24,7 +24,7 @@ func (ct *Cointop) Sort(sortBy string, desc bool, list []*Coin, renderHeaders bo
return
}
sort.SliceStable(list[:], func(i, j int) bool {
if ct.State.sortDesc {
if sortCons.sortDesc {
i, j = j, i
}
a := list[i]
@ -35,7 +35,7 @@ func (ct *Cointop) Sort(sortBy string, desc bool, list []*Coin, renderHeaders bo
if b == nil {
return false
}
switch sortBy {
switch sortCons.sortBy {
case "rank":
return a.Rank < b.Rank
case "name":
@ -89,7 +89,7 @@ func (ct *Cointop) Sort(sortBy string, desc bool, list []*Coin, renderHeaders bo
// SortAsc sorts list of coins in ascending order
func (ct *Cointop) SortAsc() error {
log.Debug("SortAsc()")
ct.State.sortDesc = false
ct.State.viewSorts[ct.State.selectedView].sortDesc = false
ct.UpdateTable()
return nil
}
@ -97,7 +97,7 @@ func (ct *Cointop) SortAsc() error {
// SortDesc sorts list of coins in descending order
func (ct *Cointop) SortDesc() error {
log.Debug("SortDesc()")
ct.State.sortDesc = true
ct.State.viewSorts[ct.State.selectedView].sortDesc = true
ct.UpdateTable()
return nil
}
@ -112,7 +112,10 @@ func (ct *Cointop) SortPrevCol() error {
k = 0
}
nextsortBy := cols[k]
ct.Sort(nextsortBy, ct.State.sortDesc, ct.State.coins, true)
curSortConst := ct.State.viewSorts[ct.State.selectedView]
curSortConst.sortBy = nextsortBy
ct.Sort(curSortConst, ct.State.coins, true)
ct.UpdateTable()
return nil
}
@ -128,7 +131,9 @@ func (ct *Cointop) SortNextCol() error {
k = l - 1
}
nextsortBy := cols[k]
ct.Sort(nextsortBy, ct.State.sortDesc, ct.State.coins, true)
curSortCons := ct.State.viewSorts[ct.State.selectedView]
curSortCons.sortBy = nextsortBy
ct.Sort(curSortCons, ct.State.coins, true)
ct.UpdateTable()
return nil
}
@ -136,11 +141,15 @@ func (ct *Cointop) SortNextCol() error {
// SortToggle toggles the sort order
func (ct *Cointop) SortToggle(sortBy string, desc bool) error {
log.Debug("SortToggle()")
if ct.State.sortBy == sortBy {
desc = !ct.State.sortDesc
curSortCons := ct.State.viewSorts[ct.State.selectedView]
if curSortCons.sortBy == sortBy {
curSortCons.sortDesc = !curSortCons.sortDesc
} else {
curSortCons.sortBy = sortBy
curSortCons.sortDesc = desc
}
ct.Sort(sortBy, desc, ct.State.coins, true)
ct.Sort(curSortCons, ct.State.coins, true)
ct.UpdateTable()
return nil
}
@ -169,7 +178,7 @@ func (ct *Cointop) GetSortColIndex() int {
log.Debug("GetSortColIndex()")
cols := ct.GetActiveTableHeaders()
for i, col := range cols {
if ct.State.sortBy == col {
if ct.State.viewSorts[ct.State.selectedView].sortBy == col {
return i
}
}

@ -80,16 +80,10 @@ func (ct *Cointop) UpdateTable() error {
} else if ct.IsPortfolioVisible() {
ct.State.coins = ct.GetPortfolioSlice()
} else {
// TODO: maintain state of previous sorting
if ct.State.sortBy == "holdings" {
ct.State.sortBy = "rank"
ct.State.sortDesc = false
}
ct.State.coins = ct.GetTableCoinsSlice()
}
ct.Sort(ct.State.sortBy, ct.State.sortDesc, ct.State.coins, true)
ct.Sort(ct.State.viewSorts[ct.State.selectedView], ct.State.coins, true)
go ct.RefreshTable()
return nil
}
@ -274,6 +268,11 @@ func (ct *Cointop) ToggleTableFullscreen() error {
func (ct *Cointop) SetSelectedView(viewName string) {
ct.State.lastSelectedView = ct.State.selectedView
ct.State.selectedView = viewName
// init sort constraint for the view if it hasn't been seen before
if _, found := ct.State.viewSorts[viewName]; !found {
ct.State.viewSorts[viewName] = &sortConstraint{DefaultSortBy, false}
}
}
// ToggleSelectedView toggles between current table view and last selected table view

@ -217,12 +217,12 @@ func (ct *Cointop) UpdateTableHeader() error {
arrow := " "
colorfn := baseColor
if !noSort {
if ct.State.sortBy == col {
currentSortCons := ct.State.viewSorts[ct.State.selectedView]
if currentSortCons.sortBy == col {
colorfn = ct.colorscheme.TableHeaderColumnActiveSprintf()
if ct.State.sortDesc {
arrow = ArrowUp
if currentSortCons.sortDesc {
arrow = ArrowDown
} else {
arrow = ArrowUp
}
}
}

Loading…
Cancel
Save