From 176330a41faecaea0da144e99d3209411ee1d92c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Soko=C5=82owski?= Date: Wed, 26 Jun 2019 18:18:34 -0400 Subject: [PATCH] refactor to make arrow key handlers into methods of *View MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jakub SokoĊ‚owski --- keys.go | 46 +++++++++++++++++++++++------------------ main.go | 63 +++++++++++++++++++++++++++++---------------------------- 2 files changed, 58 insertions(+), 51 deletions(-) diff --git a/keys.go b/keys.go index 2127cc2..9ad2460 100644 --- a/keys.go +++ b/keys.go @@ -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 } diff --git a/main.go b/main.go index 280dedd..9599b14 100644 --- a/main.go +++ b/main.go @@ -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)