From 08c7a026cc1ca7ed0d3dad51a8042663d60a6870 Mon Sep 17 00:00:00 2001 From: Vuong <3168632+vuon9@users.noreply.github.com> Date: Wed, 17 Nov 2021 21:03:42 +0700 Subject: [PATCH] Ability to have sort col by view --- cointop/cointop.go | 19 +++++++++++++------ cointop/list.go | 2 +- cointop/portfolio.go | 4 ++-- cointop/sort.go | 35 ++++++++++++++++++++++------------- cointop/table.go | 13 ++++++------- cointop/table_header.go | 8 ++++---- 6 files changed, 48 insertions(+), 33 deletions(-) diff --git a/cointop/cointop.go b/cointop/cointop.go index fa47dda..42add5d 100644 --- a/cointop/cointop.go +++ b/cointop/cointop.go @@ -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 @@ -284,9 +288,12 @@ func NewCointop(config *Config) (*Cointop, error) { refreshRate: 60 * time.Second, selectedChartRange: DefaultChartRange, shortcutKeys: DefaultShortcuts(), - sortBy: DefaultSortBy, - page: 0, - perPage: int(perPage), + selectedView: CoinsView, + viewSorts: map[string]*sortConstraint{ + CoinsView: {DefaultSortBy, false}, + }, + page: 0, + perPage: int(perPage), portfolio: &Portfolio{ Entries: make(map[string]*PortfolioEntry), }, @@ -452,7 +459,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] } diff --git a/cointop/list.go b/cointop/list.go index 12081d5..020464c 100644 --- a/cointop/list.go +++ b/cointop/list.go @@ -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() }) } diff --git a/cointop/portfolio.go b/cointop/portfolio.go index f6613fb..3b01879 100644 --- a/cointop/portfolio.go +++ b/cointop/portfolio.go @@ -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 { diff --git a/cointop/sort.go b/cointop/sort.go index e8fd44d..e6d8a54 100644 --- a/cointop/sort.go +++ b/cointop/sort.go @@ -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 = true 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 } } diff --git a/cointop/table.go b/cointop/table.go index b8477d6..0622cef 100644 --- a/cointop/table.go +++ b/cointop/table.go @@ -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 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 diff --git a/cointop/table_header.go b/cointop/table_header.go index 4add749..7a84977 100644 --- a/cointop/table_header.go +++ b/cointop/table_header.go @@ -217,12 +217,12 @@ func (ct *Cointop) UpdateTableHeader() error { arrow := " " colorfn := baseColor if !noSort { - if ct.State.sortBy == col { + corSortCons := ct.State.viewSorts[ct.State.selectedView] + if corSortCons.sortBy == col { colorfn = ct.colorscheme.TableHeaderColumnActiveSprintf() - if ct.State.sortDesc { + arrow = ArrowUp + if corSortCons.sortDesc { arrow = ArrowDown - } else { - arrow = ArrowUp } } }