extract key bindings to separate file

Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
Jakub Sokołowski 2019-06-21 10:29:23 -04:00 committed by Jakub
parent 5107d344dd
commit 33d12b5198
2 changed files with 51 additions and 8 deletions

45
keys.go Normal file
View File

@ -0,0 +1,45 @@
package main
import (
"fmt"
"github.com/jroimartin/gocui"
)
type Binding struct {
Key gocui.Key
Mod gocui.Modifier
Handler func(g *gocui.Gui, v *gocui.View) error
}
var bindings = [...]Binding{
Binding{gocui.KeyCtrlC, gocui.ModNone, quit},
Binding{gocui.KeyArrowUp, gocui.ModNone, HandlerCursorUp},
Binding{gocui.KeyArrowDown, gocui.ModNone, HandlerCursorDown},
}
func keybindings(g *gocui.Gui) error {
for _, b := range bindings {
if err := g.SetKeybinding("", b.Key, b.Mod, b.Handler); err != nil {
return err
}
}
return nil
}
func HandlerCursorUp(g *gocui.Gui, v *gocui.View) error {
fmt.Println("UP")
return nil
}
func HandlerCursorDown(g *gocui.Gui, v *gocui.View) error {
if v != nil {
cx, cy := v.Cursor()
if err := v.SetCursor(cx, cy+1); err != nil {
ox, oy := v.Origin()
if err := v.SetOrigin(ox, oy+1); err != nil {
return err
}
}
}
return nil
}

14
main.go
View File

@ -12,7 +12,7 @@ type rcpResp map[string]interface{}
const host = "127.0.0.1"
const port = 8545
const interval = 3
const interval = 5
var threadDone = make(chan struct{})
@ -23,6 +23,9 @@ func main() {
}
defer g.Close()
g.Highlight = true
g.Cursor = true
g.SetManagerFunc(layout)
if err := keybindings(g); err != nil {
@ -73,19 +76,14 @@ func writePeers(g *gocui.Gui, peers []Peer) {
})
}
func keybindings(g *gocui.Gui) error {
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
return err
}
return nil
}
func layout(g *gocui.Gui) error {
maxX, maxY := g.Size()
if v, err := g.SetView("main", 0, 0, maxX-1, maxY-1); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Highlight = true
v.SetCursor(0, 0)
fmt.Fprintln(v, "Loading peers...")
}
return nil