SelectPromptWithDescriptions is like SelectPrompt but shows a description next to each option. The descriptions slice must be the same length as options.
(msg string, options, descriptions []string, def string)
| 28 | // SelectPromptWithDescriptions is like SelectPrompt but shows a description next to each option. |
| 29 | // The descriptions slice must be the same length as options. |
| 30 | func SelectPromptWithDescriptions(msg string, options, descriptions []string, def string) (string, error) { |
| 31 | if len(options) != len(descriptions) { |
| 32 | return "", fmt.Errorf("options and descriptions must be the same length") |
| 33 | } |
| 34 | |
| 35 | prompt := &survey.Select{ |
| 36 | Message: msg, |
| 37 | Options: options, |
| 38 | Description: func(value string, index int) string { |
| 39 | return descriptions[index] |
| 40 | }, |
| 41 | } |
| 42 | |
| 43 | if slices.Contains(options, def) { |
| 44 | prompt.Default = def |
| 45 | } |
| 46 | |
| 47 | result := "" |
| 48 | if err := survey.AskOne(prompt, &result); err != nil { |
| 49 | return "", fmt.Errorf("prompt failed: %w", err) |
| 50 | } |
| 51 | return result, nil |
| 52 | } |
| 53 | |
| 54 | // ConfirmPrompt asks the user to confirm an action. |
| 55 | // It returns nil if the user confirms and an error if the user declines (or the prompt fails). |