From 565a964e08ab4508643caf83df5563a0b7b5a501 Mon Sep 17 00:00:00 2001 From: Miguel Mota Date: Sun, 1 Apr 2018 13:49:02 -0700 Subject: [PATCH] refresh ticker --- cointop/cointop.go | 36 ++++++++++++++++++++---------------- cointop/layout.go | 29 +++++++++++++++-------------- cointop/sort.go | 12 ++++++------ cointop/table.go | 9 +++++++-- 4 files changed, 48 insertions(+), 38 deletions(-) diff --git a/cointop/cointop.go b/cointop/cointop.go index 25be57d..5abb6fd 100644 --- a/cointop/cointop.go +++ b/cointop/cointop.go @@ -2,6 +2,8 @@ package cointop import ( "log" + "sync" + "time" "github.com/gizak/termui" "github.com/jroimartin/gocui" @@ -21,18 +23,21 @@ var ( // Cointop cointop type Cointop struct { - g *gocui.Gui - marketview *gocui.View - chartview *gocui.View - chartpoints [][]termui.Cell - headersview *gocui.View - tableview *gocui.View - table *table.Table - statusview *gocui.View - sortdesc bool - sortby string - api api.Interface - coins []*apitypes.Coin + g *gocui.Gui + marketview *gocui.View + chartview *gocui.View + chartpoints [][]termui.Cell + headersview *gocui.View + tableview *gocui.View + table *table.Table + statusview *gocui.View + sortdesc bool + sortby string + api api.Interface + coins []*apitypes.Coin + coinsmap map[string]apitypes.Coin + refreshmux sync.Mutex + refreshticker *time.Ticker } // Run runs cointop @@ -46,10 +51,9 @@ func Run() { g.Mouse = true g.Highlight = true ct := Cointop{ - g: g, - api: api.NewCMC(), - sortdesc: true, - sortby: "rank", + g: g, + api: api.NewCMC(), + refreshticker: time.NewTicker(1 * time.Minute), } g.SetManagerFunc(ct.layout) if err := ct.keybindings(g); err != nil { diff --git a/cointop/layout.go b/cointop/layout.go index 313be5a..a900263 100644 --- a/cointop/layout.go +++ b/cointop/layout.go @@ -1,8 +1,6 @@ package cointop import ( - "time" - "github.com/jroimartin/gocui" apitypes "github.com/miguelmota/cointop/pkg/api/types" "github.com/miguelmota/cointop/pkg/pad" @@ -78,6 +76,8 @@ func (ct *Cointop) layout(g *gocui.Gui) error { ct.tableview.SelBgColor = gocui.ColorCyan ct.tableview.SelFgColor = gocui.ColorBlack ct.updateTable() + ct.sort("rank", false) + ct.rowChanged() } if v, err := g.SetView("status", 0, maxY-2, maxX, maxY); err != nil { @@ -91,38 +91,39 @@ func (ct *Cointop) layout(g *gocui.Gui) error { ct.updateStatus("") } - //ct.intervalFetchData() + ct.intervalFetchData() return nil } func (ct *Cointop) updateTable() error { - result := []*apitypes.Coin{} - coins, err := ct.api.GetAllCoinData() + list := []*apitypes.Coin{} + coinsmap, err := ct.api.GetAllCoinData() if err != nil { return err } - for i := range coins { - coin := coins[i] - result = append(result, &coin) + ct.coinsmap = coinsmap + for i := range ct.coinsmap { + coin := ct.coinsmap[i] + list = append(list, &coin) } - ct.coins = result + ct.coins = list ct.sort(ct.sortby, ct.sortdesc) ct.refreshTable() - ct.rowChanged() return nil } func (ct *Cointop) intervalFetchData() { - ticker := time.NewTicker(5 * time.Second) go func() { for { select { - case <-ticker.C: + case <-ct.refreshticker.C: + ct.refreshmux.Lock() ct.updateTable() - //ct.updateMarket() - //ct.updateChart() + ct.updateMarket() + ct.updateChart() + ct.refreshmux.Unlock() } } }() diff --git a/cointop/sort.go b/cointop/sort.go index 36d7d47..566ede1 100644 --- a/cointop/sort.go +++ b/cointop/sort.go @@ -6,12 +6,8 @@ import ( ) func (ct *Cointop) sort(sortby string, desc bool) { - if ct.sortby == sortby { - ct.sortdesc = !ct.sortdesc - } else { - ct.sortby = sortby - ct.sortdesc = desc - } + ct.sortby = sortby + ct.sortdesc = desc slice.Sort(ct.coins[:], func(i, j int) bool { if ct.sortdesc { i, j = j, i @@ -51,6 +47,10 @@ func (ct *Cointop) sort(sortby string, desc bool) { func (ct *Cointop) sortfn(sortby string, desc bool) func(g *gocui.Gui, v *gocui.View) error { return func(g *gocui.Gui, v *gocui.View) error { + if ct.sortby == sortby { + desc = !desc + } + ct.sort(sortby, desc) ct.g.Update(func(g *gocui.Gui) error { ct.tableview.Clear() diff --git a/cointop/table.go b/cointop/table.go index 691aaf6..2257162 100644 --- a/cointop/table.go +++ b/cointop/table.go @@ -7,13 +7,13 @@ import ( "time" humanize "github.com/dustin/go-humanize" + "github.com/jroimartin/gocui" apitypes "github.com/miguelmota/cointop/pkg/api/types" "github.com/miguelmota/cointop/pkg/color" "github.com/miguelmota/cointop/pkg/table" ) func (ct *Cointop) refreshTable() error { - ct.tableview.Clear() maxX, _ := ct.g.Size() ct.table = table.New().SetWidth(maxX) ct.table.AddCol("") @@ -77,7 +77,12 @@ func (ct *Cointop) refreshTable() error { ) } - ct.table.Format().Fprint(ct.tableview) + ct.g.Update(func(g *gocui.Gui) error { + ct.tableview.Clear() + ct.table.Format().Fprint(ct.tableview) + return nil + }) + return nil }