From 3b835e6a9565ec9fe706a93a89b48578237e7535 Mon Sep 17 00:00:00 2001 From: Naveen Mahalingam Date: Sun, 1 Sep 2024 07:05:17 -0700 Subject: [PATCH] table: Pager is now 1-indexed --- table/pager.go | 18 +++++++++--------- table/pager_test.go | 40 ++++++++++++++++++++-------------------- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/table/pager.go b/table/pager.go index 9d8e2a1..f4585b2 100644 --- a/table/pager.go +++ b/table/pager.go @@ -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 @@ -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 { diff --git a/table/pager_test.go b/table/pager_test.go index f98ab33..ec431b6 100644 --- a/table/pager_test.go +++ b/table/pager_test.go @@ -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)