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
}
func HandlerCursorDispenser(mod int) func(g *gocui.Gui, v *gocui.View) error {
return func(g *gocui.Gui, v *gocui.View) error {
if v == 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
}
}
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
}
_, 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()
views := []*View{
&View{
Name: "main",
Title: "Peers",
Placeholder: "Loading peers...",
Cursor: true,
Highlight: true,
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
},
mainView := &View{
Name: "main",
Title: "Peers",
Placeholder: "Loading peers...",
Cursor: true,
Highlight: true,
Current: true,
SelFgColor: gocui.ColorBlack,
SelBgColor: gocui.ColorGreen,
TopLeft: func(mx, my int) (int, int) {
return 0, 0
},
&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
},
BotRight: func(mx, my int) (int, int) {
return mx - 1, my / 2
},
}
// 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)