From 49c6c0a7601461e67e0adb21ebe8aa463af4a740 Mon Sep 17 00:00:00 2001 From: Miguel Mota Date: Tue, 3 Apr 2018 00:10:53 -0700 Subject: [PATCH] force refresh keyboard shortcut Former-commit-id: 17bb894640bb324e9017d5c2c1564a7f9e0018bc [formerly 17bb894640bb324e9017d5c2c1564a7f9e0018bc [formerly f443687078b13969ed122a514057689feba8bbce [formerly 9b7fa0dc581f50d60bdbfb66ddf8c81ae5da0f35]]] Former-commit-id: 82cbd609bf54dee2555c16724b38d7db38c478a7 Former-commit-id: 1023e7eda1117d981e213c2508c59348cd1ef88f [formerly aeb281ea33215d36554a299076ed2861b62ae1a2] Former-commit-id: 03042640e3e2913a4c3b360f974b31a2fa5b702b --- README.md | 1 + cointop/cointop.go | 2 ++ cointop/keybindings.go | 6 ++++++ cointop/layout.go | 43 ++++++++++++++++++++++++++++++++++++------ cointop/status.go | 7 +++++-- 5 files changed, 51 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 2c60a4e..4cd6850 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,7 @@ List of shortcuts: |``|alias to previous page| |``|visit highlighted coin on CoinMarketCap| |``|alias to `` +|``|force refresh| |`j`|alias to ``| |`k`|alias to ``| |`r`|sort by *[r]ank*| diff --git a/cointop/cointop.go b/cointop/cointop.go index bceabcb..920c5d9 100644 --- a/cointop/cointop.go +++ b/cointop/cointop.go @@ -41,6 +41,7 @@ type Cointop struct { perpage int refreshmux sync.Mutex refreshticker *time.Ticker + forcerefresh chan bool } // Run runs cointop @@ -61,6 +62,7 @@ func Run() { sortdesc: false, page: 0, perpage: 100, + forcerefresh: make(chan bool), } g.SetManagerFunc(ct.layout) if err := ct.keybindings(g); err != nil { diff --git a/cointop/keybindings.go b/cointop/keybindings.go index 7c82127..44207d6 100644 --- a/cointop/keybindings.go +++ b/cointop/keybindings.go @@ -43,6 +43,7 @@ func (ct *Cointop) keybindings(g *gocui.Gui) error { ct.setKeybinding('t', ct.sortfn("totalsupply", true)) ct.setKeybinding('a', ct.sortfn("availablesupply", true)) ct.setKeybinding('l', ct.sortfn("lastupdated", true)) + ct.setKeybinding(gocui.KeyCtrlR, ct.refresh) ct.setKeybinding(gocui.KeyEnter, ct.enter) ct.setKeybinding(gocui.KeySpace, ct.enter) ct.setKeybinding(gocui.KeyCtrlC, ct.quit) @@ -51,6 +52,11 @@ func (ct *Cointop) keybindings(g *gocui.Gui) error { return nil } +func (ct *Cointop) refresh(g *gocui.Gui, v *gocui.View) error { + ct.forcerefresh <- true + return nil +} + func (ct *Cointop) enter(g *gocui.Gui, v *gocui.View) error { exec.Command("open", ct.rowLink()).Output() return nil diff --git a/cointop/layout.go b/cointop/layout.go index 0bd673e..0ddcc73 100644 --- a/cointop/layout.go +++ b/cointop/layout.go @@ -2,6 +2,8 @@ package cointop import ( "math" + "strings" + "time" "github.com/jroimartin/gocui" apitypes "github.com/miguelmota/cointop/pkg/api/types" @@ -166,14 +168,43 @@ func (ct *Cointop) intervalFetchData() { go func() { for { select { + case <-ct.forcerefresh: + ct.refreshAll() case <-ct.refreshticker.C: - ct.refreshmux.Lock() - ct.updateCoins() - ct.updateTable() - ct.updateMarket() - ct.updateChart() - ct.refreshmux.Unlock() + ct.refreshAll() } } }() } + +func (ct *Cointop) refreshAll() error { + ct.refreshmux.Lock() + ct.setRefreshStatus() + ct.updateCoins() + ct.updateTable() + ct.updateMarket() + ct.updateChart() + ct.refreshmux.Unlock() + return nil +} + +func (ct *Cointop) setRefreshStatus() { + go func() { + ct.loadingTicks("refreshing", 900) + ct.updateStatus("") + ct.rowChanged() + }() +} + +func (ct *Cointop) loadingTicks(s string, t int) { + interval := 150 + k := 0 + for i := 0; i < (t / interval); i++ { + ct.updateStatus(s + strings.Repeat(".", k)) + time.Sleep(time.Duration(i*interval) * time.Millisecond) + k = k + 1 + if k > 3 { + k = 0 + } + } +} diff --git a/cointop/status.go b/cointop/status.go index 4a7da25..285c668 100644 --- a/cointop/status.go +++ b/cointop/status.go @@ -9,8 +9,11 @@ import ( func (ct *Cointop) updateStatus(s string) { maxX, _ := ct.g.Size() - ct.statusview.Clear() - fmt.Fprintln(ct.statusview, pad.Right(fmt.Sprintf("[q]uit [← →]page %s", s), maxX, " ")) + ct.g.Update(func(g *gocui.Gui) error { + ct.statusview.Clear() + fmt.Fprintln(ct.statusview, pad.Right(fmt.Sprintf("[q]uit [← →]page %s", s), maxX, " ")) + return nil + }) } func (ct *Cointop) refreshRowLink() {