ref: e0019f69bdca6d7a8c81aa111966a8fe8e6ef083
parent: 634b1bc07a8771b5e281d04e4773cb65c270da88
author: penny <penny@limitedideas.org>
date: Thu Oct 2 11:26:33 EDT 2025
document pages.go
--- a/pages.go
+++ b/pages.go
@@ -10,10 +10,13 @@
"golang.org/x/term"
)
+// Loads rendered status items ready for pagination
type StatusLoader interface {Load(int) *[]PageItem
}
+// Get interface for loading mastodon statuses
+// The status loader uses this as the provider for statuses
type StatusGetter interface {Get(int) ([]*mastodon.Status, error)
}
@@ -22,17 +25,19 @@
client *mastodon.Client
}
+// Get(int) implementation for mastodon client calls that use the same function signature
type BasicStatusGetter struct {getter func(ctx context.Context, pg *mastodon.Pagination) ([]*mastodon.Status, error)
page *mastodon.Pagination
}
+// Get implementation for loading a status thread
type ThreadStatusGetter struct {target *mastodon.Status
client *mastodon.Client
loaded bool
}
-
+// Thread status getter
// The API doesn't take a limit for this so we ignore the limit
func (getter *ThreadStatusGetter) Get(limit int) ([]*mastodon.Status, error) { if getter.loaded {@@ -50,6 +55,7 @@
return statuses, err
}
+// Get the status calls with the getter.getter function
func (getter *BasicStatusGetter) Get(limit int) ([]*mastodon.Status, error) { if getter.page == nil { getter.page = &mastodon.Pagination{}@@ -64,6 +70,7 @@
return statuses, err
}
+// Get implementation for profile statuses
type AccountStatusGetter struct {page *mastodon.Pagination
ID mastodon.ID
@@ -70,6 +77,7 @@
client *mastodon.Client
}
+// Gets statuses for account up to limit
func (getter *AccountStatusGetter) Get(limit int) ([]*mastodon.Status, error) { if getter.page == nil { getter.page = &mastodon.Pagination{}@@ -83,6 +91,7 @@
return statuses, err
}
+// Page implements paginated dynamic rendering of PageItems
type Page struct {loader StatusLoader
//Array index for the item buffer
@@ -98,10 +107,6 @@
itembuffer *[]PageItem
findindex sync.Once
}
-type NotificationPages struct {- hc *Hellclient
- page *mastodon.Pagination
-}
type StatusPages struct {hc *Hellclient
@@ -137,6 +142,13 @@
return &itemArray
}
+// Notification PageItem loader
+type NotificationPages struct {+ hc *Hellclient
+ page *mastodon.Pagination
+}
+
+// Returns limit of rendered notification PageItems
func (noticeData *NotificationPages) Load(limit int) *[]PageItem { if noticeData.page == nil { noticeData.page = &mastodon.Pagination{}@@ -162,6 +174,8 @@
return &itemArray
}
+// Get how many of the page items fit within the given window height
+// And whether we had enough items to bump off of the screen
func findIndex(height int, items []PageItem) (index int, bumped bool) {linecount := 5
for i := range items {@@ -182,6 +196,7 @@
return index, false
}
+// adjusts page indexes to show the previous page
func (page *Page) findIndexStart() {_, windowheight, err := term.GetSize(int(0))
if err != nil {@@ -196,6 +211,7 @@
page.index = page.index - newindex
}
+// increases indexes to the next page, buffering more PostItems as needed
func (page *Page) findIndexEnd() {_, windowheight, err := term.GetSize(int(0))
if err != nil {@@ -230,6 +246,7 @@
}
}
+// Load a new batch of statusitems into the buffer
func (page *Page) Buffer() { if page.itembuffer == nil { page.itembuffer = &[]PageItem{}@@ -237,6 +254,7 @@
*page.itembuffer = append(*page.itembuffer, *page.loader.Load(20)...)
}
+// Returns the current page rendered as text
func (page *Page) String() string {var sb strings.Builder
page.findindex.Do(page.findIndexEnd)
@@ -254,6 +272,7 @@
return sb.String()
}
+// Advance the page to the next indexes
func (page *Page) Next() {page.page++
page.index = page.indexend
@@ -261,6 +280,7 @@
page.findIndexEnd()
}
+// Go to the previous page of items
func (page *Page) Prev() {page.findIndexStart()
if page.page > 0 {@@ -268,6 +288,7 @@
}
}
+// Simple navigation hint line
func (page *Page) pageTitle() string { return fmt.Sprintf("Page %v. /next for next page /prev for previous\n", page.page+1)}
--
⑨