Prompt for a choice from a list.
(message: str, choices: list[str], default: Optional[str] = None)
| 122 | |
| 123 | |
| 124 | def prompt_choice(message: str, choices: list[str], default: Optional[str] = None) -> str: |
| 125 | """Prompt for a choice from a list.""" |
| 126 | # Display numbered choices |
| 127 | console.print(f"\n[bold]{message}[/bold]") |
| 128 | for i, choice in enumerate(choices, 1): |
| 129 | console.print(f" [highlight][{i}][/highlight] {choice}") |
| 130 | |
| 131 | while True: |
| 132 | response = Prompt.ask( |
| 133 | "\n" + t("prompt_select"), |
| 134 | console=console, |
| 135 | default=str(choices.index(default) + 1) if default else None, |
| 136 | ) |
| 137 | try: |
| 138 | idx = int(response) - 1 |
| 139 | if 0 <= idx < len(choices): |
| 140 | return choices[idx] |
| 141 | except ValueError: |
| 142 | # Check if response matches a choice directly |
| 143 | if response in choices: |
| 144 | return response |
| 145 | |
| 146 | print_error(f"Please enter a number between 1 and {len(choices)}") |
| 147 | |
| 148 | |
| 149 | def prompt_text(message: str, default: Optional[str] = None) -> str: |
nothing calls this directly
no test coverage detected