From c478b98b0fc6777455d08b3bb0164d600c5b726f Mon Sep 17 00:00:00 2001 From: erroneousboat Date: Sat, 3 Feb 2018 20:32:36 +0100 Subject: [PATCH] Fix problem with scrolling and offset chat view Fixes #83 --- components/chat.go | 14 +++++++++++--- main.go | 4 ++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/components/chat.go b/components/chat.go index 3fbafec..34dda71 100644 --- a/components/chat.go +++ b/components/chat.go @@ -86,6 +86,8 @@ func (c *Chat) Buffer() termui.Buffer { if cell.Ch == '\n' { lines = append(lines, line) + + // Reset for new line line = Line{} x = 0 continue @@ -93,6 +95,8 @@ func (c *Chat) Buffer() termui.Buffer { if x+cell.Width() > c.List.InnerBounds().Dx() { lines = append(lines, line) + + // Reset for new line line = Line{} x = 0 } @@ -100,7 +104,6 @@ func (c *Chat) Buffer() termui.Buffer { line.cells = append(line.cells, cell) x++ } - lines = append(lines, line) // We will print lines bottom up, it will loop over the lines // backwards and for every line it'll set the cell in that line. @@ -113,6 +116,7 @@ func (c *Chat) Buffer() termui.Buffer { currentY := paneMaxY - 1 for i := (linesHeight - 1) - c.Offset; i >= 0; i-- { + if currentY < paneMinY { break } @@ -190,6 +194,10 @@ func (c *Chat) GetMaxItems() int { // SetMessages will put the provided messages into the Items field of the // Chat view func (c *Chat) SetMessages(messages []string) { + // Reset offset first, when scrolling in view and changing channels we + // want the offset to be 0 when loading new messages + c.Offset = 0 + for _, msg := range messages { c.List.Items = append(c.List.Items, html.UnescapeString(msg)) } @@ -216,8 +224,8 @@ func (c *Chat) ScrollUp() { c.Offset = c.Offset + 10 // Protect overscrolling - if c.Offset > len(c.List.Items)-1 { - c.Offset = len(c.List.Items) - 1 + if c.Offset > len(c.List.Items) { + c.Offset = len(c.List.Items) } } diff --git a/main.go b/main.go index 77b26a0..d862702 100644 --- a/main.go +++ b/main.go @@ -16,7 +16,7 @@ import ( ) const ( - VERSION = "v0.3.1" + VERSION = "v0.3.2" USAGE = `NAME: slack-term - slack client for your terminal @@ -27,7 +27,7 @@ VERSION: %s WEBSITE: - https://github.com/erroneousboat/slack-term + https://github.com/erroneousboat/slack-term GLOBAL OPTIONS: --help, -h