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
}
func (vc *View) CursorUp(g *gocui.Gui, v *gocui.View) error {
return MoveCursor(-1, g, v)
func (vc *ViewController) CursorUp(g *gocui.Gui, v *gocui.View) error {
return MoveCursor(-1, v)
}
func (vc *View) CursorDown(g *gocui.Gui, v *gocui.View) error {
return MoveCursor(1, g, v)
func (vc *ViewController) CursorDown(g *gocui.Gui, v *gocui.View) error {
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 {
return nil
}

18
main.go
View File

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

View File

@ -8,23 +8,38 @@ import (
"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 {
select {
case <-threadDone:
return
default:
peers, err := c.getPeers()
peers, err := p.c.getPeers()
if err != nil {
log.Panicln(err)
}
WritePeers(g, peers)
p.list = peers
writePeers(g, peers)
}
<-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 {
v, err := g.View("main")
if err != nil {

View File

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