-
Notifications
You must be signed in to change notification settings - Fork 447
gum filter: add flag to return selected indexes instead of values #986
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
base: main
Are you sure you want to change the base?
Changes from all commits
1176822
f5d4105
650ccd5
30093a8
9ef3f9c
75e8691
471a07b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -5,6 +5,7 @@ import ( | |||||||||||||||||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||||||||||||||||
| "os" | ||||||||||||||||||||||||||||||||||||
| "slices" | ||||||||||||||||||||||||||||||||||||
| "strconv" | ||||||||||||||||||||||||||||||||||||
| "strings" | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| "github.com/charmbracelet/bubbles/help" | ||||||||||||||||||||||||||||||||||||
|
|
@@ -84,7 +85,14 @@ func (o Options) Run() error { | |||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| if o.SelectIfOne && len(matches) == 1 { | ||||||||||||||||||||||||||||||||||||
| tty.Println(matches[0].Str) | ||||||||||||||||||||||||||||||||||||
| if o.OutputIndexes { | ||||||||||||||||||||||||||||||||||||
| idx := o.findIndex(matches[0].Str, filteringChoices) | ||||||||||||||||||||||||||||||||||||
| if idx >= 0 { | ||||||||||||||||||||||||||||||||||||
| tty.Println(strconv.Itoa(idx)) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||
| tty.Println(matches[0].Str) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
@@ -157,18 +165,47 @@ func (o Options) Run() error { | |||||||||||||||||||||||||||||||||||
| // than 1 or if flag --no-limit is passed, hence there is | ||||||||||||||||||||||||||||||||||||
| // no need to further checks | ||||||||||||||||||||||||||||||||||||
| if len(m.selected) > 0 { | ||||||||||||||||||||||||||||||||||||
| o.checkSelected(m) | ||||||||||||||||||||||||||||||||||||
| o.checkSelected(m, filteringChoices) | ||||||||||||||||||||||||||||||||||||
| } else if len(m.matches) > m.cursor && m.cursor >= 0 { | ||||||||||||||||||||||||||||||||||||
| tty.Println(m.matches[m.cursor].Str) | ||||||||||||||||||||||||||||||||||||
| if o.OutputIndexes { | ||||||||||||||||||||||||||||||||||||
| idx := o.findIndex(m.matches[m.cursor].Str, filteringChoices) | ||||||||||||||||||||||||||||||||||||
| if idx >= 0 { | ||||||||||||||||||||||||||||||||||||
| tty.Println(strconv.Itoa(idx)) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||
| tty.Println(m.matches[m.cursor].Str) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| func (o Options) checkSelected(m model) { | ||||||||||||||||||||||||||||||||||||
| out := []string{} | ||||||||||||||||||||||||||||||||||||
| for k := range m.selected { | ||||||||||||||||||||||||||||||||||||
| out = append(out, k) | ||||||||||||||||||||||||||||||||||||
| func (o Options) checkSelected(m model, filteringChoices []string) { | ||||||||||||||||||||||||||||||||||||
| if o.OutputIndexes { | ||||||||||||||||||||||||||||||||||||
| // For each selected item, find all indexes in filteringChoices (handles duplicates) | ||||||||||||||||||||||||||||||||||||
| indexes := make([]string, 0, len(m.selected)) | ||||||||||||||||||||||||||||||||||||
| for k := range m.selected { | ||||||||||||||||||||||||||||||||||||
| for i, choice := range filteringChoices { | ||||||||||||||||||||||||||||||||||||
| if choice == k { | ||||||||||||||||||||||||||||||||||||
| indexes = append(indexes, strconv.Itoa(i)) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+185
to
+193
|
||||||||||||||||||||||||||||||||||||
| // For each selected item, find all indexes in filteringChoices (handles duplicates) | |
| indexes := make([]string, 0, len(m.selected)) | |
| for k := range m.selected { | |
| for i, choice := range filteringChoices { | |
| if choice == k { | |
| indexes = append(indexes, strconv.Itoa(i)) | |
| } | |
| } | |
| } | |
| // For each selected item, find the first index in filteringChoices (consistent with single-selection) | |
| indexes := make([]string, 0, len(m.selected)) | |
| for k := range m.selected { | |
| idx := o.findIndex(k, filteringChoices) | |
| if idx >= 0 { | |
| indexes = append(indexes, strconv.Itoa(idx)) | |
| } | |
| } |
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.
The nested loop in
checkSelectedhas O(n*m) complexity where n is the number of selected items and m is the total number of choices. For multiple selections with many choices, this could be inefficient. Consider using the existingfindIndexhelper method which provides the same functionality but with clearer intent:idx := o.findIndex(k, filteringChoices); if idx >= 0 { indexes = append(indexes, strconv.Itoa(idx)) }