MCPcopy Create free account
hub / github.com/cli/cli / multiSelectWithSearch

Function multiSelectWithSearch

internal/prompter/prompter.go:372–513  ·  view source on GitHub ↗
(p Prompter, prompt, searchPrompt string, defaultValues, persistentValues []string, searchFunc func(string) MultiSelectSearchResult)

Source from the content-addressed store, hash-verified

370}
371
372func multiSelectWithSearch(p Prompter, prompt, searchPrompt string, defaultValues, persistentValues []string, searchFunc func(string) MultiSelectSearchResult) ([]string, error) {
373 selectedOptions := defaultValues
374
375 // The optionKeyLabels map is used to uniquely identify optionKeyLabels
376 // and provide optional display labels.
377 optionKeyLabels := make(map[string]string)
378 for _, k := range selectedOptions {
379 optionKeyLabels[k] = k
380 }
381
382 searchResult := searchFunc("")
383 if searchResult.Err != nil {
384 return nil, fmt.Errorf("failed to search: %w", searchResult.Err)
385 }
386 searchResultKeys := searchResult.Keys
387 searchResultLabels := searchResult.Labels
388 moreResults := searchResult.MoreResults
389
390 for i, k := range searchResultKeys {
391 optionKeyLabels[k] = searchResultLabels[i]
392 }
393
394 for {
395 // Build dynamic option list -> search sentinel, selections, search results, persistent options.
396 optionKeys := make([]string, 0, 1+len(selectedOptions)+len(searchResultKeys)+len(persistentValues))
397 optionLabels := make([]string, 0, len(optionKeys))
398
399 // 1. Search sentinel.
400 optionKeys = append(optionKeys, "")
401 if moreResults > 0 {
402 optionLabels = append(optionLabels, fmt.Sprintf("Search (%d more)", moreResults))
403 } else {
404 optionLabels = append(optionLabels, "Search")
405 }
406
407 // 2. Selections
408 for _, k := range selectedOptions {
409 l := optionKeyLabels[k]
410
411 if l == "" {
412 l = k
413 }
414
415 optionKeys = append(optionKeys, k)
416 optionLabels = append(optionLabels, l)
417 }
418
419 // 3. Search results
420 for _, k := range searchResultKeys {
421 // It's already selected or persistent, if we add here we'll have duplicates.
422 if slices.Contains(selectedOptions, k) || slices.Contains(persistentValues, k) {
423 continue
424 }
425
426 l := optionKeyLabels[k]
427 if l == "" {
428 l = k
429 }

Callers 2

MultiSelectWithSearchMethod · 0.85
MultiSelectWithSearchMethod · 0.85

Calls 4

ContainsMethod · 0.80
ErrorfMethod · 0.65
MultiSelectMethod · 0.65
InputMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…