on main help sign in
refactor(ui): remove dirty tracking from LazyList
commit94de6bc884658ed35797ff0dbca1ed5571af3e14 authorAyman Bagabas <ayman.bagabas@gmail.com> dateTue 9 Dec 2025, 11:01 (1 year ago) parentf79c9ce
2 files changed+26−15
internal/ui/lazylist/item.go+22 internal/ui/lazylist/list.go+4−15
internal/ui/lazylist/item.go+22
@@ -6,3 +6,25 @@ type Item interface {
66 // width.
77 Render(width int) string
88}
9+
10+// Focusable represents an item that can gain or lose focus.
11+type Focusable interface {
12+ // Focus sets the focus state of the item.
13+ Focus()
14+
15+ // Blur removes the focus state of the item.
16+ Blur()
17+
18+ // Focused returns whether the item is focused.
19+ Focused() bool
20+}
21+
22+// Highlightable represents an item that can have a highlighted region.
23+type Highlightable interface {
24+ // SetHighlight sets the highlight region (startLine, startCol) to (endLine, endCol).
25+ // Use -1 for all values to clear highlighting.
26+ SetHighlight(startLine, startCol, endLine, endCol int)
27+
28+ // GetHighlight returns the current highlight region.
29+ GetHighlight() (startLine, startCol, endLine, endCol int)
30+}
internal/ui/lazylist/list.go+4−15
@@ -31,9 +31,6 @@ type List struct {
3131 // scrolled out of view (above the viewport).
3232 // It must always be >= 0.
3333 offsetLine int
34−
35− // Dirty tracking
36− dirtyItems map[int]struct{}
3734}
3835
3936// renderedItem holds the rendered content and height of an item.
@@ -47,7 +44,6 @@ func NewList(items ...Item) *List {
4744 l := new(List)
4845 l.items = items
4946 l.renderedItems = make(map[int]renderedItem)
50− l.dirtyItems = make(map[int]struct{})
5147 return l
5248}
5349
@@ -88,9 +84,7 @@ func (l *List) getItem(idx int) renderedItem {
8884 }
8985
9086 if item, ok := l.renderedItems[idx]; ok {
91− if _, dirty := l.dirtyItems[idx]; !dirty {
92− return item
93− }
87+ return item
9488 }
9589
9690 item := l.items[idx]
@@ -104,7 +98,6 @@ func (l *List) getItem(idx int) renderedItem {
10498 }
10599
106100 l.renderedItems[idx] = ri
107− delete(l.dirtyItems, idx)
108101
109102 return ri
110103}
@@ -301,13 +294,6 @@ func (l *List) PrependItems(items ...Item) {
301294 }
302295 l.renderedItems = newCache
303296
304− // Shift dirty items
305− newDirty := make(map[int]struct{})
306− for idx := range l.dirtyItems {
307− newDirty[idx+len(items)] = struct{}{}
308− }
309− l.dirtyItems = newDirty
310−
311297 // Keep view position relative to the content that was visible
312298 l.offsetIdx += len(items)
313299
@@ -325,6 +311,9 @@ func (l *List) AppendItems(items ...Item) {
325311// Focus sets the focus state of the list.
326312func (l *List) Focus() {
327313 l.focused = true
314+ if l.selectedIdx < 0 || l.selectedIdx > len(l.items)-1 {
315+ return
316+ }
328317}
329318
330319// Blur removes the focus state from the list.