Ability to have sort col by view

pull/281/head
Vuong 3 years ago
parent fdc9664842
commit 08c7a026cc
No known key found for this signature in database
GPG Key ID: 9E32EEF440B0A5D4

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

@ -631,7 +631,7 @@ func (ct *Cointop) SetPortfolioHoldings() error {
// PortfolioEntry returns a portfolio entry // PortfolioEntry returns a portfolio entry
func (ct *Cointop) PortfolioEntry(c *Coin) (*PortfolioEntry, bool) { func (ct *Cointop) PortfolioEntry(c *Coin) (*PortfolioEntry, bool) {
//log.Debug("PortfolioEntry()") // too many // log.Debug("PortfolioEntry()") // too many
if c == nil { if c == nil {
return &PortfolioEntry{}, true 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) 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 { if _, ok := outputFormats[format]; !ok {

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

@ -80,16 +80,10 @@ func (ct *Cointop) UpdateTable() error {
} else if ct.IsPortfolioVisible() { } else if ct.IsPortfolioVisible() {
ct.State.coins = ct.GetPortfolioSlice() ct.State.coins = ct.GetPortfolioSlice()
} else { } 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.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() go ct.RefreshTable()
return nil return nil
} }
@ -274,6 +268,11 @@ func (ct *Cointop) ToggleTableFullscreen() error {
func (ct *Cointop) SetSelectedView(viewName string) { func (ct *Cointop) SetSelectedView(viewName string) {
ct.State.lastSelectedView = ct.State.selectedView ct.State.lastSelectedView = ct.State.selectedView
ct.State.selectedView = viewName ct.State.selectedView = viewName
// init sort constraint for the view in case user travels it in the first time
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 // ToggleSelectedView toggles between current table view and last selected table view

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

Loading…
Cancel
Save