@@ -2,16 +2,21 @@ package list
7 "github.com/charmbracelet/bubbles/v2/help"
8 "github.com/charmbracelet/bubbles/v2/help"
8 "github.com/charmbracelet/bubbles/v2/key"
9 "github.com/charmbracelet/bubbles/v2/key"
9 "github.com/charmbracelet/bubbles/v2/spinner"
10 "github.com/charmbracelet/bubbles/v2/spinner"
11+ "github.com/charmbracelet/bubbles/v2/textinput"
10 tea "github.com/charmbracelet/bubbletea/v2"
12 tea "github.com/charmbracelet/bubbletea/v2"
11 "github.com/charmbracelet/lipgloss/v2"
13 "github.com/charmbracelet/lipgloss/v2"
12 "github.com/opencode-ai/opencode/internal/tui/components/anim"
14 "github.com/opencode-ai/opencode/internal/tui/components/anim"
13 "github.com/opencode-ai/opencode/internal/tui/layout"
15 "github.com/opencode-ai/opencode/internal/tui/layout"
16+ "github.com/opencode-ai/opencode/internal/tui/styles"
17+ "github.com/opencode-ai/opencode/internal/tui/theme"
14 "github.com/opencode-ai/opencode/internal/tui/util"
18 "github.com/opencode-ai/opencode/internal/tui/util"
19+ "github.com/sahilm/fuzzy"
17// Constants for special index values and defaults
22// Constants for special index values and defaults
@@ -43,6 +48,18 @@ type HasAnim interface {
43 Spinning() bool // Returns true if the item is currently animating
48 Spinning() bool // Returns true if the item is currently animating
51+// HasFilterValue interface allows items to provide a filter value for searching.
52+type HasFilterValue interface {
54+ FilterValue() string // Returns a string value used for filtering/searching
57+// HasMatchIndexes interface allows items to set matched character indexes.
58+type HasMatchIndexes interface {
60+ MatchIndexes([]int) // Sets the indexes of matched characters in the item's content
46// renderedItem represents a cached rendered item with its position and content.
63// renderedItem represents a cached rendered item with its position and content.
47type renderedItem struct {
64type renderedItem struct {
48 lines []string // The rendered lines of text for this item
65 lines []string // The rendered lines of text for this item
@@ -105,10 +122,15 @@ type model struct {
105 renderState *renderState // Rendering cache and state
122 renderState *renderState // Rendering cache and state
106 selectionState selectionState // Item selection state
123 selectionState selectionState // Item selection state
107 help help.Model // Help system for keyboard shortcuts
124 help help.Model // Help system for keyboard shortcuts
108− keymap KeyMap // Key bindings for navigation
125+ keyMap KeyMap // Key bindings for navigation
109− items []util.Model // The actual list items
126+ allItems []util.Model // The actual list items
110 gapSize int // Number of empty lines between items
127 gapSize int // Number of empty lines between items
111 padding []int // Padding around the list content
128 padding []int // Padding around the list content
130+ filterable bool // Whether items can be filtered
131+ filteredItems []util.Model // Filtered items based on current search
132+ input textinput.Model // Input field for filtering items
133+ currentSearch string // Current search term for filtering
114// listOptions is a function type for configuring list options.
136// listOptions is a function type for configuring list options.
@@ -117,7 +139,7 @@ type listOptions func(*model)
117// WithKeyMap sets custom key bindings for the list.
139// WithKeyMap sets custom key bindings for the list.
118func WithKeyMap(k KeyMap) listOptions {
140func WithKeyMap(k KeyMap) listOptions {
119 return func(m *model) {
141 return func(m *model) {
120− m.keymap = k
142+ m.keyMap = k
@@ -147,7 +169,15 @@ func WithPadding(padding ...int) listOptions {
147// WithItems sets the initial items for the list.
169// WithItems sets the initial items for the list.
148func WithItems(items []util.Model) listOptions {
170func WithItems(items []util.Model) listOptions {
149 return func(m *model) {
171 return func(m *model) {
150− m.items = items
172+ m.allItems = items
173+ m.filteredItems = items // Initially, all items are visible
177+// WithFilterable enables filtering of items based on their FilterValue.
178+func WithFilterable(filterable bool) listOptions {
179+ return func(m *model) {
180+ m.filterable = filterable
@@ -157,8 +187,9 @@ func WithItems(items []util.Model) listOptions {
157func New(opts ...listOptions) ListModel {
187func New(opts ...listOptions) ListModel {
158 m := &model{
188 m := &model{
159 help: help.New(),
189 help: help.New(),
160− keymap: DefaultKeymap(),
190+ keyMap: DefaultKeyMap(),
191+ allItems: []util.Model{},
161− items: []util.Model{},
192+ filteredItems: []util.Model{},
162 renderState: newRenderState(),
193 renderState: newRenderState(),
163 gapSize: DefaultGapSize,
194 gapSize: DefaultGapSize,
164 padding: []int{},
195 padding: []int{},
@@ -167,13 +198,25 @@ func New(opts ...listOptions) ListModel {
167 for _, opt := range opts {
198 for _, opt := range opts {
203+ ti := textinput.New()
204+ ti.Placeholder = "Type to filter..."
205+ ti.SetVirtualCursor(false)
209+ // disable j,k movements
210+ m.keyMap.NDown.SetEnabled(false)
211+ m.keyMap.NUp.SetEnabled(false)
173// Init initializes the list component and sets up the initial items.
216// Init initializes the list component and sets up the initial items.
174// This is called automatically by the Bubble Tea framework.
217// This is called automatically by the Bubble Tea framework.
175func (m *model) Init() tea.Cmd {
218func (m *model) Init() tea.Cmd {
176− return m.SetItems(m.items)
219+ return m.SetItems(m.filteredItems)
179// Update handles incoming messages and updates the list state accordingly.
222// Update handles incoming messages and updates the list state accordingly.
@@ -186,34 +229,78 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
186 case anim.ColorCycleMsg, anim.StepCharsMsg, spinner.TickMsg:
229 case anim.ColorCycleMsg, anim.StepCharsMsg, spinner.TickMsg:
187 return m.handleAnimationMsg(msg)
230 return m.handleAnimationMsg(msg)
190− if m.selectionState.isValidIndex(len(m.items)) {
232+ if m.selectionState.isValidIndex(len(m.filteredItems)) {
191 return m.updateSelectedItem(msg)
233 return m.updateSelectedItem(msg)
194 return m, nil
236 return m, nil
239+// View renders the list to a string for display.
240+// Returns empty string if the list has no dimensions.
241+// Triggers re-rendering if needed before returning content.
242+func (m *model) View() tea.View {
243+ if m.viewState.height == 0 || m.viewState.width == 0 {
244+ return tea.NewView("") // No content to display
246+ if m.renderState.needsRerender {
250+ content := lipgloss.NewStyle().
251+ Padding(m.padding...).
252+ Height(m.viewState.height).
253+ Render(m.viewState.content)
256+ content = lipgloss.JoinVertical(
258+ m.inputStyle().Render(m.input.View()),
262+ view := tea.NewView(content)
264+ view.SetCursor(m.input.Cursor())
197// handleKeyPress processes keyboard input for list navigation.
269// handleKeyPress processes keyboard input for list navigation.
198// Supports scrolling, item selection, and navigation to top/bottom.
270// Supports scrolling, item selection, and navigation to top/bottom.
199func (m *model) handleKeyPress(msg tea.KeyPressMsg) (tea.Model, tea.Cmd) {
271func (m *model) handleKeyPress(msg tea.KeyPressMsg) (tea.Model, tea.Cmd) {
201− case key.Matches(msg, m.keymap.Down) || key.Matches(msg, m.keymap.NDown):
273+ case key.Matches(msg, m.keyMap.Down) || key.Matches(msg, m.keyMap.NDown):
202 m.scrollDown(1)
274 m.scrollDown(1)
203− case key.Matches(msg, m.keymap.Up) || key.Matches(msg, m.keymap.NUp):
275+ case key.Matches(msg, m.keyMap.Up) || key.Matches(msg, m.keyMap.NUp):
204 m.scrollUp(1)
276 m.scrollUp(1)
205− case key.Matches(msg, m.keymap.DownOneItem):
277+ case key.Matches(msg, m.keyMap.DownOneItem):
206 return m, m.selectNextItem()
278 return m, m.selectNextItem()
207− case key.Matches(msg, m.keymap.UpOneItem):
279+ case key.Matches(msg, m.keyMap.UpOneItem):
208 return m, m.selectPreviousItem()
280 return m, m.selectPreviousItem()
209− case key.Matches(msg, m.keymap.HalfPageDown):
281+ case key.Matches(msg, m.keyMap.HalfPageDown):
210 m.scrollDown(m.listHeight() / 2)
282 m.scrollDown(m.listHeight() / 2)
211− case key.Matches(msg, m.keymap.HalfPageUp):
283+ case key.Matches(msg, m.keyMap.HalfPageUp):
212 m.scrollUp(m.listHeight() / 2)
284 m.scrollUp(m.listHeight() / 2)
213− case key.Matches(msg, m.keymap.Home):
285+ case key.Matches(msg, m.keyMap.Home):
214 return m, m.goToTop()
286 return m, m.goToTop()
215− case key.Matches(msg, m.keymap.End):
287+ case key.Matches(msg, m.keyMap.End):
216 return m, m.goToBottom()
288 return m, m.goToBottom()
291+ return m, nil // Ignore other keys if not filterable
294+ u, cmd := m.input.Update(msg)
296+ cmds = append(cmds, cmd)
297+ if m.currentSearch != m.input.Value() {
298+ cmd = m.filter(m.input.Value())
299+ cmds = append(cmds, cmd)
301+ m.currentSearch = m.input.Value()
302+ return m, tea.Batch(cmds...)
218 return m, nil
305 return m, nil
@@ -222,7 +309,7 @@ func (m *model) handleKeyPress(msg tea.KeyPressMsg) (tea.Model, tea.Cmd) {
222// Only items implementing HasAnim and currently spinning receive these messages.
309// Only items implementing HasAnim and currently spinning receive these messages.
223func (m *model) handleAnimationMsg(msg tea.Msg) (tea.Model, tea.Cmd) {
310func (m *model) handleAnimationMsg(msg tea.Msg) (tea.Model, tea.Cmd) {
224 var cmds []tea.Cmd
311 var cmds []tea.Cmd
225− for inx, item := range m.items {
312+ for inx, item := range m.filteredItems {
226 if i, ok := item.(HasAnim); ok && i.Spinning() {
313 if i, ok := item.(HasAnim); ok && i.Spinning() {
227 updated, cmd := i.Update(msg)
314 updated, cmd := i.Update(msg)
228 cmds = append(cmds, cmd)
315 cmds = append(cmds, cmd)
@@ -238,7 +325,7 @@ func (m *model) handleAnimationMsg(msg tea.Msg) (tea.Model, tea.Cmd) {
238// This allows the selected item to handle its own input and state changes.
325// This allows the selected item to handle its own input and state changes.
239func (m *model) updateSelectedItem(msg tea.Msg) (tea.Model, tea.Cmd) {
326func (m *model) updateSelectedItem(msg tea.Msg) (tea.Model, tea.Cmd) {
240 var cmds []tea.Cmd
327 var cmds []tea.Cmd
241− u, cmd := m.items[m.selectionState.selectedIndex].Update(msg)
328+ u, cmd := m.filteredItems[m.selectionState.selectedIndex].Update(msg)
242 cmds = append(cmds, cmd)
329 cmds = append(cmds, cmd)
243 if updated, ok := u.(util.Model); ok {
330 if updated, ok := u.(util.Model); ok {
244 m.UpdateItem(m.selectionState.selectedIndex, updated)
331 m.UpdateItem(m.selectionState.selectedIndex, updated)
@@ -266,27 +353,9 @@ func (m *model) scrollUp(amount int) {
269−// View renders the list to a string for display.
270−// Returns empty string if the list has no dimensions.
271−// Triggers re-rendering if needed before returning content.
272−func (m *model) View() tea.View {
273− if m.viewState.height == 0 || m.viewState.width == 0 {
274− return tea.NewView("") // No content to display
276− if m.renderState.needsRerender {
280− lipgloss.NewStyle().
281− Padding(m.padding...).
282− Height(m.viewState.height).
283− Render(m.viewState.content),
287// Items returns a copy of all items in the list.
356// Items returns a copy of all items in the list.
288func (m *model) Items() []util.Model {
357func (m *model) Items() []util.Model {
289− return m.items
358+ return m.filteredItems
292// renderVisible determines which rendering strategy to use and triggers rendering.
361// renderVisible determines which rendering strategy to use and triggers rendering.
@@ -306,12 +375,12 @@ func (m *model) renderVisibleForward() {
306 model: m,
375 model: m,
307 start: 0,
376 start: 0,
308 cutoff: m.viewState.offset + m.listHeight(),
377 cutoff: m.viewState.offset + m.listHeight(),
309− items: m.items,
378+ items: m.filteredItems,
310 realIdx: m.renderState.lastIndex,
379 realIdx: m.renderState.lastIndex,
313 if m.renderState.lastIndex > NotRendered {
382 if m.renderState.lastIndex > NotRendered {
314− renderer.items = m.items[m.renderState.lastIndex+1:]
383+ renderer.items = m.filteredItems[m.renderState.lastIndex+1:]
315 renderer.start = len(m.renderState.lines)
384 renderer.start = len(m.renderState.lines)
@@ -326,16 +395,16 @@ func (m *model) renderVisibleReverse() {
326 model: m,
395 model: m,
327 start: 0,
396 start: 0,
328 cutoff: m.viewState.offset + m.listHeight(),
397 cutoff: m.viewState.offset + m.listHeight(),
329− items: m.items,
398+ items: m.filteredItems,
330 realIdx: m.renderState.lastIndex,
399 realIdx: m.renderState.lastIndex,
333 if m.renderState.lastIndex > NotRendered {
402 if m.renderState.lastIndex > NotRendered {
334− renderer.items = m.items[:m.renderState.lastIndex]
403+ renderer.items = m.filteredItems[:m.renderState.lastIndex]
335 renderer.start = len(m.renderState.lines)
404 renderer.start = len(m.renderState.lines)
337− m.renderState.lastIndex = len(m.items)
406+ m.renderState.lastIndex = len(m.filteredItems)
338− renderer.realIdx = len(m.items)
407+ renderer.realIdx = len(m.filteredItems)
341 renderer.render()
410 renderer.render()
@@ -389,7 +458,7 @@ func (r *forwardRenderer) render() {
391 itemLines := r.getOrRenderItem(item)
460 itemLines := r.getOrRenderItem(item)
392− if r.realIdx == len(r.model.items)-1 {
461+ if r.realIdx == len(r.model.filteredItems)-1 {
393 r.model.renderState.finalHeight = max(0, r.start+len(itemLines)-r.model.listHeight())
462 r.model.renderState.finalHeight = max(0, r.start+len(itemLines)-r.model.listHeight())
@@ -485,7 +554,7 @@ func (m *model) selectPreviousItem() tea.Cmd {
485// selectNextItem moves selection to the next item in the list.
554// selectNextItem moves selection to the next item in the list.
486// Handles focus management and ensures the selected item remains visible.
555// Handles focus management and ensures the selected item remains visible.
487func (m *model) selectNextItem() tea.Cmd {
556func (m *model) selectNextItem() tea.Cmd {
488− if m.selectionState.selectedIndex >= len(m.items)-1 || m.selectionState.selectedIndex < 0 {
557+ if m.selectionState.selectedIndex >= len(m.filteredItems)-1 || m.selectionState.selectedIndex < 0 {
489 return nil
558 return nil
@@ -543,7 +612,7 @@ func (m *model) ensureVisibleForward(cachedItem renderedItem) {
543// Handles both large items (taller than viewport) and normal items.
612// Handles both large items (taller than viewport) and normal items.
544func (m *model) ensureVisibleReverse(cachedItem renderedItem) {
613func (m *model) ensureVisibleReverse(cachedItem renderedItem) {
545 if cachedItem.height >= m.listHeight() {
614 if cachedItem.height >= m.listHeight() {
546− if m.selectionState.selectedIndex < len(m.items)-1 {
615+ if m.selectionState.selectedIndex < len(m.filteredItems)-1 {
547 changeNeeded := m.viewState.offset - (cachedItem.start + cachedItem.height - m.listHeight())
616 changeNeeded := m.viewState.offset - (cachedItem.start + cachedItem.height - m.listHeight())
548 m.decreaseOffset(changeNeeded)
617 m.decreaseOffset(changeNeeded)
@@ -567,7 +636,7 @@ func (m *model) ensureVisibleReverse(cachedItem renderedItem) {
567func (m *model) goToBottom() tea.Cmd {
636func (m *model) goToBottom() tea.Cmd {
568 cmds := []tea.Cmd{m.blurSelected()}
637 cmds := []tea.Cmd{m.blurSelected()}
569 m.viewState.reverse = true
638 m.viewState.reverse = true
570− m.selectionState.selectedIndex = len(m.items) - 1
639+ m.selectionState.selectedIndex = len(m.filteredItems) - 1
571 cmds = append(cmds, m.focusSelected())
640 cmds = append(cmds, m.focusSelected())
572 m.ResetView()
641 m.ResetView()
573 return tea.Batch(cmds...)
642 return tea.Batch(cmds...)
@@ -578,7 +647,7 @@ func (m *model) goToBottom() tea.Cmd {
578func (m *model) goToTop() tea.Cmd {
647func (m *model) goToTop() tea.Cmd {
579 cmds := []tea.Cmd{m.blurSelected()}
648 cmds := []tea.Cmd{m.blurSelected()}
580 m.viewState.reverse = false
649 m.viewState.reverse = false
581− if len(m.items) > 0 {
650+ if len(m.filteredItems) > 0 {
582 m.selectionState.selectedIndex = 0
651 m.selectionState.selectedIndex = 0
584 cmds = append(cmds, m.focusSelected())
653 cmds = append(cmds, m.focusSelected())
@@ -596,10 +665,10 @@ func (m *model) ResetView() {
596// focusSelected gives focus to the currently selected item if it supports focus.
665// focusSelected gives focus to the currently selected item if it supports focus.
597// Triggers a re-render of the item to show its focused state.
666// Triggers a re-render of the item to show its focused state.
598func (m *model) focusSelected() tea.Cmd {
667func (m *model) focusSelected() tea.Cmd {
599− if !m.selectionState.isValidIndex(len(m.items)) {
668+ if !m.selectionState.isValidIndex(len(m.filteredItems)) {
600 return nil
669 return nil
602− if i, ok := m.items[m.selectionState.selectedIndex].(layout.Focusable); ok {
671+ if i, ok := m.filteredItems[m.selectionState.selectedIndex].(layout.Focusable); ok {
603 cmd := i.Focus()
672 cmd := i.Focus()
604 m.rerenderItem(m.selectionState.selectedIndex)
673 m.rerenderItem(m.selectionState.selectedIndex)
605 return cmd
674 return cmd
@@ -610,10 +679,10 @@ func (m *model) focusSelected() tea.Cmd {
610// blurSelected removes focus from the currently selected item if it supports focus.
679// blurSelected removes focus from the currently selected item if it supports focus.
611// Triggers a re-render of the item to show its unfocused state.
680// Triggers a re-render of the item to show its unfocused state.
612func (m *model) blurSelected() tea.Cmd {
681func (m *model) blurSelected() tea.Cmd {
613− if !m.selectionState.isValidIndex(len(m.items)) {
682+ if !m.selectionState.isValidIndex(len(m.filteredItems)) {
614 return nil
683 return nil
616− if i, ok := m.items[m.selectionState.selectedIndex].(layout.Focusable); ok {
685+ if i, ok := m.filteredItems[m.selectionState.selectedIndex].(layout.Focusable); ok {
617 cmd := i.Blur()
686 cmd := i.Blur()
618 m.rerenderItem(m.selectionState.selectedIndex)
687 m.rerenderItem(m.selectionState.selectedIndex)
619 return cmd
688 return cmd
@@ -625,7 +694,7 @@ func (m *model) blurSelected() tea.Cmd {
625// This is called when an item's state changes (e.g., focus/blur) and needs to be re-displayed.
694// This is called when an item's state changes (e.g., focus/blur) and needs to be re-displayed.
626// It efficiently updates only the changed item and adjusts positions of subsequent items if needed.
695// It efficiently updates only the changed item and adjusts positions of subsequent items if needed.
627func (m *model) rerenderItem(inx int) {
696func (m *model) rerenderItem(inx int) {
628− if inx < 0 || inx >= len(m.items) || len(m.renderState.lines) == 0 {
697+ if inx < 0 || inx >= len(m.filteredItems) || len(m.renderState.lines) == 0 {
@@ -634,7 +703,7 @@ func (m *model) rerenderItem(inx int) {
637− rerenderedLines := m.getItemLines(m.items[inx])
706+ rerenderedLines := m.getItemLines(m.filteredItems[inx])
638 if slices.Equal(cachedItem.lines, rerenderedLines) {
707 if slices.Equal(cachedItem.lines, rerenderedLines) {
@@ -687,7 +756,7 @@ func (m *model) updateItemPositions(inx int, cachedItem renderedItem, newHeight
690− if inx == len(m.items)-1 {
759+ if inx == len(m.filteredItems)-1 {
691 m.renderState.finalHeight = max(0, cachedItem.start+newHeight-m.listHeight())
760 m.renderState.finalHeight = max(0, cachedItem.start+newHeight-m.listHeight())
@@ -701,7 +770,7 @@ func (m *model) updateItemPositions(inx int, cachedItem renderedItem, newHeight
702// updatePositionsForward updates positions for items after the changed item in forward mode.
771// updatePositionsForward updates positions for items after the changed item in forward mode.
703func (m *model) updatePositionsForward(inx int, currentStart int) {
772func (m *model) updatePositionsForward(inx int, currentStart int) {
704− for i := inx + 1; i < len(m.items); i++ {
773+ for i := inx + 1; i < len(m.filteredItems); i++ {
705 if existing, ok := m.renderState.items[i]; ok {
774 if existing, ok := m.renderState.items[i]; ok {
706 existing.start = currentStart
775 existing.start = currentStart
707 currentStart += existing.height
776 currentStart += existing.height
@@ -766,12 +835,12 @@ func (m *model) decreaseOffset(n int) {
766// UpdateItem replaces an item at the specified index with a new item.
835// UpdateItem replaces an item at the specified index with a new item.
767// Handles focus management and triggers re-rendering as needed.
836// Handles focus management and triggers re-rendering as needed.
768func (m *model) UpdateItem(inx int, item util.Model) {
837func (m *model) UpdateItem(inx int, item util.Model) {
769− if inx < 0 || inx >= len(m.items) {
838+ if inx < 0 || inx >= len(m.filteredItems) {
772− m.items[inx] = item
841+ m.filteredItems[inx] = item
773 if m.selectionState.selectedIndex == inx {
842 if m.selectionState.selectedIndex == inx {
774− if i, ok := m.items[m.selectionState.selectedIndex].(layout.Focusable); ok {
843+ if i, ok := m.filteredItems[m.selectionState.selectedIndex].(layout.Focusable); ok {
775 i.Focus()
844 i.Focus()
@@ -788,6 +857,10 @@ func (m *model) GetSize() (int, int) {
788// SetSize updates the list dimensions and triggers a complete re-render.
857// SetSize updates the list dimensions and triggers a complete re-render.
789// Also updates the size of all items that support sizing.
858// Also updates the size of all items that support sizing.
790func (m *model) SetSize(width int, height int) tea.Cmd {
859func (m *model) SetSize(width int, height int) tea.Cmd {
861+ height -= 2 // adjust for input field height and border
791 if m.viewState.width == width && m.viewState.height == height {
864 if m.viewState.width == width && m.viewState.height == height {
792 return nil
865 return nil
@@ -797,11 +870,14 @@ func (m *model) SetSize(width int, height int) tea.Cmd {
798 m.viewState.width = width
871 m.viewState.width = width
799 m.ResetView()
872 m.ResetView()
874+ m.input.SetWidth(m.getItemWidth() - 3)
800 return m.setAllItemsSize()
876 return m.setAllItemsSize()
803−// getItemSize calculates the available width for items, accounting for padding.
879+// getItemWidth calculates the available width for items, accounting for padding.
804−func (m *model) getItemSize() int {
880+func (m *model) getItemWidth() int {
805 width := m.viewState.width
881 width := m.viewState.width
806 switch len(m.padding) {
882 switch len(m.padding) {
@@ -816,11 +892,11 @@ func (m *model) getItemSize() int {
817// setItemSize updates the size of a specific item if it supports sizing.
893// setItemSize updates the size of a specific item if it supports sizing.
818func (m *model) setItemSize(inx int) tea.Cmd {
894func (m *model) setItemSize(inx int) tea.Cmd {
819− if inx < 0 || inx >= len(m.items) {
895+ if inx < 0 || inx >= len(m.filteredItems) {
820 return nil
896 return nil
822− if i, ok := m.items[inx].(layout.Sizeable); ok {
898+ if i, ok := m.filteredItems[inx].(layout.Sizeable); ok {
823− return i.SetSize(m.getItemSize(), 0)
899+ return i.SetSize(m.getItemWidth(), 0)
825 return nil
901 return nil
@@ -828,7 +904,7 @@ func (m *model) setItemSize(inx int) tea.Cmd {
828// setAllItemsSize updates the size of all items that support sizing.
904// setAllItemsSize updates the size of all items that support sizing.
829func (m *model) setAllItemsSize() tea.Cmd {
905func (m *model) setAllItemsSize() tea.Cmd {
830 var cmds []tea.Cmd
906 var cmds []tea.Cmd
831− for i := range m.items {
907+ for i := range m.filteredItems {
832 if cmd := m.setItemSize(i); cmd != nil {
908 if cmd := m.setItemSize(i); cmd != nil {
833 cmds = append(cmds, cmd)
909 cmds = append(cmds, cmd)
@@ -856,8 +932,9 @@ func (m *model) AppendItem(item util.Model) tea.Cmd {
856 cmds := []tea.Cmd{
932 cmds := []tea.Cmd{
857 item.Init(),
933 item.Init(),
859− m.items = append(m.items, item)
935+ m.allItems = append(m.allItems, item)
936+ m.filteredItems = m.allItems
860− cmds = append(cmds, m.setItemSize(len(m.items)-1))
937+ cmds = append(cmds, m.setItemSize(len(m.filteredItems)-1))
861 cmds = append(cmds, m.goToBottom())
938 cmds = append(cmds, m.goToBottom())
862 m.renderState.needsRerender = true
939 m.renderState.needsRerender = true
863 return tea.Batch(cmds...)
940 return tea.Batch(cmds...)
@@ -866,11 +943,12 @@ func (m *model) AppendItem(item util.Model) tea.Cmd {
866// DeleteItem removes an item at the specified index.
943// DeleteItem removes an item at the specified index.
867// Adjusts selection if necessary and triggers a complete re-render.
944// Adjusts selection if necessary and triggers a complete re-render.
868func (m *model) DeleteItem(i int) {
945func (m *model) DeleteItem(i int) {
869− if i < 0 || i >= len(m.items) {
946+ if i < 0 || i >= len(m.filteredItems) {
872− m.items = slices.Delete(m.items, i, i+1)
949+ m.allItems = slices.Delete(m.allItems, i, i+1)
873 delete(m.renderState.items, i)
950 delete(m.renderState.items, i)
951+ m.filteredItems = m.allItems
875 if m.selectionState.selectedIndex == i && m.selectionState.selectedIndex > 0 {
953 if m.selectionState.selectedIndex == i && m.selectionState.selectedIndex > 0 {
876 m.selectionState.selectedIndex--
954 m.selectionState.selectedIndex--
@@ -886,7 +964,8 @@ func (m *model) DeleteItem(i int) {
886// Adjusts cached positions and selection index, then switches to forward mode.
964// Adjusts cached positions and selection index, then switches to forward mode.
887func (m *model) PrependItem(item util.Model) tea.Cmd {
965func (m *model) PrependItem(item util.Model) tea.Cmd {
888 cmds := []tea.Cmd{item.Init()}
966 cmds := []tea.Cmd{item.Init()}
889− m.items = append([]util.Model{item}, m.items...)
967+ m.allItems = append([]util.Model{item}, m.allItems...)
968+ m.filteredItems = m.allItems
891 // Shift all cached item indices by 1
970 // Shift all cached item indices by 1
892 newItems := make(map[int]renderedItem, len(m.renderState.items))
971 newItems := make(map[int]renderedItem, len(m.renderState.items))
@@ -917,16 +996,78 @@ func (m *model) setReverse(reverse bool) {
917// SetItems replaces all items in the list with a new set.
996// SetItems replaces all items in the list with a new set.
918// Initializes all items, sets their sizes, and establishes initial selection.
997// Initializes all items, sets their sizes, and establishes initial selection.
919func (m *model) SetItems(items []util.Model) tea.Cmd {
998func (m *model) SetItems(items []util.Model) tea.Cmd {
920− m.items = items
999+ m.allItems = items
1000+ m.filteredItems = items
921 cmds := []tea.Cmd{m.setAllItemsSize()}
1001 cmds := []tea.Cmd{m.setAllItemsSize()}
923− for _, item := range m.items {
1003+ for _, item := range m.filteredItems {
924 cmds = append(cmds, item.Init())
1004 cmds = append(cmds, item.Init())
927− if len(m.items) > 0 {
1007+ if len(m.filteredItems) > 0 {
1008+ if m.viewState.reverse {
1009+ m.selectionState.selectedIndex = len(m.filteredItems) - 1
1011+ m.selectionState.selectedIndex = 0
1013+ if cmd := m.focusSelected(); cmd != nil {
1014+ cmds = append(cmds, cmd)
1017+ m.selectionState.selectedIndex = NoSelection
1021+ return tea.Batch(cmds...)
1024+func (c *model) inputStyle() lipgloss.Style {
1025+ t := theme.CurrentTheme()
1026+ return styles.BaseStyle().
1027+ BorderStyle(lipgloss.NormalBorder()).
1028+ BorderForeground(t.TextMuted()).
1029+ BorderBackground(t.Background()).
1033+func (m *model) filter(search string) tea.Cmd {
1035+ search = strings.TrimSpace(search)
1036+ search = strings.ToLower(search)
1037+ for _, item := range m.allItems {
1038+ if i, ok := item.(layout.Focusable); ok {
1039+ cmds = append(cmds, i.Blur())
1041+ if i, ok := item.(HasMatchIndexes); ok {
1042+ i.MatchIndexes(make([]int, 0))
1046+ cmds = append(cmds, m.SetItems(m.allItems)) // Reset to all items if search is empty
1047+ return tea.Batch(cmds...)
1049+ words := make([]string, 0, len(m.allItems))
1050+ for _, cmd := range m.allItems {
1051+ if f, ok := cmd.(HasFilterValue); ok {
1052+ words = append(words, strings.ToLower(f.FilterValue()))
1054+ words = append(words, strings.ToLower(""))
1057+ matches := fuzzy.Find(search, words)
1059+ filteredItems := make([]util.Model, 0, len(matches))
1060+ for _, match := range matches {
1061+ item := m.allItems[match.Index]
1062+ if i, ok := item.(HasMatchIndexes); ok {
1063+ i.MatchIndexes(match.MatchedIndexes)
1065+ filteredItems = append(filteredItems, item)
1067+ m.filteredItems = filteredItems
1068+ if len(filteredItems) > 0 {
928 if m.viewState.reverse {
1069 if m.viewState.reverse {
929− m.selectionState.selectedIndex = len(m.items) - 1
1070+ m.selectionState.selectedIndex = len(filteredItems) - 1
930 } else {
1071 } else {
931 m.selectionState.selectedIndex = 0
1072 m.selectionState.selectedIndex = 0