Skip to content

Commit

Permalink
fix: issue when resizing the pager's buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
KonstantinGasser committed Sep 19, 2023
1 parent 6385dd5 commit 86ca9ec
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 67 deletions.
2 changes: 1 addition & 1 deletion app/component/tailing/tailing.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (model *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
break
}

model.pager.Rerender(model.width, model.height)
model.pager.Resize(model.width, model.height)

case tea.KeyMsg:
if !model.bindings.Matches(msg) {
Expand Down
37 changes: 27 additions & 10 deletions store/pager.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ func (pager *Pager) MovePosition() {
// shiftAppend takes the given lines and updates the pager's
// buffer such that the lines are append to the buffer and if
// nessecarry truncates the buffer.
//
// The shift value depends on the len(lines) and each item of
// the buffer is shifted N to the left leaving N free slots
// at the end of the buffer for the N new lines.
func (pager *Pager) shiftAppend(lines []string) {

var lineIndex int
Expand Down Expand Up @@ -128,24 +132,37 @@ func (pager *Pager) String() string {
}
}

// Rerender updates the pagers internal view which depends on
// the current tty width and height.
//
// Rerender flushes the current buffer and recomputes the lines
// visible within the new dimensions (width, height).
func (pager *Pager) Rerender(width int, height int) {
func (pager *Pager) Resize(width int, height int) {

pager.ttyWidth = width
pager.size = uint8(height)

start := clamp(int(pager.position) - len(pager.buffer))
items := make([]ring.Item, len(pager.buffer))
start := clamp(int(pager.position) - int(pager.size))

items := make([]ring.Item, pager.size)
pager.reader.OffsetRead(start, items)

pager.reload(items)
pager.bufferView = strings.Join(pager.buffer, "\n")
buf := make([]string, pager.size)
for i := range buf {
buf[i] = "\000"
}
pager.writeHead = 0

for _, item := range items {
lines := lineWrap(item, pager.ttyWidth)
pager.shiftAppend(lines)
}
}

// // Rerender updates the pagers internal view which depends on
// // the current tty width and height.
// //
// // Rerender flushes the current buffer and recomputes the lines
// // visible within the new dimensions (width, height).
//
// func (pager *Pager) Rerender(width int, height int) {
// pager.Resize(width, height)
// }
func (pager *Pager) reload(items ring.Slice) {

pager.buffer = make([]string, pager.size)
Expand Down
112 changes: 56 additions & 56 deletions store/pager_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package store

import (
"fmt"
"strings"
"testing"
"time"
Expand All @@ -11,61 +10,62 @@ var (
testRefreshRate = time.Nanosecond * 0 // evaluates to a nil time.Ticker causing asap update of pager
)

func TestMoveDownNoOverflow(t *testing.T) {

store := New(12)
pager := store.NewPager(4, 23, testRefreshRate)

prefix := "test-label | "
for i := 0; i < 4; i++ {
store.Insert("test-label", len(prefix), []byte(fmt.Sprintf("%sLine-%d", prefix, i+1)))
}

sequence := []string{
"test-label | Line-1\n\x00\n\x00\n\x00",
"test-label | Line-1\ntest-label | Line-2\n\x00\n\x00",
"test-label | Line-1\ntest-label | Line-2\ntest-label | Line-3\n\x00",
"test-label | Line-1\ntest-label | Line-2\ntest-label | Line-3\ntest-label | Line-4",
}

// seqID := 0
for _, seq := range sequence {
pager.MoveDownDeprecated(false)
if seq != pager.String() {
t.Fatalf("[pager.MoveDown] expected line(s):\n%q\ngot:\n%q", seq, pager.String())
}
}
}

func TestMoveDownOverflow(t *testing.T) {

store := New(12)
pager := store.NewPager(4, 23, testRefreshRate)

prefix := "test-label | "
for i := 0; i < 9; i++ {
store.Insert("test-label", len(prefix), []byte(fmt.Sprintf("%sLine-%d", prefix, i+1)))
}

sequence := []string{
"test-label | Line-1\n\x00\n\x00\n\x00",
"test-label | Line-1\ntest-label | Line-2\n\x00\n\x00",
"test-label | Line-1\ntest-label | Line-2\ntest-label | Line-3\n\x00",
"test-label | Line-1\ntest-label | Line-2\ntest-label | Line-3\ntest-label | Line-4",
"test-label | Line-2\ntest-label | Line-3\ntest-label | Line-4\ntest-label | Line-5",
"test-label | Line-3\ntest-label | Line-4\ntest-label | Line-5\ntest-label | Line-6",
"test-label | Line-4\ntest-label | Line-5\ntest-label | Line-6\ntest-label | Line-7",
"test-label | Line-5\ntest-label | Line-6\ntest-label | Line-7\ntest-label | Line-8",
"test-label | Line-6\ntest-label | Line-7\ntest-label | Line-8\ntest-label | Line-9",
}

for _, seq := range sequence {
pager.MoveDownDeprecated(false)
if seq != pager.String() {
t.Fatalf("[pager.MoveDown] expected line(s):\n%q\ngot:\n%q", seq, pager.String())
}
}
}
//
// func TestMoveDownNoOverflow(t *testing.T) {
//
// store := New(12)
// pager := store.NewPager(4, 23, testRefreshRate)
//
// prefix := "test-label | "
// for i := 0; i < 4; i++ {
// store.Insert("test-label", len(prefix), []byte(fmt.Sprintf("%sLine-%d", prefix, i+1)))
// }
//
// sequence := []string{
// "test-label | Line-1\n\x00\n\x00\n\x00",
// "test-label | Line-1\ntest-label | Line-2\n\x00\n\x00",
// "test-label | Line-1\ntest-label | Line-2\ntest-label | Line-3\n\x00",
// "test-label | Line-1\ntest-label | Line-2\ntest-label | Line-3\ntest-label | Line-4",
// }
//
// // seqID := 0
// for _, seq := range sequence {
// pager.MoveDownDeprecated(false)
// if seq != pager.String() {
// t.Fatalf("[pager.MoveDown] expected line(s):\n%q\ngot:\n%q", seq, pager.String())
// }
// }
// }
//
// func TestMoveDownOverflow(t *testing.T) {
//
// store := New(12)
// pager := store.NewPager(4, 23, testRefreshRate)
//
// prefix := "test-label | "
// for i := 0; i < 9; i++ {
// store.Insert("test-label", len(prefix), []byte(fmt.Sprintf("%sLine-%d", prefix, i+1)))
// }
//
// sequence := []string{
// "test-label | Line-1\n\x00\n\x00\n\x00",
// "test-label | Line-1\ntest-label | Line-2\n\x00\n\x00",
// "test-label | Line-1\ntest-label | Line-2\ntest-label | Line-3\n\x00",
// "test-label | Line-1\ntest-label | Line-2\ntest-label | Line-3\ntest-label | Line-4",
// "test-label | Line-2\ntest-label | Line-3\ntest-label | Line-4\ntest-label | Line-5",
// "test-label | Line-3\ntest-label | Line-4\ntest-label | Line-5\ntest-label | Line-6",
// "test-label | Line-4\ntest-label | Line-5\ntest-label | Line-6\ntest-label | Line-7",
// "test-label | Line-5\ntest-label | Line-6\ntest-label | Line-7\ntest-label | Line-8",
// "test-label | Line-6\ntest-label | Line-7\ntest-label | Line-8\ntest-label | Line-9",
// }
//
// for _, seq := range sequence {
// pager.MoveDownDeprecated(false)
// if seq != pager.String() {
// t.Fatalf("[pager.MoveDown] expected line(s):\n%q\ngot:\n%q", seq, pager.String())
// }
// }
// }

func TestMoveDownAssertHeight(t *testing.T) {

Expand Down

0 comments on commit 86ca9ec

Please sign in to comment.