Skip to content

Commit

Permalink
table: Pager is now 1-indexed
Browse files Browse the repository at this point in the history
  • Loading branch information
jedib0t committed Sep 1, 2024
1 parent aafad7e commit 3b835e6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 29 deletions.
18 changes: 9 additions & 9 deletions table/pager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (

// Pager lets you interact with the table rendering in a paged manner.
type Pager interface {
// GoTo moves to the given 0-indexed page number.
// GoTo moves to the given 1-indexed page number.
GoTo(pageNum int) string
// Location returns the current page number in 0-indexed form.
// Location returns the current page number in 1-indexed form.
Location() int
// Next moves to the next available page and returns the same.
Next() string
Expand All @@ -29,18 +29,18 @@ type pager struct {
}

func (p *pager) GoTo(pageNum int) string {
if pageNum < 0 {
pageNum = 0
if pageNum < 1 {
pageNum = 1
}
if pageNum >= len(p.pages) {
pageNum = len(p.pages) - 1
if pageNum > len(p.pages) {
pageNum = len(p.pages)
}
p.index = pageNum
return p.pages[pageNum]
p.index = pageNum - 1
return p.pages[p.index]
}

func (p *pager) Location() int {
return p.index
return p.index + 1
}

func (p *pager) Next() string {
Expand Down
40 changes: 20 additions & 20 deletions table/pager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,51 +54,51 @@ func TestPager(t *testing.T) {
compareOutput(t, expectedOutput, tw.Render())

p := tw.Pager(PageSize(3))
assert.Equal(t, 0, p.Location())
assert.Equal(t, 1, p.Location())
compareOutput(t, expectedOutputP1, p.Render())
compareOutput(t, expectedOutputP2, p.Next())
compareOutput(t, expectedOutputP2, p.Render())
assert.Equal(t, 1, p.Location())
assert.Equal(t, 2, p.Location())
compareOutput(t, expectedOutputP3, p.Next())
compareOutput(t, expectedOutputP3, p.Render())
assert.Equal(t, 2, p.Location())
assert.Equal(t, 3, p.Location())
compareOutput(t, expectedOutputP4, p.Next())
compareOutput(t, expectedOutputP4, p.Render())
assert.Equal(t, 3, p.Location())
assert.Equal(t, 4, p.Location())
compareOutput(t, expectedOutputP4, p.Next())
compareOutput(t, expectedOutputP4, p.Render())
assert.Equal(t, 3, p.Location())
assert.Equal(t, 4, p.Location())
compareOutput(t, expectedOutputP3, p.Prev())
compareOutput(t, expectedOutputP3, p.Render())
assert.Equal(t, 2, p.Location())
assert.Equal(t, 3, p.Location())
compareOutput(t, expectedOutputP2, p.Prev())
compareOutput(t, expectedOutputP2, p.Render())
assert.Equal(t, 1, p.Location())
assert.Equal(t, 2, p.Location())
compareOutput(t, expectedOutputP1, p.Prev())
compareOutput(t, expectedOutputP1, p.Render())
assert.Equal(t, 0, p.Location())
assert.Equal(t, 1, p.Location())
compareOutput(t, expectedOutputP1, p.Prev())
compareOutput(t, expectedOutputP1, p.Render())
assert.Equal(t, 0, p.Location())
assert.Equal(t, 1, p.Location())

compareOutput(t, expectedOutputP1, p.GoTo(-1))
compareOutput(t, expectedOutputP1, p.Render())
assert.Equal(t, 0, p.Location())
compareOutput(t, expectedOutputP1, p.GoTo(0))
compareOutput(t, expectedOutputP1, p.Render())
assert.Equal(t, 0, p.Location())
compareOutput(t, expectedOutputP2, p.GoTo(1))
compareOutput(t, expectedOutputP2, p.Render())
assert.Equal(t, 1, p.Location())
compareOutput(t, expectedOutputP3, p.GoTo(2))
compareOutput(t, expectedOutputP3, p.Render())
compareOutput(t, expectedOutputP1, p.GoTo(1))
compareOutput(t, expectedOutputP1, p.Render())
assert.Equal(t, 1, p.Location())
compareOutput(t, expectedOutputP2, p.GoTo(2))
compareOutput(t, expectedOutputP2, p.Render())
assert.Equal(t, 2, p.Location())
compareOutput(t, expectedOutputP4, p.GoTo(3))
compareOutput(t, expectedOutputP4, p.Render())
compareOutput(t, expectedOutputP3, p.GoTo(3))
compareOutput(t, expectedOutputP3, p.Render())
assert.Equal(t, 3, p.Location())
compareOutput(t, expectedOutputP4, p.GoTo(4))
compareOutput(t, expectedOutputP4, p.Render())
assert.Equal(t, 3, p.Location())
assert.Equal(t, 4, p.Location())
compareOutput(t, expectedOutputP4, p.GoTo(5))
compareOutput(t, expectedOutputP4, p.Render())
assert.Equal(t, 4, p.Location())

sb := strings.Builder{}
p.SetOutputMirror(&sb)
Expand Down

0 comments on commit 3b835e6

Please sign in to comment.