2
0
mirror of https://github.com/miguelmota/cointop synced 2024-11-05 00:00:14 +00:00

quick col sort alt-arrow

This commit is contained in:
Miguel Mota 2018-04-05 13:11:09 -07:00
parent 3f311f0c39
commit cdd896a765
4 changed files with 86 additions and 13 deletions

View File

@ -20,6 +20,19 @@ func (ct *Cointop) setKeybinding(key interface{}, callback func(g *gocui.Gui, v
}
}
func (ct *Cointop) setKeybindingMod(key interface{}, mod gocui.Modifier, callback func(g *gocui.Gui, v *gocui.View) error) {
var err error
switch t := key.(type) {
case gocui.Key:
err = ct.g.SetKeybinding("", t, mod, callback)
case rune:
err = ct.g.SetKeybinding("", t, mod, callback)
}
if err != nil {
log.Fatal(err)
}
}
func (ct *Cointop) keybindings(g *gocui.Gui) error {
ct.setKeybinding(gocui.KeyArrowUp, ct.cursorUp)
ct.setKeybinding(gocui.KeyArrowDown, ct.cursorDown)
@ -38,6 +51,8 @@ func (ct *Cointop) keybindings(g *gocui.Gui) error {
ct.setKeybinding(gocui.KeyCtrlP, ct.prevPage)
ct.setKeybinding(gocui.KeyCtrlR, ct.refresh)
ct.setKeybinding(gocui.KeyCtrlU, ct.pageUp)
ct.setKeybindingMod(gocui.KeyArrowLeft, gocui.ModAlt, ct.sortPrevCol)
ct.setKeybindingMod(gocui.KeyArrowRight, gocui.ModAlt, ct.sortNextCol)
ct.setKeybinding('0', ct.firstPage)
ct.setKeybinding('1', ct.sortfn("1hchange", true))
ct.setKeybinding('2', ct.sortfn("24hchange", true))

View File

@ -4,6 +4,18 @@ import (
"github.com/jroimartin/gocui"
)
func (ct *Cointop) getCurrentPage() int {
return ct.page + 1
}
func (ct *Cointop) getTotalPages() int {
return (ct.getListCount() / ct.perpage) + 1
}
func (ct *Cointop) getListCount() int {
return len(ct.allcoins)
}
func (ct *Cointop) cursorDown(g *gocui.Gui, v *gocui.View) error {
if ct.tableview == nil {
return nil
@ -195,15 +207,3 @@ func (ct *Cointop) lastPage(g *gocui.Gui, v *gocui.View) error {
ct.rowChanged()
return nil
}
func (ct *Cointop) getCurrentPage() int {
return ct.page + 1
}
func (ct *Cointop) getTotalPages() int {
return (ct.getListCount() / ct.perpage) + 1
}
func (ct *Cointop) getListCount() int {
return len(ct.allcoins)
}

View File

@ -6,6 +6,20 @@ import (
apt "github.com/miguelmota/cointop/pkg/api/types"
)
var colorder = []string{
"rank",
"name",
"symbol",
"price",
"marketcap",
"24hvolume",
"1hchange",
"7dchange",
"totalsupply",
"availablesupply",
"lastupdated",
}
func (ct *Cointop) sort(sortby string, desc bool, list []*apt.Coin) {
ct.sortby = sortby
ct.sortdesc = desc
@ -63,3 +77,47 @@ func (ct *Cointop) sortfn(sortby string, desc bool) func(g *gocui.Gui, v *gocui.
return nil
}
}
func (ct *Cointop) getSortColIndex() int {
for i, col := range colorder {
if ct.sortby == col {
return i
}
}
return 0
}
func (ct *Cointop) sortPrevCol(g *gocui.Gui, v *gocui.View) error {
nextsortby := colorder[0]
i := ct.getSortColIndex()
k := i - 1
if k < 0 {
k = 0
}
nextsortby = colorder[k]
ct.sort(nextsortby, ct.sortdesc, ct.coins)
ct.Update(func() {
ct.tableview.Clear()
ct.updateTable()
})
ct.rowChanged()
return nil
}
func (ct *Cointop) sortNextCol(g *gocui.Gui, v *gocui.View) error {
nextsortby := colorder[0]
l := len(colorder)
i := ct.getSortColIndex()
k := i + 1
if k > l-1 {
k = l - 1
}
nextsortby = colorder[k]
ct.sort(nextsortby, ct.sortdesc, ct.coins)
ct.Update(func() {
ct.tableview.Clear()
ct.updateTable()
})
ct.rowChanged()
return nil
}

View File

@ -12,7 +12,7 @@ func (ct *Cointop) updateStatusbar(s string) {
ct.statusbarview.Clear()
currpage := ct.getCurrentPage()
totalpages := ct.getTotalPages()
fmt.Fprintln(ct.statusbarview, pad.Right(fmt.Sprintf("[q]uit [← →]page %v/%v %s", currpage, totalpages, s), maxX, " "))
fmt.Fprintln(ct.statusbarview, pad.Right(fmt.Sprintf("[q]uit [h]elp [← →]page %v/%v %s", currpage, totalpages, s), maxX, " "))
})
}