| 39 | } |
| 40 | |
| 41 | func (p *huhPrompter) buildSelectForm(prompt, defaultValue string, options []string) (*huh.Form, *int) { |
| 42 | var result int |
| 43 | |
| 44 | if !slices.Contains(options, defaultValue) { |
| 45 | defaultValue = "" |
| 46 | } |
| 47 | |
| 48 | formOptions := make([]huh.Option[int], len(options)) |
| 49 | for i, o := range options { |
| 50 | if defaultValue == o { |
| 51 | result = i |
| 52 | } |
| 53 | formOptions[i] = huh.NewOption(o, i) |
| 54 | } |
| 55 | |
| 56 | form := p.newForm( |
| 57 | huh.NewGroup( |
| 58 | huh.NewSelect[int](). |
| 59 | Title(prompt). |
| 60 | Value(&result). |
| 61 | Options(formOptions...), |
| 62 | ), |
| 63 | ) |
| 64 | return form, &result |
| 65 | } |
| 66 | |
| 67 | func (p *huhPrompter) Select(prompt, defaultValue string, options []string) (int, error) { |
| 68 | form, result := p.buildSelectForm(prompt, defaultValue, options) |