From c478b98b0fc6777455d08b3bb0164d600c5b726f Mon Sep 17 00:00:00 2001 From: erroneousboat Date: Sat, 3 Feb 2018 20:32:36 +0100 Subject: [PATCH 1/2] 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 From f2c61c7df2de605c716b3ace020436fe86de0f44 Mon Sep 17 00:00:00 2001 From: erroneousboat Date: Sat, 24 Feb 2018 12:10:30 +0100 Subject: [PATCH 2/2] Re-enable adding of line and supply documentation --- components/chat.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/components/chat.go b/components/chat.go index 34dda71..5dd2b93 100644 --- a/components/chat.go +++ b/components/chat.go @@ -81,9 +81,12 @@ func (c *Chat) Buffer() termui.Buffer { lines := []Line{} line := Line{} + // When we encounter a newline or are at the bounds of the chat view we + // stop iterating over the cells and add the line to the line array x := 0 for _, cell := range cells { + // When we encounter a newline we add the line to the array if cell.Ch == '\n' { lines = append(lines, line) @@ -105,6 +108,10 @@ func (c *Chat) Buffer() termui.Buffer { x++ } + // Append the last line to the array when we didn't encounter any + // newlines or were at the bounds of the chat view + 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. // Offset is the number which allows us to begin printing the