You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cointop/cointop/sort.go

155 lines
3.3 KiB
Go

package cointop
import (
6 years ago
"sort"
5 years ago
"sync"
6 years ago
"github.com/miguelmota/gocui"
)
5 years ago
var sortlock sync.Mutex
4 years ago
// Sort sorts the list of coins
func (ct *Cointop) Sort(sortBy string, desc bool, list []*Coin, renderHeaders bool) {
ct.debuglog("sort()")
5 years ago
sortlock.Lock()
defer sortlock.Unlock()
if list == nil {
return
}
if len(list) < 2 {
return
}
5 years ago
ct.State.sortBy = sortBy
ct.State.sortDesc = desc
6 years ago
sort.Slice(list[:], func(i, j int) bool {
5 years ago
if ct.State.sortDesc {
i, j = j, i
}
a := list[i]
b := list[j]
if a == nil {
return true
}
if b == nil {
return false
}
5 years ago
switch sortBy {
case "rank":
return a.Rank < b.Rank
case "name":
return a.Name < b.Name
case "symbol":
return a.Symbol < b.Symbol
case "price":
6 years ago
return a.Price < b.Price
6 years ago
case "holdings":
return a.Holdings < b.Holdings
case "balance":
return a.Balance < b.Balance
case "marketcap":
6 years ago
return a.MarketCap < b.MarketCap
case "24hvolume":
6 years ago
return a.Volume24H < b.Volume24H
case "1hchange":
return a.PercentChange1H < b.PercentChange1H
case "24hchange":
return a.PercentChange24H < b.PercentChange24H
case "7dchange":
return a.PercentChange7D < b.PercentChange7D
case "totalsupply":
return a.TotalSupply < b.TotalSupply
case "availablesupply":
return a.AvailableSupply < b.AvailableSupply
case "lastupdated":
return a.LastUpdated < b.LastUpdated
default:
return a.Rank < b.Rank
}
})
5 years ago
if renderHeaders {
ct.UpdateTableHeader()
5 years ago
}
}
4 years ago
// SortAsc sorts list of coins in ascending order
func (ct *Cointop) SortAsc() error {
ct.debuglog("sortAsc()")
5 years ago
ct.State.sortDesc = false
ct.UpdateTable()
return nil
}
4 years ago
// SortDesc sorts list of coins in descending order
func (ct *Cointop) SortDesc() error {
ct.debuglog("sortDesc()")
5 years ago
ct.State.sortDesc = true
ct.UpdateTable()
return nil
}
4 years ago
// SortPrevCol sorts the previous column
func (ct *Cointop) SortPrevCol() error {
ct.debuglog("sortPrevCol()")
nextsortBy := ct.TableColumnOrder[0]
4 years ago
i := ct.GetSortColIndex()
k := i - 1
if k < 0 {
k = 0
}
nextsortBy = ct.TableColumnOrder[k]
4 years ago
ct.Sort(nextsortBy, ct.State.sortDesc, ct.State.coins, true)
ct.UpdateTable()
return nil
}
4 years ago
// SortNextCol sorts the next column
func (ct *Cointop) SortNextCol() error {
ct.debuglog("sortNextCol()")
nextsortBy := ct.TableColumnOrder[0]
l := len(ct.TableColumnOrder)
4 years ago
i := ct.GetSortColIndex()
k := i + 1
if k > l-1 {
k = l - 1
}
nextsortBy = ct.TableColumnOrder[k]
4 years ago
ct.Sort(nextsortBy, ct.State.sortDesc, ct.State.coins, true)
ct.UpdateTable()
return nil
}
5 years ago
4 years ago
// SortToggle toggles the sort order
func (ct *Cointop) SortToggle(sortBy string, desc bool) error {
ct.debuglog("sortToggle()")
5 years ago
if ct.State.sortBy == sortBy {
desc = !ct.State.sortDesc
}
4 years ago
ct.Sort(sortBy, desc, ct.State.coins, true)
ct.UpdateTable()
5 years ago
return nil
}
4 years ago
// Sortfn returns the sort function as a wrapped gocui keybinding function
func (ct *Cointop) Sortfn(sortBy string, desc bool) func(g *gocui.Gui, v *gocui.View) error {
ct.debuglog("sortfn()")
5 years ago
return func(g *gocui.Gui, v *gocui.View) error {
4 years ago
return ct.SortToggle(sortBy, desc)
5 years ago
}
}
4 years ago
// GetSortColIndex gets the sort column index
func (ct *Cointop) GetSortColIndex() int {
ct.debuglog("getSortColIndex()")
for i, col := range ct.TableColumnOrder {
5 years ago
if ct.State.sortBy == col {
return i
}
}
return 0
}