Repository navigation
feat: Add Cursor() methods to Select and MultiSelect fields - #407
Conversation
|
+1 because I actually also have this use-case, more or less - but with a Some comments on the change: func (m *MultiSelect[T]) Hovered() T {
return m.options.val[m.cursor]
} |
|
@Siri-chan I agree, good idea. Plus, the lists can be filtered, so just having the cursor index might not be sufficient to know what option is hovered. Here's what I came up with that works for my use case, what do you think? // Hovered returns the value of the option under the cursor, and a bool
// indicating whether one was found. If there are no visible options, returns
// a zero-valued T and false.
func (m *MultiSelect[T]) Hovered() (T, bool) {
if len(m.filteredOptions) == 0 || m.cursor >= len(m.filteredOptions) {
var zero T
return zero, false
}
return m.filteredOptions[m.cursor].Value, true
} |
|
Seems fine by me - your branch works for my use-case and doesn't break any of the demo tapes as best I can tell |
|
After some more testing, it looks like there is a possible race condition here. Commands are evaluated in a separate goroutine from the one that handles messages, and the DescriptionFunc/TitleFunc/etc functions are called during command evaluation in the background, so they can't read anything that could be modified in another component's Update(). Not sure the best way to fix this. |
|
Thanks! |
Hi! This PR addresses a small limitation I ran into while building out a UI with this library. I was using a MultiSelect field to display a list of options, and wanted to use a Note to show additional details/context information for the option under the cursor. However, there was no way to know which option that was.
With this change, you can now write the following:
There is still the small issue of knowing when to invalidate the DescriptionFunc - I solved this by using the value of
Cursor()as a hash:This relies on implementation details however, so if this use case is common, something like it (but with a more idiomatic api) could be a good candidate for a built-in utility.