From a4252ffdfbf627d5fac086f30f13b6fd1c08fcc9 Mon Sep 17 00:00:00 2001 From: Edouard Paris Date: Mon, 29 Apr 2019 09:44:55 +0200 Subject: [PATCH] fix view channels: wrong current channel issue #11 --- ui/controller.go | 4 ++-- ui/models/channels.go | 4 ++++ ui/views/channels.go | 12 ++++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/ui/controller.go b/ui/controller.go index e0cf8a5..ee6776d 100644 --- a/ui/controller.go +++ b/ui/controller.go @@ -171,8 +171,8 @@ func (c *controller) OnEnter(g *gocui.Gui, v *gocui.View) error { switch view.Name() { case views.CHANNELS: c.views.SetPrevious(view) - _, cy := v.Cursor() - err := c.models.SetCurrentChannel(context.Background(), cy) + index := c.views.Channels.Index() + err := c.models.SetCurrentChannel(context.Background(), index) if err != nil { return err } diff --git a/ui/models/channels.go b/ui/models/channels.go index 71b95db..fac7d07 100644 --- a/ui/models/channels.go +++ b/ui/models/channels.go @@ -16,6 +16,10 @@ func (c *Channels) List() []*models.Channel { return c.list } +func (c *Channels) Len() int { + return len(c.list) +} + func (c *Channels) Get(index int) *models.Channel { if index < 0 || index > len(c.list)-1 { return nil diff --git a/ui/views/channels.go b/ui/views/channels.go index 394dd98..a235d8e 100644 --- a/ui/views/channels.go +++ b/ui/views/channels.go @@ -20,11 +20,16 @@ const ( ) type Channels struct { + index int columns *gocui.View view *gocui.View channels *models.Channels } +func (c Channels) Index() int { + return c.index +} + func (c Channels) Name() string { return CHANNELS } @@ -35,10 +40,17 @@ func (c *Channels) Wrap(v *gocui.View) view { } func (c *Channels) CursorDown() error { + if c.channels.Len() <= c.index+1 { + return nil + } + c.index++ return cursorDown(c.view, 1) } func (c *Channels) CursorUp() error { + if c.index > 0 { + c.index-- + } return cursorUp(c.view, 1) }