diff --git a/cointop/chart.go b/cointop/chart.go index 2365d1f..fd5c9b8 100644 --- a/cointop/chart.go +++ b/cointop/chart.go @@ -9,7 +9,7 @@ import ( ) func (ct *Cointop) updateChart() error { - maxX, _ := ct.g.Size() + maxX := ct.Width() if len(ct.chartpoints) == 0 { ct.chartPoints(maxX, "bitcoin") } diff --git a/cointop/cointop.go b/cointop/cointop.go index d5dbf3b..645702a 100644 --- a/cointop/cointop.go +++ b/cointop/cointop.go @@ -30,7 +30,7 @@ type Cointop struct { headersview *gocui.View tableview *gocui.View table *table.Table - statusview *gocui.View + statusbarview *gocui.View sortdesc bool sortby string api api.Interface diff --git a/cointop/layout.go b/cointop/layout.go index f6b7548..efc4870 100644 --- a/cointop/layout.go +++ b/cointop/layout.go @@ -12,7 +12,7 @@ import ( ) func (ct *Cointop) layout(g *gocui.Gui) error { - maxX, maxY := g.Size() + maxX, maxY := ct.Size() chartHeight := 10 topOffset := 0 @@ -84,15 +84,15 @@ func (ct *Cointop) layout(g *gocui.Gui) error { ct.rowChanged() } - if v, err := g.SetView("status", 0, maxY-2, maxX, maxY); err != nil { + if v, err := g.SetView("statusbar", 0, maxY-2, maxX, maxY); err != nil { if err != gocui.ErrUnknownView { return err } - ct.statusview = v - ct.statusview.Frame = false - ct.statusview.BgColor = gocui.ColorCyan - ct.statusview.FgColor = gocui.ColorBlack - ct.updateStatus("") + ct.statusbarview = v + ct.statusbarview.Frame = false + ct.statusbarview.BgColor = gocui.ColorCyan + ct.statusbarview.FgColor = gocui.ColorBlack + ct.updateStatusbar("") } ct.intervalFetchData() @@ -191,7 +191,7 @@ func (ct *Cointop) refreshAll() error { func (ct *Cointop) setRefreshStatus() { go func() { ct.loadingTicks("refreshing", 900) - ct.updateStatus("") + ct.updateStatusbar("") ct.rowChanged() }() } @@ -200,7 +200,7 @@ 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)) + ct.updateStatusbar(s + strings.Repeat(".", k)) time.Sleep(time.Duration(i*interval) * time.Millisecond) k = k + 1 if k > 3 { diff --git a/cointop/market.go b/cointop/market.go deleted file mode 100644 index d753bd0..0000000 --- a/cointop/market.go +++ /dev/null @@ -1,21 +0,0 @@ -package cointop - -import ( - "fmt" - - "github.com/miguelmota/cointop/pkg/color" - "github.com/miguelmota/cointop/pkg/humanize" - "github.com/miguelmota/cointop/pkg/pad" -) - -func (ct *Cointop) updateMarket() error { - maxX, _ := ct.g.Size() - market, err := ct.api.GetGlobalMarketData() - if err != nil { - return err - } - timeframe := "7 Day" - ct.marketview.Clear() - fmt.Fprintln(ct.marketview, pad.Right(fmt.Sprintf("%s Chart: %s • Total Market Cap: %s • 24H Volume: %s • BTC Dominance: %.2f%% • Active Currencies: %s • Active Markets: %s", color.Cyan("cointop"), timeframe, color.WhiteBold(humanize.Commaf(market.TotalMarketCapUSD)), humanize.Commaf(market.Total24HVolumeUSD), market.BitcoinPercentageOfMarketCap, humanize.Commaf(float64(market.ActiveCurrencies)), humanize.Commaf(float64(market.ActiveMarkets))), maxX, " ")) - return nil -} diff --git a/cointop/marketbar.go b/cointop/marketbar.go new file mode 100644 index 0000000..5a4ddff --- /dev/null +++ b/cointop/marketbar.go @@ -0,0 +1,23 @@ +package cointop + +import ( + "fmt" + + "github.com/miguelmota/cointop/pkg/color" + "github.com/miguelmota/cointop/pkg/humanize" + "github.com/miguelmota/cointop/pkg/pad" +) + +func (ct *Cointop) updateMarket() error { + maxX := ct.Width() + market, err := ct.api.GetGlobalMarketData() + if err != nil { + return err + } + timeframe := "7 Day" + ct.Update(func() { + ct.marketview.Clear() + fmt.Fprintln(ct.marketview, pad.Right(fmt.Sprintf("%s Chart: %s • Total Market Cap: %s • 24H Volume: %s • BTC Dominance: %.2f%% • Active Currencies: %s • Active Markets: %s", color.Cyan("cointop"), timeframe, color.WhiteBold(humanize.Commaf(market.TotalMarketCapUSD)), humanize.Commaf(market.Total24HVolumeUSD), market.BitcoinPercentageOfMarketCap, humanize.Commaf(float64(market.ActiveCurrencies)), humanize.Commaf(float64(market.ActiveMarkets))), maxX, " ")) + }) + return nil +} diff --git a/cointop/size.go b/cointop/size.go new file mode 100644 index 0000000..e571ff3 --- /dev/null +++ b/cointop/size.go @@ -0,0 +1,18 @@ +package cointop + +// Size returns window width and height +func (ct *Cointop) Size() (int, int) { + return ct.g.Size() +} + +// Width returns window width +func (ct *Cointop) Width() int { + w, _ := ct.Size() + return w +} + +// Height returns window height +func (ct *Cointop) Height() int { + _, h := ct.Size() + return h +} diff --git a/cointop/sort.go b/cointop/sort.go index 62f74d7..ddfdde4 100644 --- a/cointop/sort.go +++ b/cointop/sort.go @@ -53,23 +53,10 @@ func (ct *Cointop) sortfn(sortby string, desc bool) func(g *gocui.Gui, v *gocui. } ct.sort(sortby, desc, ct.coins) - ct.g.Update(func(g *gocui.Gui) error { + ct.Update(func() { ct.tableview.Clear() ct.updateTable() - return nil }) - /* - g.Update(func(g *gocui.Gui) error { - ct.chartview.Clear() - maxX, _ := g.Size() - _, cy := ct.chartview.Cursor() - coin := "ethereum" - ct.chartPoints(maxX, coin) - ct.updateChart() - fmt.Fprint(ct.chartview, cy) - return nil - }) - */ ct.rowChanged() return nil diff --git a/cointop/status.go b/cointop/status.go deleted file mode 100644 index 285c668..0000000 --- a/cointop/status.go +++ /dev/null @@ -1,25 +0,0 @@ -package cointop - -import ( - "fmt" - - "github.com/jroimartin/gocui" - "github.com/miguelmota/cointop/pkg/pad" -) - -func (ct *Cointop) updateStatus(s string) { - maxX, _ := ct.g.Size() - 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() { - url := ct.rowLink() - ct.g.Update(func(g *gocui.Gui) error { - ct.updateStatus(fmt.Sprintf("[↵]%s", url)) - return nil - }) -} diff --git a/cointop/statusbar.go b/cointop/statusbar.go new file mode 100644 index 0000000..e43c200 --- /dev/null +++ b/cointop/statusbar.go @@ -0,0 +1,22 @@ +package cointop + +import ( + "fmt" + + "github.com/miguelmota/cointop/pkg/pad" +) + +func (ct *Cointop) updateStatusbar(s string) { + maxX := ct.Width() + ct.Update(func() { + ct.statusbarview.Clear() + fmt.Fprintln(ct.statusbarview, pad.Right(fmt.Sprintf("[q]uit [← →]page %s", s), maxX, " ")) + }) +} + +func (ct *Cointop) refreshRowLink() { + url := ct.rowLink() + ct.Update(func() { + ct.updateStatusbar(fmt.Sprintf("[↵]%s", url)) + }) +} diff --git a/cointop/table.go b/cointop/table.go index de817c2..0847981 100644 --- a/cointop/table.go +++ b/cointop/table.go @@ -6,7 +6,6 @@ import ( "strings" "time" - "github.com/jroimartin/gocui" apt "github.com/miguelmota/cointop/pkg/api/types" "github.com/miguelmota/cointop/pkg/color" "github.com/miguelmota/cointop/pkg/humanize" @@ -14,7 +13,7 @@ import ( ) func (ct *Cointop) refreshTable() error { - maxX, _ := ct.g.Size() + maxX := ct.Width() ct.table = table.New().SetWidth(maxX) ct.table.AddCol("") ct.table.AddCol("") @@ -77,10 +76,9 @@ func (ct *Cointop) refreshTable() error { ) } - ct.g.Update(func(g *gocui.Gui) error { + ct.Update(func() { ct.tableview.Clear() ct.table.Format().Fprint(ct.tableview) - return nil }) return nil diff --git a/cointop/update.go b/cointop/update.go new file mode 100644 index 0000000..94a1c72 --- /dev/null +++ b/cointop/update.go @@ -0,0 +1,11 @@ +package cointop + +import "github.com/jroimartin/gocui" + +// Update update view +func (ct *Cointop) Update(f func()) { + ct.g.Update(func(g *gocui.Gui) error { + f() + return nil + }) +}