Skip to content

Commit 6e7130c

Browse files
authored
Merge pull request cointop-sh#281 from vuon9/feature/sort-by-view
Ability to have sort col by view
2 parents 6c44ff1 + 06c56b5 commit 6e7130c

File tree

6 files changed

+46
-31
lines changed

6 files changed

+46
-31
lines changed

cointop/cointop.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ type Views struct {
3535
Input *InputView
3636
}
3737

38+
type sortConstraint struct {
39+
sortBy string
40+
sortDesc bool
41+
}
42+
3843
// State is the state preferences of cointop
3944
type State struct {
4045
allCoins []*Coin
@@ -77,8 +82,7 @@ type State struct {
7782
selectedView string
7883
lastSelectedView string
7984
shortcutKeys map[string]string
80-
sortDesc bool
81-
sortBy string
85+
viewSorts map[string]*sortConstraint
8286
tableOffsetX int
8387
onlyTable bool
8488
onlyChart bool
@@ -280,9 +284,12 @@ func NewCointop(config *Config) (*Cointop, error) {
280284
refreshRate: 60 * time.Second,
281285
selectedChartRange: DefaultChartRange,
282286
shortcutKeys: DefaultShortcuts(),
283-
sortBy: DefaultSortBy,
287+
selectedView: CoinsView,
284288
page: 0,
285289
perPage: int(perPage),
290+
viewSorts: map[string]*sortConstraint{
291+
CoinsView: {DefaultSortBy, false},
292+
},
286293
portfolio: &Portfolio{
287294
Entries: make(map[string]*PortfolioEntry),
288295
},
@@ -449,7 +456,7 @@ func NewCointop(config *Config) (*Cointop, error) {
449456
if max > 100 {
450457
max = 100
451458
}
452-
ct.Sort(ct.State.sortBy, ct.State.sortDesc, ct.State.allCoins, false)
459+
ct.Sort(ct.State.viewSorts[ct.State.selectedView], ct.State.allCoins, false)
453460
ct.State.coins = ct.State.allCoins[0:max]
454461
}
455462

cointop/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func (ct *Cointop) processCoins(coins []types.Coin) {
174174
}
175175

176176
time.AfterFunc(10*time.Millisecond, func() {
177-
ct.Sort(ct.State.sortBy, ct.State.sortDesc, ct.State.coins, true)
177+
ct.Sort(ct.State.viewSorts[ct.State.selectedView], ct.State.coins, true)
178178
ct.UpdateTable()
179179
})
180180
}

cointop/portfolio.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ func (ct *Cointop) SetPortfolioHoldings() error {
631631

632632
// PortfolioEntry returns a portfolio entry
633633
func (ct *Cointop) PortfolioEntry(c *Coin) (*PortfolioEntry, bool) {
634-
//log.Debug("PortfolioEntry()") // too many
634+
// log.Debug("PortfolioEntry()") // too many
635635
if c == nil {
636636
return &PortfolioEntry{}, true
637637
}
@@ -830,7 +830,7 @@ func (ct *Cointop) PrintHoldingsTable(options *TablePrintOptions) error {
830830
return fmt.Errorf("the option %q is not a valid column name", sortBy)
831831
}
832832

833-
ct.Sort(sortBy, sortDesc, holdings, true)
833+
ct.Sort(&sortConstraint{sortBy: sortBy, sortDesc: sortDesc}, holdings, true)
834834
}
835835

836836
if _, ok := outputFormats[format]; !ok {

cointop/sort.go

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@ import (
1111
var sortlock sync.Mutex
1212

1313
// Sort sorts the list of coins
14-
func (ct *Cointop) Sort(sortBy string, desc bool, list []*Coin, renderHeaders bool) {
14+
func (ct *Cointop) Sort(sortCons *sortConstraint, list []*Coin, renderHeaders bool) {
1515
log.Debug("Sort()")
1616
sortlock.Lock()
1717
defer sortlock.Unlock()
18-
ct.State.sortBy = sortBy
19-
ct.State.sortDesc = desc
18+
19+
ct.State.viewSorts[ct.State.selectedView] = sortCons
2020
if list == nil {
2121
return
2222
}
2323
if len(list) < 2 {
2424
return
2525
}
2626
sort.SliceStable(list[:], func(i, j int) bool {
27-
if ct.State.sortDesc {
27+
if sortCons.sortDesc {
2828
i, j = j, i
2929
}
3030
a := list[i]
@@ -35,7 +35,7 @@ func (ct *Cointop) Sort(sortBy string, desc bool, list []*Coin, renderHeaders bo
3535
if b == nil {
3636
return false
3737
}
38-
switch sortBy {
38+
switch sortCons.sortBy {
3939
case "rank":
4040
return a.Rank < b.Rank
4141
case "name":
@@ -89,15 +89,15 @@ func (ct *Cointop) Sort(sortBy string, desc bool, list []*Coin, renderHeaders bo
8989
// SortAsc sorts list of coins in ascending order
9090
func (ct *Cointop) SortAsc() error {
9191
log.Debug("SortAsc()")
92-
ct.State.sortDesc = false
92+
ct.State.viewSorts[ct.State.selectedView].sortDesc = false
9393
ct.UpdateTable()
9494
return nil
9595
}
9696

9797
// SortDesc sorts list of coins in descending order
9898
func (ct *Cointop) SortDesc() error {
9999
log.Debug("SortDesc()")
100-
ct.State.sortDesc = true
100+
ct.State.viewSorts[ct.State.selectedView].sortDesc = true
101101
ct.UpdateTable()
102102
return nil
103103
}
@@ -112,7 +112,10 @@ func (ct *Cointop) SortPrevCol() error {
112112
k = 0
113113
}
114114
nextsortBy := cols[k]
115-
ct.Sort(nextsortBy, ct.State.sortDesc, ct.State.coins, true)
115+
116+
curSortConst := ct.State.viewSorts[ct.State.selectedView]
117+
curSortConst.sortBy = nextsortBy
118+
ct.Sort(curSortConst, ct.State.coins, true)
116119
ct.UpdateTable()
117120
return nil
118121
}
@@ -128,19 +131,25 @@ func (ct *Cointop) SortNextCol() error {
128131
k = l - 1
129132
}
130133
nextsortBy := cols[k]
131-
ct.Sort(nextsortBy, ct.State.sortDesc, ct.State.coins, true)
134+
curSortCons := ct.State.viewSorts[ct.State.selectedView]
135+
curSortCons.sortBy = nextsortBy
136+
ct.Sort(curSortCons, ct.State.coins, true)
132137
ct.UpdateTable()
133138
return nil
134139
}
135140

136141
// SortToggle toggles the sort order
137142
func (ct *Cointop) SortToggle(sortBy string, desc bool) error {
138143
log.Debug("SortToggle()")
139-
if ct.State.sortBy == sortBy {
140-
desc = !ct.State.sortDesc
144+
curSortCons := ct.State.viewSorts[ct.State.selectedView]
145+
if curSortCons.sortBy == sortBy {
146+
curSortCons.sortDesc = !curSortCons.sortDesc
147+
} else {
148+
curSortCons.sortBy = sortBy
149+
curSortCons.sortDesc = desc
141150
}
142151

143-
ct.Sort(sortBy, desc, ct.State.coins, true)
152+
ct.Sort(curSortCons, ct.State.coins, true)
144153
ct.UpdateTable()
145154
return nil
146155
}
@@ -169,7 +178,7 @@ func (ct *Cointop) GetSortColIndex() int {
169178
log.Debug("GetSortColIndex()")
170179
cols := ct.GetActiveTableHeaders()
171180
for i, col := range cols {
172-
if ct.State.sortBy == col {
181+
if ct.State.viewSorts[ct.State.selectedView].sortBy == col {
173182
return i
174183
}
175184
}

cointop/table.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,10 @@ func (ct *Cointop) UpdateTable() error {
8080
} else if ct.IsPortfolioVisible() {
8181
ct.State.coins = ct.GetPortfolioSlice()
8282
} else {
83-
// TODO: maintain state of previous sorting
84-
if ct.State.sortBy == "holdings" {
85-
ct.State.sortBy = "rank"
86-
ct.State.sortDesc = false
87-
}
88-
8983
ct.State.coins = ct.GetTableCoinsSlice()
9084
}
9185

92-
ct.Sort(ct.State.sortBy, ct.State.sortDesc, ct.State.coins, true)
86+
ct.Sort(ct.State.viewSorts[ct.State.selectedView], ct.State.coins, true)
9387
go ct.RefreshTable()
9488
return nil
9589
}
@@ -274,6 +268,11 @@ func (ct *Cointop) ToggleTableFullscreen() error {
274268
func (ct *Cointop) SetSelectedView(viewName string) {
275269
ct.State.lastSelectedView = ct.State.selectedView
276270
ct.State.selectedView = viewName
271+
272+
// init sort constraint for the view if it hasn't been seen before
273+
if _, found := ct.State.viewSorts[viewName]; !found {
274+
ct.State.viewSorts[viewName] = &sortConstraint{DefaultSortBy, false}
275+
}
277276
}
278277

279278
// ToggleSelectedView toggles between current table view and last selected table view

cointop/table_header.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,12 +217,12 @@ func (ct *Cointop) UpdateTableHeader() error {
217217
arrow := " "
218218
colorfn := baseColor
219219
if !noSort {
220-
if ct.State.sortBy == col {
220+
currentSortCons := ct.State.viewSorts[ct.State.selectedView]
221+
if currentSortCons.sortBy == col {
221222
colorfn = ct.colorscheme.TableHeaderColumnActiveSprintf()
222-
if ct.State.sortDesc {
223+
arrow = ArrowUp
224+
if currentSortCons.sortDesc {
223225
arrow = ArrowDown
224-
} else {
225-
arrow = ArrowUp
226226
}
227227
}
228228
}

0 commit comments

Comments
 (0)