diff --git a/json.go b/json.go index 427452e..aab451a 100644 --- a/json.go +++ b/json.go @@ -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 { diff --git a/keys.go b/keys.go index 05e6af0..d5262a4 100644 --- a/keys.go +++ b/keys.go @@ -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 } diff --git a/main.go b/main.go index afa1b1c..e297a39 100644 --- a/main.go +++ b/main.go @@ -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 diff --git a/peers.go b/peers.go new file mode 100644 index 0000000..cdfb93c --- /dev/null +++ b/peers.go @@ -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 + }) +}