1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
from __future__ import annotations
from typing import Generic, Sequence, TypeVar
from prompt_toolkit.application import Application
from prompt_toolkit.filters import (
Condition,
FilterOrBool,
is_done,
renderer_height_is_known,
to_filter,
)
from prompt_toolkit.formatted_text import AnyFormattedText
from prompt_toolkit.key_binding.key_bindings import (
DynamicKeyBindings,
KeyBindings,
KeyBindingsBase,
merge_key_bindings,
)
from prompt_toolkit.key_binding.key_processor import KeyPressEvent
from prompt_toolkit.layout import (
AnyContainer,
ConditionalContainer,
HSplit,
Layout,
Window,
)
from prompt_toolkit.layout.controls import FormattedTextControl
from prompt_toolkit.layout.dimension import Dimension
from prompt_toolkit.styles import BaseStyle, Style
from prompt_toolkit.utils import suspend_to_background_supported
from prompt_toolkit.widgets import Box, Frame, Label, RadioList
__all__ = [
"ChoiceInput",
"choice",
]
_T = TypeVar("_T")
E = KeyPressEvent
def create_default_choice_input_style() -> BaseStyle:
return Style.from_dict(
{
"frame.border": "#884444",
"selected-option": "bold",
}
)
class ChoiceInput(Generic[_T]):
"""
Input selection prompt. Ask the user to choose among a set of options.
Example usage::
input_selection = ChoiceInput(
message="Please select a dish:",
options=[
("pizza", "Pizza with mushrooms"),
("salad", "Salad with tomatoes"),
("sushi", "Sushi"),
],
default="pizza",
)
result = input_selection.prompt()
:param message: Plain text or formatted text to be shown before the options.
:param options: Sequence of ``(value, label)`` tuples. The labels can be
formatted text.
:param default: Default value. If none is given, the first option is
considered the default.
:param mouse_support: Enable mouse support.
:param style: :class:`.Style` instance for the color scheme.
:param symbol: Symbol to be displayed in front of the selected choice.
:param bottom_toolbar: Formatted text or callable that returns formatted
text to be displayed at the bottom of the screen.
:param show_frame: `bool` or
:class:`~prompt_toolkit.filters.Filter`. When True, surround the input
with a frame.
:param enable_interrupt: `bool` or
:class:`~prompt_toolkit.filters.Filter`. When True, raise
the ``interrupt_exception`` (``KeyboardInterrupt`` by default) when
control-c has been pressed.
:param interrupt_exception: The exception type that will be raised when
there is a keyboard interrupt (control-c keypress).
"""
def __init__(
self,
*,
message: AnyFormattedText,
options: Sequence[tuple[_T, AnyFormattedText]],
default: _T | None = None,
mouse_support: bool = False,
style: BaseStyle | None = None,
symbol: str = ">",
bottom_toolbar: AnyFormattedText = None,
show_frame: FilterOrBool = False,
enable_suspend: FilterOrBool = False,
enable_interrupt: FilterOrBool = True,
interrupt_exception: type[BaseException] = KeyboardInterrupt,
key_bindings: KeyBindingsBase | None = None,
) -> None:
if style is None:
style = create_default_choice_input_style()
self.message = message
self.default = default
self.options = options
self.mouse_support = mouse_support
self.style = style
self.symbol = symbol
self.show_frame = show_frame
self.enable_suspend = enable_suspend
self.interrupt_exception = interrupt_exception
self.enable_interrupt = enable_interrupt
self.bottom_toolbar = bottom_toolbar
self.key_bindings = key_bindings
def _create_application(self) -> Application[_T]:
radio_list = RadioList(
values=self.options,
default=self.default,
select_on_focus=True,
open_character="",
select_character=self.symbol,
close_character="",
show_cursor=False,
show_numbers=True,
container_style="class:input-selection",
default_style="class:option",
selected_style="",
checked_style="class:selected-option",
number_style="class:number",
show_scrollbar=False,
)
container: AnyContainer = HSplit(
[
Box(
Label(text=self.message, dont_extend_height=True),
padding_top=0,
padding_left=1,
padding_right=1,
padding_bottom=0,
),
Box(
radio_list,
padding_top=0,
padding_left=3,
padding_right=1,
padding_bottom=0,
),
]
)
@Condition
def show_frame_filter() -> bool:
return to_filter(self.show_frame)()
show_bottom_toolbar = (
Condition(lambda: self.bottom_toolbar is not None)
& ~is_done
& renderer_height_is_known
)
container = ConditionalContainer(
Frame(container),
alternative_content=container,
filter=show_frame_filter,
)
bottom_toolbar = ConditionalContainer(
Window(
FormattedTextControl(
lambda: self.bottom_toolbar, style="class:bottom-toolbar.text"
),
style="class:bottom-toolbar",
dont_extend_height=True,
height=Dimension(min=1),
),
filter=show_bottom_toolbar,
)
layout = Layout(
HSplit(
[
container,
# Add an empty window between the selection input and the
# bottom toolbar, if the bottom toolbar is visible, in
# order to allow the bottom toolbar to be displayed at the
# bottom of the screen.
ConditionalContainer(Window(), filter=show_bottom_toolbar),
bottom_toolbar,
]
),
focused_element=radio_list,
)
kb = KeyBindings()
@kb.add("enter", eager=True)
def _accept_input(event: E) -> None:
"Accept input when enter has been pressed."
event.app.exit(result=radio_list.current_value, style="class:accepted")
@Condition
def enable_interrupt() -> bool:
return to_filter(self.enable_interrupt)()
@kb.add("c-c", filter=enable_interrupt)
@kb.add("<sigint>", filter=enable_interrupt)
def _keyboard_interrupt(event: E) -> None:
"Abort when Control-C has been pressed."
event.app.exit(exception=self.interrupt_exception(), style="class:aborting")
suspend_supported = Condition(suspend_to_background_supported)
@Condition
def enable_suspend() -> bool:
return to_filter(self.enable_suspend)()
@kb.add("c-z", filter=suspend_supported & enable_suspend)
def _suspend(event: E) -> None:
"""
Suspend process to background.
"""
event.app.suspend_to_background()
return Application(
layout=layout,
full_screen=False,
mouse_support=self.mouse_support,
key_bindings=merge_key_bindings(
[kb, DynamicKeyBindings(lambda: self.key_bindings)]
),
style=self.style,
)
def prompt(self) -> _T:
return self._create_application().run()
async def prompt_async(self) -> _T:
return await self._create_application().run_async()
def choice(
message: AnyFormattedText,
*,
options: Sequence[tuple[_T, AnyFormattedText]],
default: _T | None = None,
mouse_support: bool = False,
style: BaseStyle | None = None,
symbol: str = ">",
bottom_toolbar: AnyFormattedText = None,
show_frame: bool = False,
enable_suspend: FilterOrBool = False,
enable_interrupt: FilterOrBool = True,
interrupt_exception: type[BaseException] = KeyboardInterrupt,
key_bindings: KeyBindingsBase | None = None,
) -> _T:
"""
Choice selection prompt. Ask the user to choose among a set of options.
Example usage::
result = choice(
message="Please select a dish:",
options=[
("pizza", "Pizza with mushrooms"),
("salad", "Salad with tomatoes"),
("sushi", "Sushi"),
],
default="pizza",
)
:param message: Plain text or formatted text to be shown before the options.
:param options: Sequence of ``(value, label)`` tuples. The labels can be
formatted text.
:param default: Default value. If none is given, the first option is
considered the default.
:param mouse_support: Enable mouse support.
:param style: :class:`.Style` instance for the color scheme.
:param symbol: Symbol to be displayed in front of the selected choice.
:param bottom_toolbar: Formatted text or callable that returns formatted
text to be displayed at the bottom of the screen.
:param show_frame: `bool` or
:class:`~prompt_toolkit.filters.Filter`. When True, surround the input
with a frame.
:param enable_interrupt: `bool` or
:class:`~prompt_toolkit.filters.Filter`. When True, raise
the ``interrupt_exception`` (``KeyboardInterrupt`` by default) when
control-c has been pressed.
:param interrupt_exception: The exception type that will be raised when
there is a keyboard interrupt (control-c keypress).
"""
return ChoiceInput[_T](
message=message,
options=options,
default=default,
mouse_support=mouse_support,
style=style,
symbol=symbol,
bottom_toolbar=bottom_toolbar,
show_frame=show_frame,
enable_suspend=enable_suspend,
enable_interrupt=enable_interrupt,
interrupt_exception=interrupt_exception,
key_bindings=key_bindings,
).prompt()
|