diff --git a/Makefile b/Makefile index e69de29..aad8cd8 100644 --- a/Makefile +++ b/Makefile @@ -0,0 +1,5 @@ +all: + @echo "no default" + +run: + go run cointop.go keybindings.go diff --git a/cointop.go b/cointop.go index bbcd822..94cf3c5 100644 --- a/cointop.go +++ b/cointop.go @@ -58,7 +58,6 @@ func (ct *Cointop) layout(g *gocui.Gui) error { return err } t := table.New().SetWidth(maxX) - headers := []string{ pad.Right("[r]ank", 13, " "), pad.Right("[n]ame", 13, " "), @@ -100,73 +99,6 @@ func (ct *Cointop) layout(g *gocui.Gui) error { return nil } -func (ct *Cointop) keybindings(g *gocui.Gui) error { - if err := g.SetKeybinding("", gocui.KeyArrowDown, gocui.ModNone, ct.cursorDown); err != nil { - return err - } - if err := g.SetKeybinding("", 'j', gocui.ModNone, ct.cursorDown); err != nil { - return err - } - if err := g.SetKeybinding("", gocui.KeyArrowUp, gocui.ModNone, ct.cursorUp); err != nil { - return err - } - if err := g.SetKeybinding("", 'k', gocui.ModNone, ct.cursorUp); err != nil { - return err - } - if err := g.SetKeybinding("", gocui.KeyCtrlD, gocui.ModNone, ct.pageDown); err != nil { - return err - } - if err := g.SetKeybinding("", gocui.KeyCtrlU, gocui.ModNone, ct.pageUp); err != nil { - return err - } - if err := g.SetKeybinding("", 'r', gocui.ModNone, ct.sort("rank", false)); err != nil { - return err - } - if err := g.SetKeybinding("", 'n', gocui.ModNone, ct.sort("name", true)); err != nil { - return err - } - if err := g.SetKeybinding("", 's', gocui.ModNone, ct.sort("symbol", false)); err != nil { - return err - } - if err := g.SetKeybinding("", 'p', gocui.ModNone, ct.sort("price", true)); err != nil { - return err - } - if err := g.SetKeybinding("", 'm', gocui.ModNone, ct.sort("marketcap", true)); err != nil { - return err - } - if err := g.SetKeybinding("", 'v', gocui.ModNone, ct.sort("24hvolume", true)); err != nil { - return err - } - if err := g.SetKeybinding("", '1', gocui.ModNone, ct.sort("1hchange", true)); err != nil { - return err - } - if err := g.SetKeybinding("", '2', gocui.ModNone, ct.sort("24hchange", true)); err != nil { - return err - } - if err := g.SetKeybinding("", '7', gocui.ModNone, ct.sort("7dchange", true)); err != nil { - return err - } - if err := g.SetKeybinding("", 't', gocui.ModNone, ct.sort("totalsupply", true)); err != nil { - return err - } - if err := g.SetKeybinding("", 'a', gocui.ModNone, ct.sort("availablesupply", true)); err != nil { - return err - } - if err := g.SetKeybinding("", 'l', gocui.ModNone, ct.sort("lastupdated", true)); err != nil { - return err - } - if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, ct.quit); err != nil { - return err - } - if err := g.SetKeybinding("", 'q', gocui.ModNone, ct.quit); err != nil { - return err - } - if err := g.SetKeybinding("", gocui.KeyEsc, gocui.ModNone, ct.quit); err != nil { - return err - } - return nil -} - 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 { diff --git a/keybindings.go b/keybindings.go new file mode 100644 index 0000000..52311b0 --- /dev/null +++ b/keybindings.go @@ -0,0 +1,38 @@ +package main + +import ( + "log" + + "github.com/jroimartin/gocui" +) + +func (ct *Cointop) setKeybinding(key gocui.Key, callback func(g *gocui.Gui, v *gocui.View) error) { + if err := ct.g.SetKeybinding("", key, gocui.ModNone, callback); err != nil { + log.Fatal(err) + } +} + +func (ct *Cointop) keybindings(g *gocui.Gui) error { + ct.setKeybinding(gocui.KeyArrowDown, ct.cursorDown) + ct.setKeybinding('j', ct.cursorDown) + ct.setKeybinding(gocui.KeyArrowUp, ct.cursorUp) + 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(gocui.KeyCtrlC, ct.quit) + ct.setKeybinding('q', ct.quit) + ct.setKeybinding(gocui.KeyEsc, ct.quit) + return nil +}