showTextList displays a non-interactive numbered list for non-TTY environments
(title string, items []ListItem)
| 64 | |
| 65 | // showTextList displays a non-interactive numbered list for non-TTY environments |
| 66 | func showTextList(title string, items []ListItem) (string, error) { |
| 67 | listLog.Printf("Showing text list: title=%s, items=%d", title, len(items)) |
| 68 | |
| 69 | fmt.Fprintf(os.Stderr, "\n%s\n\n", title) |
| 70 | for i, item := range items { |
| 71 | fmt.Fprintf(os.Stderr, " %d) %s\n", i+1, item.title) |
| 72 | if item.description != "" { |
| 73 | fmt.Fprintf(os.Stderr, " %s\n", item.description) |
| 74 | } |
| 75 | } |
| 76 | fmt.Fprintf(os.Stderr, "\nSelect (1-%d): ", len(items)) |
| 77 | |
| 78 | var choice int |
| 79 | _, err := fmt.Scanf("%d", &choice) |
| 80 | if err != nil { |
| 81 | return "", fmt.Errorf("invalid input: %w", err) |
| 82 | } |
| 83 | |
| 84 | if choice < 1 || choice > len(items) { |
| 85 | return "", fmt.Errorf("selection out of range (must be 1-%d)", len(items)) |
| 86 | } |
| 87 | |
| 88 | selectedItem := items[choice-1] |
| 89 | listLog.Printf("Selected item from text list: %s", selectedItem.value) |
| 90 | return selectedItem.value, nil |
| 91 | } |
no test coverage detected