github.com/xyproto/orbiton/v2@v2.65.12-0.20240516144430-e10a419274ec/pages.go (about) 1 package main 2 3 import ( 4 "github.com/xyproto/vt100" 5 ) 6 7 // Page represents a single page of text. 8 type Page struct { 9 Lines []string 10 } 11 12 // ScrollableTextBox holds pages of text and keeps track of the current page. 13 type ScrollableTextBox struct { 14 *Box 15 Pages []Page 16 CurrentPage int 17 } 18 19 // NewScrollableTextBox creates a new instance of ScrollableText which also encapsulates a Box 20 func NewScrollableTextBox(pages []Page) *ScrollableTextBox { 21 return &ScrollableTextBox{ 22 Box: &Box{0, 0, 0, 0}, 23 Pages: pages, 24 CurrentPage: 0, 25 } 26 } 27 28 // DrawScrollableText will draw a scrollable text widget. 29 // Takes a Box struct for the size and position. 30 // Uses bt.Foreground and bt.Background. 31 func (e *Editor) DrawScrollableText(bt *BoxTheme, c *vt100.Canvas, stb *ScrollableTextBox) { 32 if stb.CurrentPage >= len(stb.Pages) || stb.CurrentPage < 0 { 33 // Invalid page number, do nothing or log an error 34 return 35 } 36 37 page := stb.Pages[stb.CurrentPage] 38 x := uint(stb.X) 39 for i, s := range page.Lines { 40 y := uint(stb.Y + i) 41 if int(y) < stb.Y+stb.H { // Ensure we're within the box height 42 c.Write(x, y, *bt.Foreground, *bt.Background, s) 43 } 44 } 45 } 46 47 // NextPage advances to the next page if there is one. 48 func (stb *ScrollableTextBox) NextPage() { 49 if stb.CurrentPage < len(stb.Pages)-1 { 50 stb.CurrentPage++ 51 } 52 } 53 54 // PrevPage goes back to the previous page if there is one. 55 func (stb *ScrollableTextBox) PrevPage() { 56 if stb.CurrentPage > 0 { 57 stb.CurrentPage-- 58 } 59 }