Skip to content

Commit

Permalink
fix(filter): fix selection while filtering (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
sammcj authored Dec 29, 2024
1 parent 101ba38 commit cb6d882
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 12 deletions.
31 changes: 22 additions & 9 deletions app_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,25 +275,38 @@ func (m *AppModel) handleSpaceKey() (tea.Model, tea.Cmd) {
logging.DebugLogger.Printf("Toggling selection for model: %s (before: %v)\n", item.Name, item.Selected)
item.Selected = !item.Selected

// Update the item in the list's items
items := m.list.Items()
for i, listItem := range items {
// Update both the filtered and unfiltered lists
filteredItems := m.list.Items()
for i, listItem := range filteredItems {
if model, ok := listItem.(Model); ok && model.Name == item.Name {
items[i] = item
break
filteredItems[i] = item
}
}

m.list.SetItems(items)

// Find the index of the actual item in the full list and update it
// Always update the main model list
for i, model := range m.models {
if model.Name == item.Name {
m.models[i] = item
break
}
}

// Update the items in the list
m.list.SetItems(filteredItems)

// If filtering is active, force a refresh of the view
if m.list.FilterState() == list.Filtering || m.list.FilterState() == list.FilterApplied {
// Store current cursor position
currentIndex := m.list.Index()

// Force a view refresh by temporarily clearing and reapplying items
tempItems := m.list.Items()
m.list.SetItems(nil)
m.list.SetItems(tempItems)

// Restore cursor position
m.list.Select(currentIndex)
}

logging.DebugLogger.Printf("Toggled selection for model: %s (after: %v)\n", item.Name, item.Selected)
}
return m, nil
Expand Down
26 changes: 23 additions & 3 deletions item_delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,17 @@ func (d itemDelegate) Update(msg tea.Msg, m *list.Model) tea.Cmd {
if ok {
logging.DebugLogger.Printf("Delegate toggling selection for model: %s (before: %v)\n", i.Name, i.Selected)
i.Selected = !i.Selected

// Update the item in the filtered list
m.SetItem(m.Index(), i)
// Update the main model list
d.appModel.models[m.Index()] = i

// Update the main model list by name match
for idx, model := range d.appModel.models {
if model.Name == i.Name {
d.appModel.models[idx] = i
break
}
}
logging.DebugLogger.Printf("Updated main model list for model: %s (after: %v)\n", i.Name, i.Selected)
}
return nil
Expand Down Expand Up @@ -79,7 +87,19 @@ func (d itemDelegate) Render(w io.Writer, m list.Model, index int, item list.Ite
idStyle = idStyle.Foreground(lipgloss.Color("225")).BorderLeft(true).PaddingLeft(-2).PaddingRight(-2)
}

if model.Selected {
// Check if the model is selected in both filtered and unfiltered states
isSelected := model.Selected
if d.appModel.list.FilterState() == list.Filtering || d.appModel.list.FilterState() == list.FilterApplied {
// When filtering, also check the main models list to ensure selection state is accurate
for _, m := range d.appModel.models {
if m.Name == model.Name && m.Selected {
isSelected = true
break
}
}
}

if isSelected {
// de-indent to allow for selection border
selectedStyle := lipgloss.NewStyle().Background(lipgloss.Color("92")).Bold(true).Italic(true)
nameStyle = nameStyle.Inherit(selectedStyle)
Expand Down

0 comments on commit cb6d882

Please sign in to comment.