refactor to make arrow key handlers into methods of *View

Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
Jakub Sokołowski 2019-06-26 18:18:34 -04:00 committed by Jakub
parent ccad242463
commit 176330a41f
2 changed files with 58 additions and 51 deletions

46
keys.go
View File

@ -12,26 +12,32 @@ type Binding struct {
Handler func(g *gocui.Gui, v *gocui.View) error Handler func(g *gocui.Gui, v *gocui.View) error
} }
func HandlerCursorDispenser(mod int) func(g *gocui.Gui, v *gocui.View) error { func (vc *View) CursorUp(g *gocui.Gui, v *gocui.View) error {
return func(g *gocui.Gui, v *gocui.View) error { return MoveCursor(-1, g, v)
if v == nil { }
return nil
} func (vc *View) CursorDown(g *gocui.Gui, v *gocui.View) error {
_, my := v.Size() return MoveCursor(1, g, v)
cx, cy := v.Cursor() }
log.Printf("my: %d, cx: %d, cy: %d", my, cx, cy)
if cy+mod < 0 || cy+mod == my { func MoveCursor(mod int, g *gocui.Gui, v *gocui.View) error {
return nil if v == nil {
}
if err := v.SetCursor(cx, cy+mod); err != nil {
if mod == -1 {
return nil
}
ox, oy := v.Origin()
if err := v.SetOrigin(ox, oy+mod); err != nil {
return err
}
}
return nil return nil
} }
_, my := v.Size()
cx, cy := v.Cursor()
log.Printf("my: %d, cx: %d, cy: %d", my, cx, cy)
if cy+mod < 0 || cy+mod == my {
return nil
}
if err := v.SetCursor(cx, cy+mod); err != nil {
if mod == -1 {
return nil
}
ox, oy := v.Origin()
if err := v.SetOrigin(ox, oy+mod); err != nil {
return err
}
}
return nil
} }

63
main.go
View File

@ -28,40 +28,41 @@ func main() {
} }
defer g.Close() defer g.Close()
views := []*View{ mainView := &View{
&View{ Name: "main",
Name: "main", Title: "Peers",
Title: "Peers", Placeholder: "Loading peers...",
Placeholder: "Loading peers...", Cursor: true,
Cursor: true, Highlight: true,
Highlight: true, Current: true,
Current: true, SelFgColor: gocui.ColorBlack,
SelFgColor: gocui.ColorBlack, SelBgColor: gocui.ColorGreen,
SelBgColor: gocui.ColorGreen, TopLeft: func(mx, my int) (int, int) {
Keybindings: []Binding{ return 0, 0
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{ BotRight: func(mx, my int) (int, int) {
Name: "info", return mx - 1, my / 2
Title: "Details",
Placeholder: "Loading details...",
TopLeft: func(mx, my int) (int, int) {
return 0, my/2 + 1
},
BotRight: func(mx, my int) (int, int) {
return mx - 1, my - 1
},
}, },
} }
// 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...",
TopLeft: func(mx, my int) (int, int) {
return 0, my/2 + 1
},
BotRight: func(mx, my int) (int, int) {
return mx - 1, my - 1
},
}
views := []*View{mainView, infoView}
vm := NewViewManager(g, views) vm := NewViewManager(g, views)