src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/cli/tk/listbox_state.go (about) 1 package tk 2 3 import ( 4 "fmt" 5 6 "src.elv.sh/pkg/ui" 7 ) 8 9 // ListBoxState keeps the mutable state ListBox. 10 type ListBoxState struct { 11 Items Items 12 Selected int 13 // The first element to show. Used when rendering and adjusted accordingly 14 // when the terminal size changes or the user has scrolled. 15 First int 16 // Height of the listbox, excluding horizontal scrollbar when using the 17 // horizontal layout. Stored in the state for commands to move the cursor by 18 // page (for vertical layout) or column (for horizontal layout). 19 ContentHeight int 20 } 21 22 // Items is an interface for accessing multiple items. 23 type Items interface { 24 // Show renders the item at the given zero-based index. 25 Show(i int) ui.Text 26 // Len returns the number of items. 27 Len() int 28 } 29 30 // TestItems is an implementation of Items useful for testing. 31 type TestItems struct { 32 Prefix string 33 Style ui.Styling 34 NItems int 35 } 36 37 // Show returns a plain text consisting of the prefix and i. If the prefix is 38 // empty, it defaults to "item ". 39 func (it TestItems) Show(i int) ui.Text { 40 prefix := it.Prefix 41 if prefix == "" { 42 prefix = "item " 43 } 44 return ui.T(fmt.Sprintf("%s%d", prefix, i), it.Style) 45 } 46 47 // Len returns it.NItems. 48 func (it TestItems) Len() int { 49 return it.NItems 50 }