@@ -13,24 +13,19 @@ type ListItems interface {
1313 Len () int
1414 // Get accesses the underlying item.
1515 Get (i int ) any
16- // Show renders the item at the given zero-based index.
17- Show (i int ) ui.Text
18- }
19-
20- // StyleLiner is an optional interface that [ListItems] can implement.
21- type StyleLiner interface {
22- // StyleLine returns a "line styling" for item i, which gets applied to
23- // whole lines occupied by the item, including empty spaces.
24- StyleLine (i int ) ui.Styling
16+ // Show renders the item at the given zero-based index,
17+ // also returning the "area styling" for the item,
18+ // which is applied to the entire area occupied by the item in the listbox.
19+ Show (i int ) (ui.Text , ui.Styling )
2520}
2621
2722type stringItems []string
2823
2924// StringItems returns a [ListItems] backed up a slice of strings.
30- func StringItems (items ... string ) ListItems { return stringItems (items ) }
31- func (si stringItems ) Len () int { return len (si ) }
32- func (si stringItems ) Get (i int ) any { return si [i ] }
33- func (si stringItems ) Show (i int ) ui.Text { return ui .T (si [i ]) }
25+ func StringItems (items ... string ) ListItems { return stringItems (items ) }
26+ func (si stringItems ) Len () int { return len (si ) }
27+ func (si stringItems ) Get (i int ) any { return si [i ] }
28+ func (si stringItems ) Show (i int ) ( ui.Text , ui. Styling ) { return ui .T (si [i ]), ui . Nop }
3429
3530// ListBox shows a list of items and supports choosing one of them.
3631//
@@ -138,23 +133,22 @@ func (v *listBoxView) renderSingleColumn(width, height int) *term.Buffer {
138133 n := v .items .Len ()
139134 var i int
140135 for i = first ; i < n && len (lv .Lines ) < height ; i ++ {
141- text := v .items .Show (i )
142- lineStyling := ui .Nop
143- if styleLiner , ok := v .items .(StyleLiner ); ok {
144- lineStyling = styleLiner .StyleLine (i )
145- }
136+ text , areaStyling := v .items .Show (i )
146137 if i == v .selected {
147138 lv .DotAtLine = len (lv .Lines )
148- lineStyling = ui .Stylings (lineStyling , ui .Inverse )
139+ areaStyling = ui .Stylings (areaStyling , ui .Inverse )
149140 }
150141
151142 lines := text .SplitByRune ('\n' )
152143 if i == first {
153144 lines = lines [firstCrop :]
154145 }
155146 for _ , line := range lines {
147+ if len (lv .Lines ) == height {
148+ break
149+ }
156150 lv .Lines = append (lv .Lines , line )
157- lv .LineStylings = append (lv .LineStylings , lineStyling )
151+ lv .LineStylings = append (lv .LineStylings , areaStyling )
158152 }
159153 }
160154 if first == 0 && i == n && firstCrop == 0 && len (lv .Lines ) < height {
@@ -187,19 +181,15 @@ func (w *listBoxView) renderMultiColumn(width, height int) *term.Buffer {
187181 // Render the column starting from i.
188182 for j := i ; j < i + colHeight && j < n ; j ++ {
189183 last = j
190- text := items .Show (j )
191- lineStyling := ui .Nop
192- if styleLiner , ok := w .items .(StyleLiner ); ok {
193- lineStyling = styleLiner .StyleLine (i )
194- }
184+ text , areaStyling := items .Show (j )
195185 if j == selected {
196186 col .DotAtLine = len (col .Lines )
197- lineStyling = ui .Stylings (lineStyling , ui .Inverse )
187+ areaStyling = ui .Stylings (areaStyling , ui .Inverse )
198188 }
199189
200190 // TODO: Complain about multi-line items more loudly.
201191 col .Lines = append (col .Lines , text .SplitByRune ('\n' )[0 ])
202- col .LineStylings = append (col .LineStylings , lineStyling )
192+ col .LineStylings = append (col .LineStylings , areaStyling )
203193 }
204194
205195 colWidth := maxWidth (items , padding , i , i + colHeight )
0 commit comments