From 51d299888d395d94adc6ef1e85b4cd5876a06d1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Soko=C5=82owski?= Date: Thu, 27 Jun 2019 09:14:28 -0400 Subject: [PATCH] implement peer removal without prompt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jakub SokoĊ‚owski --- client.go | 9 +++++++++ keys.go | 6 ++++++ main.go | 4 +++- peers.go | 8 ++++++++ view.go | 2 ++ 5 files changed, 28 insertions(+), 1 deletion(-) diff --git a/client.go b/client.go index a57126f..60112d6 100644 --- a/client.go +++ b/client.go @@ -24,3 +24,12 @@ func (c *client) getPeers() ([]Peer, error) { } return peers, nil } + +func (c *client) removePeer(enode string) (bool, error) { + var rval bool + err := c.rpcClient.Call(&rval, "admin_removePeer", enode) + if err != nil { + return false, err + } + return rval, nil +} diff --git a/keys.go b/keys.go index 28cc71d..1a88823 100644 --- a/keys.go +++ b/keys.go @@ -44,3 +44,9 @@ func MoveCursor(mod int, vc *ViewController, g *gocui.Gui, v *gocui.View) error } return nil } + +func (vc *ViewController) HandleDelete(g *gocui.Gui, v *gocui.View) error { + ps := vc.State.(*PeersState) + selectedPeer := ps.selected + rval := ps.Remove(*selectedPeer) +} diff --git a/main.go b/main.go index 5472092..dd0893f 100644 --- a/main.go +++ b/main.go @@ -52,6 +52,8 @@ func main() { Binding{gocui.KeyArrowDown, gocui.ModNone, mainView.CursorDown}, Binding{'k', gocui.ModNone, mainView.CursorUp}, Binding{'j', gocui.ModNone, mainView.CursorDown}, + Binding{gocui.KeyDelete, gocui.ModNone, mainView.HandleDelete}, + Binding{'d', gocui.ModNone, mainView.HandleDelete}, } infoView := &ViewController{ Name: "info", @@ -60,7 +62,7 @@ func main() { Enabled: true, Wrap: true, // corner positions - TopLeft: func(mx, my int) (int, int) { return 0, my/2 + 1 }, + TopLeft: func(mx, my int) (int, int) { return 0, (my / 2) + 1 }, BotRight: func(mx, my int) (int, int) { return mx - 1, my - 1 }, } diff --git a/peers.go b/peers.go index 6995dd0..086f27c 100644 --- a/peers.go +++ b/peers.go @@ -64,6 +64,14 @@ func writePeers(g *gocui.Gui, peers []Peer) { }) } +func (p *PeersState) Remove(peer Peer) error { + success, err := p.c.removePeer(peer.Enode) + if err != nil || success != true { + log.Panicln(err) + } + return nil +} + func writePeerDetails(g *gocui.Gui, peer *Peer) { g.Update(func(g *gocui.Gui) error { v, err := g.View("info") diff --git a/view.go b/view.go index 6fc717a..d660fc9 100644 --- a/view.go +++ b/view.go @@ -42,6 +42,7 @@ func (m *ViewManager) Layout(g *gocui.Gui) error { x1, y1 := cfg.BotRight(mx, my) v, err := g.SetView(cfg.Name, x0, y0, x1, y1) + // Some settings can be set only once if err == gocui.ErrUnknownView { cfg.SetKeybindings(g) @@ -54,6 +55,7 @@ func (m *ViewManager) Layout(g *gocui.Gui) error { } else if err != nil { log.Panicln(err) } + v.Title = cfg.Title v.Wrap = cfg.Wrap v.SelFgColor = cfg.SelFgColor