Fix problem with scrolling and offset chat view

Fixes #83
This commit is contained in:
erroneousboat 2018-02-03 20:32:36 +01:00
parent bbc0f1ea04
commit c478b98b0f
2 changed files with 13 additions and 5 deletions

View File

@ -86,6 +86,8 @@ func (c *Chat) Buffer() termui.Buffer {
if cell.Ch == '\n' { if cell.Ch == '\n' {
lines = append(lines, line) lines = append(lines, line)
// Reset for new line
line = Line{} line = Line{}
x = 0 x = 0
continue continue
@ -93,6 +95,8 @@ func (c *Chat) Buffer() termui.Buffer {
if x+cell.Width() > c.List.InnerBounds().Dx() { if x+cell.Width() > c.List.InnerBounds().Dx() {
lines = append(lines, line) lines = append(lines, line)
// Reset for new line
line = Line{} line = Line{}
x = 0 x = 0
} }
@ -100,7 +104,6 @@ func (c *Chat) Buffer() termui.Buffer {
line.cells = append(line.cells, cell) line.cells = append(line.cells, cell)
x++ x++
} }
lines = append(lines, line)
// We will print lines bottom up, it will loop over the lines // 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. // 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 currentY := paneMaxY - 1
for i := (linesHeight - 1) - c.Offset; i >= 0; i-- { for i := (linesHeight - 1) - c.Offset; i >= 0; i-- {
if currentY < paneMinY { if currentY < paneMinY {
break break
} }
@ -190,6 +194,10 @@ func (c *Chat) GetMaxItems() int {
// SetMessages will put the provided messages into the Items field of the // SetMessages will put the provided messages into the Items field of the
// Chat view // Chat view
func (c *Chat) SetMessages(messages []string) { 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 { for _, msg := range messages {
c.List.Items = append(c.List.Items, html.UnescapeString(msg)) c.List.Items = append(c.List.Items, html.UnescapeString(msg))
} }
@ -216,8 +224,8 @@ func (c *Chat) ScrollUp() {
c.Offset = c.Offset + 10 c.Offset = c.Offset + 10
// Protect overscrolling // Protect overscrolling
if c.Offset > len(c.List.Items)-1 { if c.Offset > len(c.List.Items) {
c.Offset = len(c.List.Items) - 1 c.Offset = len(c.List.Items)
} }
} }

View File

@ -16,7 +16,7 @@ import (
) )
const ( const (
VERSION = "v0.3.1" VERSION = "v0.3.2"
USAGE = `NAME: USAGE = `NAME:
slack-term - slack client for your terminal slack-term - slack client for your terminal