shithub: hell

Download patch

ref: 1f03cd5a5c5f2b2a351c7169c149f87ac1ec1730
parent: 7d1a10551f567003be765fff0e56a84a43423835
author: penny <penny@limitedideas.org>
date: Thu Sep 25 19:15:13 EDT 2025

working on window sized based page rendering

--- a/main.go
+++ b/main.go
@@ -123,17 +123,16 @@
 				return
 			case "next":
 				hc.page.Next()
-				fmt.Print(hc.page.String())
+				fmt.Printf(hc.page.String())
 				return
 			case "bookmarks":
-				hc.pause(true)
 				hc.page = &Page{}
 				hc.page.loader = &StatusPages{hc: hc}
-				fmt.Print(hc.page.String())
+				fmt.Printf(hc.page.String())
 				return
 			case "notice":
 				defer hc.updatePrompt()
-				hc.pause(true)
+				defer hc.pause(true)
 				notifications, err := hc.GetUnreadNotifications()
 				if len(notifications) > 0 {
 					hc.PrintNotifications(notifications)
--- a/pages.go
+++ b/pages.go
@@ -6,6 +6,7 @@
 	"strings"
 	
 	"codeberg.org/penny64/hellclient-go-mastodon"
+	"golang.org/x/term"
 )
 
 type StatusLoader interface {
@@ -16,8 +17,10 @@
 	loader StatusLoader
 	//Array index for the item buffer
 	index int
+	//Index of the last item on the page
+	indexend int
 	//our previous index for going back
-	previndex int
+	previndexes []int
 	//Semantic page number for the UI
 	page int
 	itembuffer *[]PageItem
@@ -38,6 +41,12 @@
 	lines    int
 }
 
+func makePageItem(renderedtext string) (page PageItem) {
+	page.lines = strings.Count(renderedtext, "\n") + 1
+	page.itemtext = renderedtext
+	return
+}
+
 func (statusData *StatusPages) Load(limit int) *[]PageItem {
 	if statusData.page == nil {
 		statusData.page = &mastodon.Pagination{}
@@ -52,7 +61,7 @@
 	
 	var itemArray []PageItem
 	for i, _ := range statuses {
-		item := PageItem{itemtext: statusData.hc.renderAndIncrement(statusData.hc.ctxref, statuses[i]) + "\n"}
+		item := makePageItem(statusData.hc.renderAndIncrement(statusData.hc.ctxref, statuses[i]) + "\n")
 		itemArray = append(itemArray, item)
 	}
 	statusData.page.MinID = ""
@@ -75,7 +84,7 @@
 	noticeArray := noticeData.hc.RenderNotificationsArray(notices)
 	var itemArray []PageItem
 	for i, _ := range noticeArray {
-		item := PageItem{itemtext: noticeArray[i]}
+		item := makePageItem(noticeArray[i])
 		itemArray = append(itemArray, item)
 	}
 	noticeData.page.MinID = ""
@@ -82,27 +91,47 @@
 	return &itemArray
 }
 
+func (page *Page) findIndexEnd() {
+	_, windowheight, err := term.GetSize(int(0))
+	if err != nil {
+		page.indexend = page.index + 5
+		return
+	}
+	page.indexend = page.index - 1
+	var items []PageItem
+	if len(*page.itembuffer) >= page.index {
+		items = (*page.itembuffer)[page.index:]
+	}
+	var linecount int
+	for i, _ := range items {
+		if ((*page.itembuffer)[i].lines + linecount > windowheight) {
+			return
+		}
+		linecount = linecount + (*page.itembuffer)[i].lines
+		page.indexend++
+	}
+	return
+}
+
 func (page *Page) Buffer() {
 	if page.itembuffer == nil {
 		page.itembuffer = &[]PageItem{}
 	}
-	if len(*page.itembuffer) < (page.index + 5) {
+	if page.indexend == 0 {
 		*page.itembuffer = append(*page.itembuffer, *page.loader.Load(20)...)
-
 	}
+	if len(*page.itembuffer) < page.indexend {
+		*page.itembuffer = append(*page.itembuffer, *page.loader.Load(20)...)
+	}
 }
 
 func (page *Page) String() string {
 	var sb strings.Builder
 	page.Buffer()
+	page.findIndexEnd()
 	var items []PageItem
-	if len(*page.itembuffer) >= page.index {
-		items = (*page.itembuffer)[page.index:]
-	}
+	items = (*page.itembuffer)[page.index:page.indexend]
 
-	if len(items) > 5 {
-		items = items[:5]
-	}
 	reverseditems := reverseArray(items)
 	for i, _ := range reverseditems {
 		sb.WriteString(reverseditems[i].itemtext)
@@ -113,7 +142,10 @@
 
 func (page *Page) Next() {
 	page.page++
-	page.index = page.index + 5
+	page.index = page.indexend
+	page.indexend = 0
+	page.Buffer()
+	page.findIndexEnd()
 }
 
 func (page *Page) Prev() {
--