implement moving of cursor with arrow keys

Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
Jakub Sokołowski 2019-06-21 14:53:50 -04:00 committed by Jakub
parent 33d12b5198
commit 62c32b7a3d
4 changed files with 66 additions and 51 deletions

View File

@ -32,9 +32,7 @@ func (p Peer) AsTable(maxWidth int) string {
// the ID is too long to display in full in most places
func (id peerId) String() string {
return fmt.Sprintf("%s...%s",
string(id[:6]),
string(id[len(id)-6:]))
return fmt.Sprintf("%s...%s", string(id[:6]), string(id[len(id)-6:]))
}
type NetworkInfo struct {

30
keys.go
View File

@ -1,7 +1,6 @@
package main
import (
"fmt"
"github.com/jroimartin/gocui"
)
@ -13,8 +12,8 @@ type Binding struct {
var bindings = [...]Binding{
Binding{gocui.KeyCtrlC, gocui.ModNone, quit},
Binding{gocui.KeyArrowUp, gocui.ModNone, HandlerCursorUp},
Binding{gocui.KeyArrowDown, gocui.ModNone, HandlerCursorDown},
Binding{gocui.KeyArrowUp, gocui.ModNone, HandlerCursorDispenser(-1)},
Binding{gocui.KeyArrowDown, gocui.ModNone, HandlerCursorDispenser(1)},
}
func keybindings(g *gocui.Gui) error {
@ -26,20 +25,25 @@ func keybindings(g *gocui.Gui) error {
return nil
}
func HandlerCursorUp(g *gocui.Gui, v *gocui.View) error {
fmt.Println("UP")
return nil
}
func HandlerCursorDown(g *gocui.Gui, v *gocui.View) error {
if v != nil {
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
}
mx, _ := v.Size()
cx, cy := v.Cursor()
if err := v.SetCursor(cx, cy+1); err != nil {
if cy == 0 || cx == mx {
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+1); err != nil {
if err := v.SetOrigin(ox, oy+mod); err != nil {
return err
}
}
return nil
}
return nil
}

43
main.go
View File

@ -2,10 +2,8 @@ package main
import (
"fmt"
"log"
"time"
//"github.com/kr/pretty"
"github.com/jroimartin/gocui"
"log"
)
type rcpResp map[string]interface{}
@ -23,6 +21,7 @@ func main() {
}
defer g.Close()
g.SelFgColor = gocui.ColorGreen
g.Highlight = true
g.Cursor = true
@ -39,51 +38,25 @@ func main() {
}
// Start RPC calling routine
go fetchPeers(c, g)
go FetchPeers(c, g)
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
log.Panicln(err)
}
}
func fetchPeers(c *client, g *gocui.Gui) {
for {
select {
case <-threadDone:
return
case <-time.After(interval * time.Second):
peers, err := c.getPeers()
if err != nil {
log.Panicln(err)
}
writePeers(g, peers)
}
}
}
func writePeers(g *gocui.Gui, peers []Peer) {
g.Update(func(g *gocui.Gui) error {
v, err := g.View("main")
if err != nil {
return err
}
v.Clear()
maxWidth, _ := g.Size()
for _, peer := range peers {
fmt.Fprintf(v, "%s\n", peer.AsTable(maxWidth))
}
return nil
})
}
func layout(g *gocui.Gui) error {
maxX, maxY := g.Size()
if v, err := g.SetView("main", 0, 0, maxX-1, maxY-1); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.SelFgColor = gocui.ColorBlack
v.SelBgColor = gocui.ColorGreen
v.Title = "Peers"
v.Highlight = true
v.SetCursor(0, 0)
v.SetCursor(0, 1)
g.SetCurrentView("main")
fmt.Fprintln(v, "Loading peers...")
}
return nil

40
peers.go Normal file
View File

@ -0,0 +1,40 @@
package main
import (
"fmt"
"github.com/jroimartin/gocui"
"log"
"time"
)
func FetchPeers(c *client, g *gocui.Gui) {
for {
select {
case <-threadDone:
return
default:
peers, err := c.getPeers()
if err != nil {
log.Panicln(err)
}
WritePeers(g, peers)
}
<-time.After(interval * time.Second)
}
}
func WritePeers(g *gocui.Gui, peers []Peer) {
g.Update(func(g *gocui.Gui) error {
v, err := g.View("main")
if err != nil {
return err
}
v.Clear()
maxWidth, _ := g.Size()
fmt.Fprintf(v, "%-15s | %-40s | %s\n", "Peer ID", "Name", "Protocols")
for _, peer := range peers {
fmt.Fprintf(v, "%s\n", peer.AsTable(maxWidth))
}
return nil
})
}