diff --git a/cointop/cointop.go b/cointop/cointop.go index 1811c62..16f0bba 100644 --- a/cointop/cointop.go +++ b/cointop/cointop.go @@ -32,7 +32,7 @@ type Cointop struct { table *table.Table statusview *gocui.View sortdesc bool - currentsort string + sortby string api api.Interface coins []*apitypes.Coin } @@ -42,9 +42,8 @@ func (ct *Cointop) rowChanged() { } func (ct *Cointop) fetchData() ([]*apitypes.Coin, error) { - limit := 100 result := []*apitypes.Coin{} - coins, err := ct.api.GetAllCoinData(int(limit)) + coins, err := ct.api.GetAllCoinData() if err != nil { return result, err } @@ -75,8 +74,10 @@ func Run() { g.Mouse = true g.Highlight = true ct := Cointop{ - g: g, - api: api.NewCMC(), + g: g, + api: api.NewCMC(), + sortdesc: true, + sortby: "rank", } g.SetManagerFunc(ct.layout) if err := ct.keybindings(g); err != nil { diff --git a/cointop/keybindings.go b/cointop/keybindings.go index f444515..7aac75d 100644 --- a/cointop/keybindings.go +++ b/cointop/keybindings.go @@ -27,18 +27,18 @@ func (ct *Cointop) keybindings(g *gocui.Gui) error { ct.setKeybinding('k', ct.cursorUp) ct.setKeybinding(gocui.KeyCtrlD, ct.pageDown) ct.setKeybinding(gocui.KeyCtrlU, ct.pageUp) - ct.setKeybinding('r', ct.sort("rank", false)) - ct.setKeybinding('n', ct.sort("name", true)) - ct.setKeybinding('s', ct.sort("symbol", false)) - ct.setKeybinding('p', ct.sort("price", true)) - ct.setKeybinding('m', ct.sort("marketcap", true)) - ct.setKeybinding('v', ct.sort("24hvolume", true)) - ct.setKeybinding('1', ct.sort("1hchange", true)) - ct.setKeybinding('2', ct.sort("24hchange", true)) - ct.setKeybinding('7', ct.sort("7dchange", true)) - ct.setKeybinding('t', ct.sort("totalsupply", true)) - ct.setKeybinding('a', ct.sort("availablesupply", true)) - ct.setKeybinding('l', ct.sort("lastupdated", true)) + ct.setKeybinding('r', ct.sortfn("rank", false)) + ct.setKeybinding('n', ct.sortfn("name", true)) + ct.setKeybinding('s', ct.sortfn("symbol", false)) + ct.setKeybinding('p', ct.sortfn("price", true)) + ct.setKeybinding('m', ct.sortfn("marketcap", true)) + ct.setKeybinding('v', ct.sortfn("24hvolume", true)) + ct.setKeybinding('1', ct.sortfn("1hchange", true)) + ct.setKeybinding('2', ct.sortfn("24hchange", true)) + ct.setKeybinding('7', ct.sortfn("7dchange", true)) + ct.setKeybinding('t', ct.sortfn("totalsupply", true)) + ct.setKeybinding('a', ct.sortfn("availablesupply", true)) + ct.setKeybinding('l', ct.sortfn("lastupdated", true)) ct.setKeybinding(gocui.KeyEnter, ct.enter) ct.setKeybinding(gocui.KeySpace, ct.enter) ct.setKeybinding(gocui.KeyCtrlC, ct.quit) diff --git a/cointop/layout.go b/cointop/layout.go index 4ecdfbe..f882291 100644 --- a/cointop/layout.go +++ b/cointop/layout.go @@ -42,15 +42,15 @@ func (ct *Cointop) layout(g *gocui.Gui) error { pad.Right("[r]ank", 13, " "), pad.Right("[n]ame", 13, " "), pad.Right("[s]ymbol", 8, " "), - pad.Left("[p]rice", 10, " "), + pad.Left("[p]rice", 13, " "), pad.Left("[m]arket cap", 17, " "), pad.Left("24H [v]olume", 15, " "), pad.Left("[1]H%", 9, " "), pad.Left("[2]4H%", 9, " "), pad.Left("[7]D%", 9, " "), pad.Left("[t]otal supply", 20, " "), - pad.Left("[a]vailable supply", 19, " "), - pad.Left("[l]ast updated", 17, " "), + pad.Left("[a]vailable supply", 18, " "), + pad.Left("[l]ast updated", 18, " "), } for _, h := range headers { t.AddCol(h) @@ -74,6 +74,14 @@ func (ct *Cointop) layout(g *gocui.Gui) error { ct.tableview.Highlight = true ct.tableview.SelBgColor = gocui.ColorCyan ct.tableview.SelFgColor = gocui.ColorBlack + var err error + if len(ct.coins) == 0 { + ct.coins, err = ct.fetchData() + if err != nil { + return err + } + } + ct.sort(ct.sortby, ct.sortdesc) ct.updateTable() ct.rowChanged() } diff --git a/cointop/sort.go b/cointop/sort.go index 62df281..36d7d47 100644 --- a/cointop/sort.go +++ b/cointop/sort.go @@ -5,50 +5,54 @@ import ( "github.com/jroimartin/gocui" ) -func (ct *Cointop) sort(sortby string, desc bool) func(g *gocui.Gui, v *gocui.View) error { - return func(g *gocui.Gui, v *gocui.View) error { - if ct.currentsort == sortby { - ct.sortdesc = !ct.sortdesc - } else { - ct.currentsort = sortby - ct.sortdesc = desc +func (ct *Cointop) sort(sortby string, desc bool) { + if ct.sortby == sortby { + ct.sortdesc = !ct.sortdesc + } else { + ct.sortby = sortby + ct.sortdesc = desc + } + slice.Sort(ct.coins[:], func(i, j int) bool { + if ct.sortdesc { + i, j = j, i } - slice.Sort(ct.coins[:], func(i, j int) bool { - if ct.sortdesc { - i, j = j, i - } - a := ct.coins[i] - b := ct.coins[j] - switch sortby { - case "rank": - return a.Rank < b.Rank - case "name": - return a.Name < b.Name - case "symbol": - return a.Symbol < b.Symbol - case "price": - return a.PriceUSD < b.PriceUSD - case "marketcap": - return a.MarketCapUSD < b.MarketCapUSD - case "24hvolume": - return a.USD24HVolume < b.USD24HVolume - case "1hchange": - return a.PercentChange1H < b.PercentChange1H - case "24hchange": - return a.PercentChange24H < b.PercentChange24H - case "7dchange": - return a.PercentChange7D < b.PercentChange7D - case "totalsupply": - return a.TotalSupply < b.TotalSupply - case "availablesupply": - return a.AvailableSupply < b.AvailableSupply - case "lastupdated": - return a.LastUpdated < b.LastUpdated - default: - return a.Rank < b.Rank - } - }) - g.Update(func(g *gocui.Gui) error { + a := ct.coins[i] + b := ct.coins[j] + switch sortby { + case "rank": + return a.Rank < b.Rank + case "name": + return a.Name < b.Name + case "symbol": + return a.Symbol < b.Symbol + case "price": + return a.PriceUSD < b.PriceUSD + case "marketcap": + return a.MarketCapUSD < b.MarketCapUSD + case "24hvolume": + return a.USD24HVolume < b.USD24HVolume + case "1hchange": + return a.PercentChange1H < b.PercentChange1H + case "24hchange": + return a.PercentChange24H < b.PercentChange24H + case "7dchange": + return a.PercentChange7D < b.PercentChange7D + case "totalsupply": + return a.TotalSupply < b.TotalSupply + case "availablesupply": + return a.AvailableSupply < b.AvailableSupply + case "lastupdated": + return a.LastUpdated < b.LastUpdated + default: + return a.Rank < b.Rank + } + }) +} + +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 { + ct.sort(sortby, desc) + ct.g.Update(func(g *gocui.Gui) error { ct.tableview.Clear() ct.updateTable() return nil diff --git a/cointop/table.go b/cointop/table.go index c125747..58c0a83 100644 --- a/cointop/table.go +++ b/cointop/table.go @@ -29,13 +29,6 @@ func (ct *Cointop) updateTable() error { ct.table.AddCol("") ct.table.AddCol("") ct.table.HideColumHeaders = true - var err error - if len(ct.coins) == 0 { - ct.coins, err = ct.fetchData() - if err != nil { - return err - } - } for _, coin := range ct.coins { unix, _ := strconv.ParseInt(coin.LastUpdated, 10, 64) lastUpdated := time.Unix(unix, 0).Format("15:04:05 Jan 02") @@ -61,9 +54,15 @@ func (ct *Cointop) updateTable() error { if coin.PercentChange7D < 0 { color7d = color.Red } + name := coin.Name + lastchar := len(name) + if lastchar > 20 { + lastchar = 20 + name = fmt.Sprintf("%s...", name[0:17]) + } ct.table.AddRow( pad.Left(fmt.Sprint(coin.Rank), 4, " "), - pad.Right(coin.Name, 22, " "), + pad.Right(" "+string(name), 22, " "), pad.Right(coin.Symbol, 6, " "), colorprice(pad.Left(humanize.Commaf(coin.PriceUSD), 12, " ")), pad.Left(humanize.Commaf(coin.MarketCapUSD), 17, " "), diff --git a/pkg/api/cmc/cmc.go b/pkg/api/cmc/cmc.go index a5c7cab..4571bdb 100644 --- a/pkg/api/cmc/cmc.go +++ b/pkg/api/cmc/cmc.go @@ -15,9 +15,9 @@ func New() *Service { } // GetAllCoinData gets all coin data -func (s *Service) GetAllCoinData(limit int) (map[string]types.Coin, error) { +func (s *Service) GetAllCoinData() (map[string]types.Coin, error) { ret := make(map[string]types.Coin) - coins, err := cmc.GetAllCoinData(int(limit)) + coins, err := cmc.GetAllCoinData(0) if err != nil { return ret, err } diff --git a/pkg/api/interface.go b/pkg/api/interface.go index 7e59931..4ee9b69 100644 --- a/pkg/api/interface.go +++ b/pkg/api/interface.go @@ -6,7 +6,7 @@ import ( // Interface interface type Interface interface { - GetAllCoinData(limit int) (map[string]types.Coin, error) + GetAllCoinData() (map[string]types.Coin, error) GetCoinGraphData(coin string, start int64, end int64) (types.CoinGraph, error) GetGlobalMarketGraphData(start int64, end int64) (types.MarketGraph, error) //GetCoinData(coin string) (types.Coin, error)