-
Notifications
You must be signed in to change notification settings - Fork 273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(list): implement GlobalIndex helper #574
Conversation
This commit introduces a new `filteredItem` field called `index` which stores the index of the filtered item in the unfiltered list. This allows to get at runtime the unfiltered list index for the selected (and possibly filtered) item. This is the only solution I found to use `SetItem` with a filtered list. The name `GlobalIndex` might not be ideal, I'm happy to change it to something else. (`UnfilteredIndex`?) Fixes: charmbracelet#550
03fbaa7
to
1fc1edf
Compare
Ah this is such a good catch! Thank you so much for working on this. I'm thinking it might make sense to integrate your change directly into I just wrote a test that showcases the behaviour you've added with this PR + the Let me know if you're cool with me adding this test to your PR. If you can think of a case where you need |
I'll want to refactor the test before merging so there are more conditions to satisfy. Right now it's more of a walkthrough on the behaviour of this change. If you have any suggestions to improve it as well, I'm all ears :) |
Thanks for the review @bashbunni ❤️
I explicitly added a new function/field because my understanding of
So if I want to get the visible position of the item, only E.g. (assuming the cursor is on the item:
It seems to me that the
But that might require a bigger codechange and introduce a breaking change, so I understand if this is not ideal. Alternatively, type Index struct {
Filtered int
Unfiltered int
}
func (m Model) Index() int {
filteredIndex := m.Paginator.Page*m.Paginator.PerPage + m.cursor
unfilteredIndex := filteredIndex
if m.filteredItems != nil && index < len(m.filteredItems) {
unfilteredIndex = m.filteredItems[index].index
}
return Index {
Filtered: filteredIndex,
Unfiltered: unfilteredIndex,
}
} But this would be even more disruptive to existing codebase.
Go ahead, and I'll have a look once you've done so :) Feel free to ping me! |
Thank you!! I will take another look when I have a moment. I do think this is a valuable fix here 🙏 |
Hi @bashbunni 👋 Let me know if / how I can help push this forward 🙏 |
After battling against bubble's incomplete interface, I've decided to store the list's index manually and do the logic myself. The indexes are computed each time the list change size (i.e. after executing a command) and used whenever changing an item is necessary. This prevent the list of items from being replaced during selecting all as described in #175 (comment) It removes the dependency on charmbracelet/bubbles#574. Fix #175
Could I ask for an update the godoc for It currently states the following, which implies that it operates across the unfiltered list:
|
// Index returns the index of the currently selected item as it is stored in the | ||
// filtered list of items. | ||
// Using this value with SetItem() might be incorrect, consider using | ||
// GlobalIndex() instead. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nobe4 thank you so much for bringing this to our attention and for your patience! |
This commit introduces a new
filteredItem
field calledindex
which stores the index of the filtered item in the unfiltered list. This allows to get at runtime the unfiltered list index for the selected (and possibly filtered) item.This is the only solution I found to use
SetItem
with a filtered list.The name
GlobalIndex
might not be ideal, I'm happy to change it to something else. (UnfilteredIndex
?)Fixes: #550
Example
Consider the following code:
Selecting with
" "
and"x"
behaves similarly before filtering. Upon filtering,"x"
behaves as described in #550 (i.e. wrongly) and" "
behaves correctly: it updates the correct item in place.