2023-12-08 10:37:32 +08:00
|
|
|
from typing import List
|
|
|
|
|
|
|
|
from .iobase import pipe_interaction
|
|
|
|
|
|
|
|
|
|
|
|
class RadioOption:
|
|
|
|
def __init__(self, id, text):
|
|
|
|
# it will show as: - (id): text
|
|
|
|
self._id = id
|
|
|
|
self._text = text
|
|
|
|
|
2023-12-08 18:28:36 +08:00
|
|
|
|
2023-12-08 10:37:32 +08:00
|
|
|
def ui_radio_select(title: str, options: List[RadioOption]) -> str | None:
|
|
|
|
"""
|
|
|
|
```chatmark type=form
|
2023-12-08 18:28:36 +08:00
|
|
|
How would you like to make the change?
|
|
|
|
> - (insert) Insert the new code.
|
|
|
|
> - (new) Put the code in a new file.
|
|
|
|
> - (replace) Replace the current code.
|
|
|
|
```
|
2023-12-08 10:37:32 +08:00
|
|
|
"""
|
2023-12-08 18:28:36 +08:00
|
|
|
option_line = lambda option: f"> - ({option._id}) {option._text}"
|
2023-12-08 10:37:32 +08:00
|
|
|
options_lines = "\n".join([option_line(option) for option in options])
|
|
|
|
ui_message = f"""
|
|
|
|
```chatmark type=form
|
|
|
|
{title}
|
|
|
|
{options_lines}
|
|
|
|
```
|
|
|
|
"""
|
|
|
|
|
|
|
|
response = pipe_interaction(ui_message)
|
2023-12-08 18:28:36 +08:00
|
|
|
|
|
|
|
selected_options = [
|
|
|
|
key
|
|
|
|
for key, value in response.items()
|
|
|
|
if value == "checked" and key in [option._id for option in options]
|
|
|
|
]
|
2023-12-08 10:37:32 +08:00
|
|
|
return selected_options[0] if len(selected_options) > 0 else None
|