skillSearchFunc returns a search function for MultiSelectWithSearch that filters skills by case-insensitive substring match on name and description.
(skills []discovery.Skill, labelWidth int)
| 822 | // skillSearchFunc returns a search function for MultiSelectWithSearch that |
| 823 | // filters skills by case-insensitive substring match on name and description. |
| 824 | func skillSearchFunc(skills []discovery.Skill, labelWidth int) func(string) prompter.MultiSelectSearchResult { |
| 825 | return func(query string) prompter.MultiSelectSearchResult { |
| 826 | var matched []discovery.Skill |
| 827 | if query == "" { |
| 828 | matched = skills |
| 829 | } else { |
| 830 | q := strings.ToLower(query) |
| 831 | for _, s := range skills { |
| 832 | if strings.Contains(strings.ToLower(s.DisplayName()), q) || |
| 833 | strings.Contains(strings.ToLower(s.Description), q) { |
| 834 | matched = append(matched, s) |
| 835 | } |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | more := 0 |
| 840 | if len(matched) > maxSearchResults { |
| 841 | more = len(matched) - maxSearchResults |
| 842 | matched = matched[:maxSearchResults] |
| 843 | } |
| 844 | |
| 845 | keys := make([]string, len(matched)) |
| 846 | labels := make([]string, len(matched)) |
| 847 | for i, s := range matched { |
| 848 | keys[i] = s.DisplayName() |
| 849 | label := s.DisplayName() |
| 850 | if s.Description != "" { |
| 851 | label = fmt.Sprintf("%s - %s", label, text.RemoveExcessiveWhitespace(iostreams.NewUntrusted(s.Description).String())) |
| 852 | } |
| 853 | labels[i] = text.Truncate(labelWidth, label) |
| 854 | } |
| 855 | |
| 856 | return prompter.MultiSelectSearchResult{ |
| 857 | Keys: keys, |
| 858 | Labels: labels, |
| 859 | MoreResults: more, |
| 860 | } |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | // matchSelectedSkills maps display names back to skill structs. |
| 865 | func matchSelectedSkills(skills []discovery.Skill, selected []string) ([]discovery.Skill, error) { |