manage cursor based on current peer position

Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
Jakub Sokołowski 2019-06-27 15:34:34 -04:00 committed by Jakub
parent b307b82ac4
commit d793156f62
3 changed files with 30 additions and 35 deletions

31
keys.go
View File

@ -11,37 +11,12 @@ type Binding struct {
} }
func (vc *ViewController) CursorUp(g *gocui.Gui, v *gocui.View) error { func (vc *ViewController) CursorUp(g *gocui.Gui, v *gocui.View) error {
return MoveCursor(-1, vc, g, v) vc.State.SetCurrent(vc.State.GetState().Current - 1)
return nil
} }
func (vc *ViewController) CursorDown(g *gocui.Gui, v *gocui.View) error { func (vc *ViewController) CursorDown(g *gocui.Gui, v *gocui.View) error {
return MoveCursor(1, vc, g, v) vc.State.SetCurrent(vc.State.GetState().Current + 1)
}
func MoveCursor(mod int, vc *ViewController, g *gocui.Gui, v *gocui.View) error {
if v == nil {
return nil
}
cx, cy := v.Cursor()
// get peers
ps := vc.State.GetState()
peers := ps.Peers
// Don't go beyond available list of peers
if cy+mod >= len(peers) || cy+mod < 0 {
return nil
}
// update currently selected peer in the list
vc.State.SetCurrent(cy + mod)
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 return nil
} }

View File

@ -15,6 +15,9 @@ type Model struct {
} }
func (m *Model) Current(state PeersState, peerIndex int) PeersState { func (m *Model) Current(state PeersState, peerIndex int) PeersState {
if peerIndex < 0 || peerIndex >= len(state.Peers) {
return state
}
return PeersState{ return PeersState{
Peers: state.Peers, Peers: state.Peers,
Current: peerIndex, Current: peerIndex,

View File

@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"log"
"strings" "strings"
"github.com/jroimartin/gocui" "github.com/jroimartin/gocui"
@ -10,12 +11,13 @@ import (
func GenRenderFunc(g *gocui.Gui, state *State) func() { func GenRenderFunc(g *gocui.Gui, state *State) func() {
return func() { return func() {
ps := state.GetState() ps := state.GetState()
renderPeers(g, ps.Peers) renderPeerList(g, ps.Peers)
renderPeerInfo(g, state.GetCurrent()) renderPeerInfo(g, state.GetCurrent())
updatePeerCursor(g, ps.Current)
} }
} }
func renderPeers(g *gocui.Gui, peers []Peer) { func renderPeerList(g *gocui.Gui, peers []Peer) {
if len(peers) == 0 { if len(peers) == 0 {
return return
} }
@ -56,11 +58,18 @@ func renderPeerInfo(g *gocui.Gui, peer *Peer) {
}) })
} }
func boolToString(v bool, yes string, no string) string { func updatePeerCursor(g *gocui.Gui, current int) {
if v { v, err := g.View("main")
return yes if err != nil {
} else { log.Panicln("unable to find main view")
return no }
cx, _ := v.Cursor()
if err := v.SetCursor(cx, current); err != nil {
ox, _ := v.Origin()
if err := v.SetOrigin(ox, current); err != nil {
log.Panicln("unable to scroll")
}
} }
} }
@ -77,3 +86,11 @@ func (p Peer) AsTable(maxWidth int) string {
boolToString(p.Network.Trusted, "trusted", "normal"), boolToString(p.Network.Trusted, "trusted", "normal"),
boolToString(p.Network.Static, "static", "dynamic")) boolToString(p.Network.Static, "static", "dynamic"))
} }
func boolToString(v bool, yes string, no string) string {
if v {
return yes
} else {
return no
}
}