ShowInteractiveList displays an interactive list using huh.Select with arrow key navigation. Returns the selected item's value, or an error if cancelled or failed. Use this for standalone pickers outside a form context; prefer huh.Select directly when building a multi-field form with WithTheme/With
(title string, items []ListItem)
| 21 | // Use this for standalone pickers outside a form context; prefer huh.Select directly |
| 22 | // when building a multi-field form with WithTheme/WithAccessible applied to the whole form. |
| 23 | func ShowInteractiveList(title string, items []ListItem) (string, error) { |
| 24 | listLog.Printf("Showing interactive list: title=%s, items=%d", title, len(items)) |
| 25 | |
| 26 | if len(items) == 0 { |
| 27 | return "", errors.New("no items to display") |
| 28 | } |
| 29 | |
| 30 | // Check if we're in a TTY environment |
| 31 | if !tty.IsStderrTerminal() { |
| 32 | listLog.Print("Non-TTY detected, falling back to text list") |
| 33 | return showTextList(title, items) |
| 34 | } |
| 35 | |
| 36 | // Build huh options, combining title and description into the option label |
| 37 | opts := make([]huh.Option[string], len(items)) |
| 38 | for i, item := range items { |
| 39 | label := item.title |
| 40 | if item.description != "" { |
| 41 | label = fmt.Sprintf("%s – %s", item.title, item.description) |
| 42 | } |
| 43 | opts[i] = huh.NewOption(label, item.value) |
| 44 | } |
| 45 | |
| 46 | var selected string |
| 47 | form := huh.NewForm( |
| 48 | huh.NewGroup( |
| 49 | huh.NewSelect[string](). |
| 50 | Title(title). |
| 51 | Options(opts...). |
| 52 | Value(&selected), |
| 53 | ), |
| 54 | ).WithTheme(styles.HuhTheme).WithAccessible(IsAccessibleMode()) |
| 55 | |
| 56 | if err := form.Run(); err != nil { |
| 57 | listLog.Printf("Error running list form: %v", err) |
| 58 | return "", fmt.Errorf("failed to run interactive list: %w", err) |
| 59 | } |
| 60 | |
| 61 | listLog.Printf("Selected item: %s", selected) |
| 62 | return selected, nil |
| 63 | } |
| 64 | |
| 65 | // showTextList displays a non-interactive numbered list for non-TTY environments |
| 66 | func showTextList(title string, items []ListItem) (string, error) { |