refactor to make arrow key handlers into methods of *View
Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
parent
ccad242463
commit
176330a41f
12
keys.go
12
keys.go
|
@ -12,8 +12,15 @@ type Binding struct {
|
|||
Handler func(g *gocui.Gui, v *gocui.View) error
|
||||
}
|
||||
|
||||
func HandlerCursorDispenser(mod int) func(g *gocui.Gui, v *gocui.View) error {
|
||||
return func(g *gocui.Gui, v *gocui.View) error {
|
||||
func (vc *View) CursorUp(g *gocui.Gui, v *gocui.View) error {
|
||||
return MoveCursor(-1, g, v)
|
||||
}
|
||||
|
||||
func (vc *View) CursorDown(g *gocui.Gui, v *gocui.View) error {
|
||||
return MoveCursor(1, g, v)
|
||||
}
|
||||
|
||||
func MoveCursor(mod int, g *gocui.Gui, v *gocui.View) error {
|
||||
if v == nil {
|
||||
return nil
|
||||
}
|
||||
|
@ -33,5 +40,4 @@ func HandlerCursorDispenser(mod int) func(g *gocui.Gui, v *gocui.View) error {
|
|||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
|
21
main.go
21
main.go
|
@ -28,8 +28,7 @@ func main() {
|
|||
}
|
||||
defer g.Close()
|
||||
|
||||
views := []*View{
|
||||
&View{
|
||||
mainView := &View{
|
||||
Name: "main",
|
||||
Title: "Peers",
|
||||
Placeholder: "Loading peers...",
|
||||
|
@ -38,19 +37,20 @@ func main() {
|
|||
Current: true,
|
||||
SelFgColor: gocui.ColorBlack,
|
||||
SelBgColor: gocui.ColorGreen,
|
||||
Keybindings: []Binding{
|
||||
Binding{gocui.KeyCtrlC, gocui.ModNone, quit},
|
||||
Binding{gocui.KeyArrowUp, gocui.ModNone, HandlerCursorDispenser(-1)},
|
||||
Binding{gocui.KeyArrowDown, gocui.ModNone, HandlerCursorDispenser(1)},
|
||||
},
|
||||
TopLeft: func(mx, my int) (int, int) {
|
||||
return 0, 0
|
||||
},
|
||||
BotRight: func(mx, my int) (int, int) {
|
||||
return mx - 1, my / 2
|
||||
},
|
||||
},
|
||||
&View{
|
||||
}
|
||||
// bindings defined separately so handlers can reference mainView
|
||||
mainView.Keybindings = []Binding{
|
||||
Binding{gocui.KeyCtrlC, gocui.ModNone, quit},
|
||||
Binding{gocui.KeyArrowUp, gocui.ModNone, mainView.CursorUp},
|
||||
Binding{gocui.KeyArrowDown, gocui.ModNone, mainView.CursorDown},
|
||||
}
|
||||
infoView := &View{
|
||||
Name: "info",
|
||||
Title: "Details",
|
||||
Placeholder: "Loading details...",
|
||||
|
@ -60,9 +60,10 @@ func main() {
|
|||
BotRight: func(mx, my int) (int, int) {
|
||||
return mx - 1, my - 1
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
views := []*View{mainView, infoView}
|
||||
|
||||
vm := NewViewManager(g, views)
|
||||
|
||||
g.SetManagerFunc(vm.Layout)
|
||||
|
|
Loading…
Reference in New Issue