extract key bindings to separate file
Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
parent
5107d344dd
commit
33d12b5198
|
@ -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
14
main.go
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue