2019-06-21 14:29:23 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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},
|
2019-06-21 18:53:50 +00:00
|
|
|
Binding{gocui.KeyArrowUp, gocui.ModNone, HandlerCursorDispenser(-1)},
|
|
|
|
Binding{gocui.KeyArrowDown, gocui.ModNone, HandlerCursorDispenser(1)},
|
2019-06-21 14:29:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-06-21 18:53:50 +00:00
|
|
|
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
|
|
|
|
}
|
2019-06-26 18:59:33 +00:00
|
|
|
_, my := v.Size()
|
2019-06-21 14:29:23 +00:00
|
|
|
cx, cy := v.Cursor()
|
2019-06-26 18:59:33 +00:00
|
|
|
log.Printf("my: %d, cx: %d, cy: %d", my, cx, cy)
|
|
|
|
if cy+mod < 0 || cy+mod == my {
|
2019-06-21 18:53:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err := v.SetCursor(cx, cy+mod); err != nil {
|
|
|
|
if mod == -1 {
|
|
|
|
return nil
|
|
|
|
}
|
2019-06-21 14:29:23 +00:00
|
|
|
ox, oy := v.Origin()
|
2019-06-21 18:53:50 +00:00
|
|
|
if err := v.SetOrigin(ox, oy+mod); err != nil {
|
2019-06-21 14:29:23 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-06-21 18:53:50 +00:00
|
|
|
return nil
|
2019-06-21 14:29:23 +00:00
|
|
|
}
|
|
|
|
}
|