Skip to content

Commit

Permalink
chore: fixup
Browse files Browse the repository at this point in the history
Signed-off-by: moul <94029+moul@users.noreply.github.com>
  • Loading branch information
moul committed Sep 24, 2024
1 parent 7d44835 commit c8e4e1f
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 104 deletions.
53 changes: 31 additions & 22 deletions examples/gno.land/p/demo/avl/pager/pager.gno
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
type Pager struct {
Tree *avl.Tree
PageQueryParam string
SizeQueryParam string
DefaultPageSize int
}

Expand All @@ -39,6 +40,7 @@ func NewPager(tree *avl.Tree) *Pager {
return &Pager{
Tree: tree,
PageQueryParam: "page",
SizeQueryParam: "size",
DefaultPageSize: 10,
}
}
Expand Down Expand Up @@ -115,8 +117,11 @@ func (p *Pager) GetPageByPath(rawURL string) (*Page, error) {
return p.GetPage(pageNumber, pageSize), nil
}

// RenderSelector generates the Markdown for the page selector.
func (p *Page) RenderSelector() string {
// UI generates the Markdown UI for the page selector.
func (p *Page) Selector() string {
pageNumber := p.PageNumber
pageNumber = max(pageNumber, 1)

if p.TotalPages <= 1 {
return ""
}
Expand All @@ -128,9 +133,7 @@ func (p *Page) RenderSelector() string {
md += ufmt.Sprintf("[%d](?%s=%d) | ", 1, p.Pager.PageQueryParam, 1)

// Before
if p.PageNumber == 5 {
md += ufmt.Sprintf("[%d](?%s=%d) | ", 2, p.Pager.PageQueryParam, 2)
} else if p.PageNumber > 4 {
if p.PageNumber > 4 {
md += "… | "
}

Expand All @@ -143,8 +146,12 @@ func (p *Page) RenderSelector() string {
}
}

// Current page
md += ufmt.Sprintf("**%d**", p.PageNumber)
if p.PageNumber > 0 && p.PageNumber <= p.TotalPages {
// Current page
md += ufmt.Sprintf("**%d**", p.PageNumber)
} else {
md += ufmt.Sprintf("_%d_", p.PageNumber)
}

if p.HasNext {
md += " | "
Expand All @@ -157,9 +164,7 @@ func (p *Page) RenderSelector() string {
md += ufmt.Sprintf("[%d](?%s=%d) | ", p.PageNumber+2, p.Pager.PageQueryParam, p.PageNumber+2)
}

if p.PageNumber == p.TotalPages-4 {
md += ufmt.Sprintf("[%d](?%s=%d) | ", p.PageNumber+3, p.Pager.PageQueryParam, p.PageNumber+3)
} else if p.PageNumber < p.TotalPages-3 {
if p.PageNumber < p.TotalPages-3 {
md += "… | "
}

Expand All @@ -181,32 +186,36 @@ func (p *Pager) ParseQuery(rawURL string) (int, int, error) {
pageNumber := 1
pageSize := p.DefaultPageSize

if pageStr := query.Get(p.PageQueryParam); pageStr != "" {
pageNumber, err = strconv.Atoi(pageStr)
if err != nil || pageNumber < 1 {
pageNumber = 1
if p.PageQueryParam != "" {
if pageStr := query.Get(p.PageQueryParam); pageStr != "" {
pageNumber, err = strconv.Atoi(pageStr)
if err != nil || pageNumber < 1 {
pageNumber = 1
}
}
}

if sizeStr := query.Get("size"); sizeStr != "" {
pageSize, err = strconv.Atoi(sizeStr)
if err != nil || pageSize < 1 {
pageSize = p.DefaultPageSize
if p.SizeQueryParam != "" {
if sizeStr := query.Get(p.SizeQueryParam); sizeStr != "" {
pageSize, err = strconv.Atoi(sizeStr)
if err != nil || pageSize < 1 {
pageSize = p.DefaultPageSize
}
}
}

return pageNumber, pageSize, nil
}

func max(a, b int) int {
if a > b {
func min(a, b int) int {
if a < b {
return a
}
return b
}

func min(a, b int) int {
if a < b {
func max(a, b int) int {
if a > b {
return a
}
return b
Expand Down
19 changes: 11 additions & 8 deletions examples/gno.land/p/demo/avl/pager/pager_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestPager_GetPageByPath(t *testing.T) {
}
}

func TestPage_RenderSelector(t *testing.T) {
func TestPage_Selector(t *testing.T) {
// Create a new AVL tree and populate it with some key-value pairs.
tree := avl.NewTree()
tree.Set("a", 1)
Expand All @@ -106,12 +106,12 @@ func TestPage_RenderSelector(t *testing.T) {
for _, tt := range tests {
page := pager.GetPage(tt.pageNumber, tt.pageSize)

md := page.RenderSelector()
uassert.Equal(t, tt.expected, md)
ui := page.Selector()
uassert.Equal(t, tt.expected, ui)
}
}

func TestPager_RenderSelector_WithManyPages(t *testing.T) {
func TestPager_UI_WithManyPages(t *testing.T) {
// Create a new AVL tree and populate it with many key-value pairs.
tree := avl.NewTree()
for i := 0; i < 100; i++ {
Expand All @@ -127,23 +127,26 @@ func TestPager_RenderSelector_WithManyPages(t *testing.T) {
pageSize int
expected string
}{
// XXX: -1
// XXX: 0
{1, 10, "**1** | [2](?page=2) | [3](?page=3) | … | [10](?page=10)"},
{2, 10, "[1](?page=1) | **2** | [3](?page=3) | [4](?page=4) | … | [10](?page=10)"},
{3, 10, "[1](?page=1) | [2](?page=2) | **3** | [4](?page=4) | [5](?page=5) | … | [10](?page=10)"},
{4, 10, "[1](?page=1) | [2](?page=2) | [3](?page=3) | **4** | [5](?page=5) | [6](?page=6) | … | [10](?page=10)"},
{5, 10, "[1](?page=1) | [2](?page=2) | [3](?page=3) | [4](?page=4) | **5** | [6](?page=6) | [7](?page=7) | … | [10](?page=10)"},
{6, 10, "[1](?page=1) | … | [4](?page=4) | [5](?page=5) | **6** | [7](?page=7) | [8](?page=8) | [9](?page=9) | [10](?page=10)"},
{5, 10, "[1](?page=1) | | [3](?page=3) | [4](?page=4) | **5** | [6](?page=6) | [7](?page=7) | … | [10](?page=10)"},
{6, 10, "[1](?page=1) | … | [4](?page=4) | [5](?page=5) | **6** | [7](?page=7) | [8](?page=8) | | [10](?page=10)"},
{7, 10, "[1](?page=1) | … | [5](?page=5) | [6](?page=6) | **7** | [8](?page=8) | [9](?page=9) | [10](?page=10)"},
{8, 10, "[1](?page=1) | … | [6](?page=6) | [7](?page=7) | **8** | [9](?page=9) | [10](?page=10)"},
{9, 10, "[1](?page=1) | … | [7](?page=7) | [8](?page=8) | **9** | [10](?page=10)"},
{10, 10, "[1](?page=1) | … | [8](?page=8) | [9](?page=9) | **10**"},
// XXX: 11
}

for _, tt := range tests {
page := pager.GetPage(tt.pageNumber, tt.pageSize)

md := page.RenderSelector()
uassert.Equal(t, tt.expected, md)
ui := page.Selector()
uassert.Equal(t, tt.expected, ui)
}
}

Expand Down
134 changes: 60 additions & 74 deletions examples/gno.land/p/demo/avl/pager/z_filetest.gno
Original file line number Diff line number Diff line change
Expand Up @@ -21,95 +21,81 @@ func main() {
for pn := -1; pn < 8; pn++ {
page := pager.GetPage(pn, 7)

// Print the items on the page.
println(ufmt.Sprintf("Page %d of %d", page.PageNumber, page.TotalPages))
for _, item := range page.Items {
println(ufmt.Sprintf("Key: %s, Value: %v", item.Key, item.Value))
println(ufmt.Sprintf("## Page %d of %d", page.PageNumber, page.TotalPages))
for idx, item := range page.Items {
println(ufmt.Sprintf("- idx=%d key=%s value=%d", idx, item.Key, item.Value))
}

// Render and print the page selector.
pageSelector := page.RenderSelector()
println("Page Selector:")
println(pageSelector)
println(page.Selector())
println()
}
}

// Output:
// Page 0 of 6
// Page Selector:
// **0** | [1](?page=1) | [2](?page=2) | … | [6](?page=6)
// ## Page 0 of 6
// _0_ | [1](?page=1) | [2](?page=2) | … | [6](?page=6)
//
// Page 0 of 6
// Page Selector:
// **0** | [1](?page=1) | [2](?page=2) | … | [6](?page=6)
// ## Page 0 of 6
// _0_ | [1](?page=1) | [2](?page=2) | … | [6](?page=6)
//
// Page 1 of 6
// Key: 0000001, Value: (unhandled)
// Key: 0000002, Value: (unhandled)
// Key: 0000003, Value: (unhandled)
// Key: 0000004, Value: (unhandled)
// Key: 0000005, Value: (unhandled)
// Key: 0000006, Value: (unhandled)
// Key: 0000007, Value: (unhandled)
// Page Selector:
// ## Page 1 of 6
// - idx=0 key=0000001 value=0
// - idx=1 key=0000002 value=1
// - idx=2 key=0000003 value=2
// - idx=3 key=0000004 value=3
// - idx=4 key=0000005 value=4
// - idx=5 key=0000006 value=5
// - idx=6 key=0000007 value=6
// **1** | [2](?page=2) | [3](?page=3) | … | [6](?page=6)
//
// Page 2 of 6
// Key: 0000008, Value: (unhandled)
// Key: 0000009, Value: (unhandled)
// Key: 000000a, Value: (unhandled)
// Key: 000000b, Value: (unhandled)
// Key: 000000c, Value: (unhandled)
// Key: 000000d, Value: (unhandled)
// Key: 000000e, Value: (unhandled)
// Page Selector:
// [1](?page=1) | **2** | [3](?page=3) | [4](?page=4) | [5](?page=5) | [6](?page=6)
// ## Page 2 of 6
// - idx=0 key=0000008 value=7
// - idx=1 key=0000009 value=8
// - idx=2 key=000000a value=9
// - idx=3 key=000000b value=10
// - idx=4 key=000000c value=11
// - idx=5 key=000000d value=12
// - idx=6 key=000000e value=13
// [1](?page=1) | **2** | [3](?page=3) | [4](?page=4) | … | [6](?page=6)
//
// Page 3 of 6
// Key: 000000f, Value: (unhandled)
// Key: 000000g, Value: (unhandled)
// Key: 000000h, Value: (unhandled)
// Key: 000000j, Value: (unhandled)
// Key: 000000k, Value: (unhandled)
// Key: 000000m, Value: (unhandled)
// Key: 000000n, Value: (unhandled)
// Page Selector:
// ## Page 3 of 6
// - idx=0 key=000000f value=14
// - idx=1 key=000000g value=15
// - idx=2 key=000000h value=16
// - idx=3 key=000000j value=17
// - idx=4 key=000000k value=18
// - idx=5 key=000000m value=19
// - idx=6 key=000000n value=20
// [1](?page=1) | [2](?page=2) | **3** | [4](?page=4) | [5](?page=5) | [6](?page=6)
//
// Page 4 of 6
// Key: 000000p, Value: (unhandled)
// Key: 000000q, Value: (unhandled)
// Key: 000000r, Value: (unhandled)
// Key: 000000s, Value: (unhandled)
// Key: 000000t, Value: (unhandled)
// Key: 000000v, Value: (unhandled)
// Key: 000000w, Value: (unhandled)
// Page Selector:
// ## Page 4 of 6
// - idx=0 key=000000p value=21
// - idx=1 key=000000q value=22
// - idx=2 key=000000r value=23
// - idx=3 key=000000s value=24
// - idx=4 key=000000t value=25
// - idx=5 key=000000v value=26
// - idx=6 key=000000w value=27
// [1](?page=1) | [2](?page=2) | [3](?page=3) | **4** | [5](?page=5) | [6](?page=6)
//
// Page 5 of 6
// Key: 000000x, Value: (unhandled)
// Key: 000000y, Value: (unhandled)
// Key: 000000z, Value: (unhandled)
// Key: 0000010, Value: (unhandled)
// Key: 0000011, Value: (unhandled)
// Key: 0000012, Value: (unhandled)
// Key: 0000013, Value: (unhandled)
// Page Selector:
// [1](?page=1) | [2](?page=2) | [3](?page=3) | [4](?page=4) | **5** | [6](?page=6)
// ## Page 5 of 6
// - idx=0 key=000000x value=28
// - idx=1 key=000000y value=29
// - idx=2 key=000000z value=30
// - idx=3 key=0000010 value=31
// - idx=4 key=0000011 value=32
// - idx=5 key=0000012 value=33
// - idx=6 key=0000013 value=34
// [1](?page=1) | … | [3](?page=3) | [4](?page=4) | **5** | [6](?page=6)
//
// Page 6 of 6
// Key: 0000014, Value: (unhandled)
// Key: 0000015, Value: (unhandled)
// Key: 0000016, Value: (unhandled)
// Key: 0000017, Value: (unhandled)
// Key: 0000018, Value: (unhandled)
// Key: 0000019, Value: (unhandled)
// Key: 000001a, Value: (unhandled)
// Page Selector:
// ## Page 6 of 6
// - idx=0 key=0000014 value=35
// - idx=1 key=0000015 value=36
// - idx=2 key=0000016 value=37
// - idx=3 key=0000017 value=38
// - idx=4 key=0000018 value=39
// - idx=5 key=0000019 value=40
// - idx=6 key=000001a value=41
// [1](?page=1) | … | [4](?page=4) | [5](?page=5) | **6**
//
// Page 7 of 6
// Page Selector:
// [1](?page=1) | … | [5](?page=5) | [6](?page=6) | **7**
// ## Page 7 of 6
// [1](?page=1) | … | [5](?page=5) | [6](?page=6) | _7_

0 comments on commit c8e4e1f

Please sign in to comment.