attach PeersState under Data for ViewController

Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
Jakub Sokołowski 2019-06-26 19:54:04 -04:00 committed by Jakub
parent 176330a41f
commit 197679f10c
4 changed files with 42 additions and 24 deletions

16
keys.go
View File

@ -12,15 +12,21 @@ type Binding struct {
Handler func(g *gocui.Gui, v *gocui.View) error Handler func(g *gocui.Gui, v *gocui.View) error
} }
func (vc *View) CursorUp(g *gocui.Gui, v *gocui.View) error { func (vc *ViewController) CursorUp(g *gocui.Gui, v *gocui.View) error {
return MoveCursor(-1, g, v) return MoveCursor(-1, v)
} }
func (vc *View) CursorDown(g *gocui.Gui, v *gocui.View) error { func (vc *ViewController) CursorDown(g *gocui.Gui, v *gocui.View) error {
return MoveCursor(1, g, v) peers := vc.Data.(*PeersState).list
_, cy := v.Cursor()
// Don't go beyond available list of peers
if cy+1 >= len(peers) {
return nil
}
return MoveCursor(1, v)
} }
func MoveCursor(mod int, g *gocui.Gui, v *gocui.View) error { func MoveCursor(mod int, v *gocui.View) error {
if v == nil { if v == nil {
return nil return nil
} }

18
main.go
View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"fmt"
"github.com/jroimartin/gocui" "github.com/jroimartin/gocui"
"log" "log"
"os" "os"
@ -28,7 +27,9 @@ func main() {
} }
defer g.Close() defer g.Close()
mainView := &View{ peers := NewPeersState(host, port)
mainView := &ViewController{
Name: "main", Name: "main",
Title: "Peers", Title: "Peers",
Placeholder: "Loading peers...", Placeholder: "Loading peers...",
@ -37,6 +38,7 @@ func main() {
Current: true, Current: true,
SelFgColor: gocui.ColorBlack, SelFgColor: gocui.ColorBlack,
SelBgColor: gocui.ColorGreen, SelBgColor: gocui.ColorGreen,
Data: peers,
TopLeft: func(mx, my int) (int, int) { TopLeft: func(mx, my int) (int, int) {
return 0, 0 return 0, 0
}, },
@ -50,7 +52,7 @@ func main() {
Binding{gocui.KeyArrowUp, gocui.ModNone, mainView.CursorUp}, Binding{gocui.KeyArrowUp, gocui.ModNone, mainView.CursorUp},
Binding{gocui.KeyArrowDown, gocui.ModNone, mainView.CursorDown}, Binding{gocui.KeyArrowDown, gocui.ModNone, mainView.CursorDown},
} }
infoView := &View{ infoView := &ViewController{
Name: "info", Name: "info",
Title: "Details", Title: "Details",
Placeholder: "Loading details...", Placeholder: "Loading details...",
@ -62,20 +64,14 @@ func main() {
}, },
} }
views := []*View{mainView, infoView} views := []*ViewController{mainView, infoView}
vm := NewViewManager(g, views) vm := NewViewManager(g, views)
g.SetManagerFunc(vm.Layout) g.SetManagerFunc(vm.Layout)
url := fmt.Sprintf("http://%s:%d", host, port)
c, err := newClient(url)
if err != nil {
log.Panicln(err)
}
// Start RPC calling routine // Start RPC calling routine
go FetchPeers(c, g) go peers.Fetch(g)
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit { if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
log.Panicln(err) log.Panicln(err)

View File

@ -8,23 +8,38 @@ import (
"time" "time"
) )
func FetchPeers(c *client, g *gocui.Gui) { type PeersState struct {
c *client
list []Peer
}
func NewPeersState(host string, port int) *PeersState {
url := fmt.Sprintf("http://%s:%d", host, port)
c, err := newClient(url)
if err != nil {
log.Panicln(err)
}
return &PeersState{c: c}
}
func (p *PeersState) Fetch(g *gocui.Gui) {
for { for {
select { select {
case <-threadDone: case <-threadDone:
return return
default: default:
peers, err := c.getPeers() peers, err := p.c.getPeers()
if err != nil { if err != nil {
log.Panicln(err) log.Panicln(err)
} }
WritePeers(g, peers) p.list = peers
writePeers(g, peers)
} }
<-time.After(interval * time.Second) <-time.After(interval * time.Second)
} }
} }
func WritePeers(g *gocui.Gui, peers []Peer) { func writePeers(g *gocui.Gui, peers []Peer) {
g.Update(func(g *gocui.Gui) error { g.Update(func(g *gocui.Gui) error {
v, err := g.View("main") v, err := g.View("main")
if err != nil { if err != nil {

View File

@ -6,7 +6,7 @@ import (
"github.com/jroimartin/gocui" "github.com/jroimartin/gocui"
) )
type View struct { type ViewController struct {
Name string Name string
Title string Title string
Placeholder string Placeholder string
@ -19,14 +19,15 @@ type View struct {
SelFgColor gocui.Attribute SelFgColor gocui.Attribute
Keybindings []Binding Keybindings []Binding
Manager ViewManager Manager ViewManager
Data interface{}
} }
type ViewManager struct { type ViewManager struct {
g *gocui.Gui g *gocui.Gui
views []*View views []*ViewController
} }
func NewViewManager(g *gocui.Gui, views []*View) *ViewManager { func NewViewManager(g *gocui.Gui, views []*ViewController) *ViewManager {
vm := ViewManager{ vm := ViewManager{
g: g, g: g,
views: views, views: views,
@ -66,7 +67,7 @@ func (m *ViewManager) Layout(g *gocui.Gui) error {
return nil return nil
} }
func (v *View) SetKeybindings(g *gocui.Gui) error { func (v *ViewController) SetKeybindings(g *gocui.Gui) error {
for _, b := range v.Keybindings { for _, b := range v.Keybindings {
// IDEA: I can pass a method instead of a function here // IDEA: I can pass a method instead of a function here
if err := g.SetKeybinding("", b.Key, b.Mod, b.Handler); err != nil { if err := g.SetKeybinding("", b.Key, b.Mod, b.Handler); err != nil {