@@ -227,10 +227,10 @@ func (m selectModel) viewableOptions() ([]string, int) {
227
227
func (m selectModel ) filteredOptions () []string {
228
228
options := []string {}
229
229
for _ , o := range m .options {
230
- prefix := strings .ToLower (m .search .Value ())
230
+ filter := strings .ToLower (m .search .Value ())
231
231
option := strings .ToLower (o )
232
232
233
- if strings .HasPrefix (option , prefix ) {
233
+ if strings .Contains (option , filter ) {
234
234
options = append (options , o )
235
235
}
236
236
}
@@ -249,15 +249,20 @@ func MultiSelect(inv *serpent.Invocation, opts MultiSelectOptions) ([]string, er
249
249
return opts .Defaults , nil
250
250
}
251
251
252
- options := make ([]multiSelectOption , len (opts .Options ))
252
+ options := make ([]* multiSelectOption , len (opts .Options ))
253
253
for i , option := range opts .Options {
254
- options [i ].option = option
255
-
254
+ chosen := false
256
255
for _ , d := range opts .Defaults {
257
256
if option == d {
258
- options [ i ]. chosen = true
257
+ chosen = true
259
258
}
260
259
}
260
+
261
+ options [i ] = & multiSelectOption {
262
+ option : option ,
263
+ chosen : chosen ,
264
+ }
265
+
261
266
}
262
267
263
268
initialModel := multiSelectModel {
@@ -293,7 +298,7 @@ type multiSelectOption struct {
293
298
294
299
type multiSelectModel struct {
295
300
search textinput.Model
296
- options []multiSelectOption
301
+ options []* multiSelectOption
297
302
cursor int
298
303
message string
299
304
canceled bool
@@ -321,8 +326,9 @@ func (m multiSelectModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
321
326
}
322
327
323
328
case tea .KeySpace :
324
- if len (m .options ) != 0 {
325
- m .options [m .cursor ].chosen = ! m .options [m .cursor ].chosen
329
+ options := m .filteredOptions ()
330
+ if len (options ) != 0 {
331
+ options [m .cursor ].chosen = ! options [m .cursor ].chosen
326
332
}
327
333
328
334
case tea .KeyUp :
@@ -336,13 +342,15 @@ func (m multiSelectModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
336
342
}
337
343
338
344
case tea .KeyRight :
339
- for i := range m .options {
340
- m .options [i ].chosen = true
345
+ options := m .filteredOptions ()
346
+ for _ , option := range options {
347
+ option .chosen = false
341
348
}
342
349
343
350
case tea .KeyLeft :
344
- for i := range m .options {
345
- m .options [i ].chosen = false
351
+ options := m .filteredOptions ()
352
+ for _ , option := range options {
353
+ option .chosen = false
346
354
}
347
355
348
356
default :
@@ -352,8 +360,9 @@ func (m multiSelectModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
352
360
// If the search query has changed then we need to ensure
353
361
// the cursor is still pointing at a valid option.
354
362
if m .search .Value () != oldSearch {
355
- if m .cursor > len (m .options )- 1 {
356
- m .cursor = max (0 , len (m .options )- 1 )
363
+ options := m .filteredOptions ()
364
+ if m .cursor > len (options )- 1 {
365
+ m .cursor = max (0 , len (options )- 1 )
357
366
}
358
367
}
359
368
}
@@ -370,23 +379,27 @@ func (m multiSelectModel) View() string {
370
379
if ! m .selected {
371
380
s += fmt .Sprintf ("%s %s[Use arrows to move, space to select, <right> to all, <left> to none, type to filter]\n " , msg , m .search .View ())
372
381
373
- for i , option := range m .options {
382
+ for i , option := range m .filteredOptions () {
374
383
cursor := " "
384
+ chosen := "[ ]"
385
+ o := option .option
386
+
375
387
if m .cursor == i {
376
388
cursor = pretty .Sprint (pretty .FgColor (Green ), "> " )
389
+ chosen = pretty .Sprint (pretty .FgColor (Green ), "[ ]" )
390
+ o = pretty .Sprint (pretty .FgColor (Green ), o )
377
391
}
378
392
379
- chosen := "[ ]"
380
393
if option .chosen {
381
394
chosen = pretty .Sprint (pretty .FgColor (Green ), "[x]" )
382
395
}
383
396
384
- o := option . option
385
- if m . cursor == i {
386
- o = pretty . Sprint ( pretty . FgColor ( Green ), o )
387
- }
388
-
389
- s += fmt . Sprintf ( "%s%s %s \n " , cursor , chosen , o )
397
+ s += fmt . Sprintf (
398
+ "%s%s %s \n " ,
399
+ cursor ,
400
+ chosen ,
401
+ o ,
402
+ )
390
403
}
391
404
} else {
392
405
selected := pretty .Sprint (DefaultStyles .Keyword , strings .Join (m .selectedOptions (), ", " ))
@@ -397,6 +410,19 @@ func (m multiSelectModel) View() string {
397
410
return s
398
411
}
399
412
413
+ func (m multiSelectModel ) filteredOptions () []* multiSelectOption {
414
+ options := []* multiSelectOption {}
415
+ for _ , o := range m .options {
416
+ filter := strings .ToLower (m .search .Value ())
417
+ option := strings .ToLower (o .option )
418
+
419
+ if strings .Contains (option , filter ) {
420
+ options = append (options , o )
421
+ }
422
+ }
423
+ return options
424
+ }
425
+
400
426
func (m multiSelectModel ) selectedOptions () []string {
401
427
selected := []string {}
402
428
for _ , o := range m .options {
0 commit comments