|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import Generic, Sequence, TypeVar |
| 4 | + |
| 5 | +from prompt_toolkit.application import Application |
| 6 | +from prompt_toolkit.filters import Condition, FilterOrBool, to_filter |
| 7 | +from prompt_toolkit.formatted_text import AnyFormattedText |
| 8 | +from prompt_toolkit.key_binding.key_bindings import KeyBindings |
| 9 | +from prompt_toolkit.key_binding.key_processor import KeyPressEvent |
| 10 | +from prompt_toolkit.layout import AnyContainer, HSplit, Layout |
| 11 | +from prompt_toolkit.styles import BaseStyle |
| 12 | +from prompt_toolkit.utils import suspend_to_background_supported |
| 13 | +from prompt_toolkit.widgets import Box, Frame, Label, RadioList |
| 14 | + |
| 15 | +_T = TypeVar("_T") |
| 16 | +E = KeyPressEvent |
| 17 | + |
| 18 | + |
| 19 | +class InputSelection(Generic[_T]): |
| 20 | + def __init__( |
| 21 | + self, |
| 22 | + *, |
| 23 | + message: AnyFormattedText, |
| 24 | + options: Sequence[tuple[_T, AnyFormattedText]], |
| 25 | + default: _T | None = None, |
| 26 | + mouse_support: bool = True, |
| 27 | + style: BaseStyle | None = None, |
| 28 | + symbol: str = ">", |
| 29 | + show_frame: bool = False, |
| 30 | + enable_suspend: FilterOrBool = False, |
| 31 | + enable_abort: FilterOrBool = True, |
| 32 | + interrupt_exception: type[BaseException] = KeyboardInterrupt, |
| 33 | + ) -> None: |
| 34 | + self.message = message |
| 35 | + self.default = default |
| 36 | + self.options = options |
| 37 | + self.mouse_support = mouse_support |
| 38 | + self.style = style |
| 39 | + self.symbol = symbol |
| 40 | + self.show_frame = show_frame |
| 41 | + self.enable_suspend = enable_suspend |
| 42 | + self.interrupt_exception = interrupt_exception |
| 43 | + self.enable_abort = enable_abort |
| 44 | + |
| 45 | + def _create_application(self) -> Application[_T]: |
| 46 | + radio_list = RadioList( |
| 47 | + values=self.options, |
| 48 | + default=self.default, |
| 49 | + select_on_focus=True, |
| 50 | + open_character="", |
| 51 | + select_character=self.symbol, |
| 52 | + close_character="", |
| 53 | + show_cursor=False, |
| 54 | + show_numbers=True, |
| 55 | + container_style="class:input-selection", |
| 56 | + default_style="class:option", |
| 57 | + selected_style="", |
| 58 | + checked_style="class:selected-option", |
| 59 | + number_style="class:number", |
| 60 | + ) |
| 61 | + container: AnyContainer = HSplit( |
| 62 | + [ |
| 63 | + Box( |
| 64 | + Label(text=self.message, dont_extend_height=True), |
| 65 | + padding_top=0, |
| 66 | + padding_left=1, |
| 67 | + padding_right=1, |
| 68 | + padding_bottom=0, |
| 69 | + ), |
| 70 | + Box( |
| 71 | + radio_list, |
| 72 | + padding_top=0, |
| 73 | + padding_left=3, |
| 74 | + padding_right=1, |
| 75 | + padding_bottom=0, |
| 76 | + ), |
| 77 | + ] |
| 78 | + ) |
| 79 | + if self.show_frame: |
| 80 | + container = Frame(container) |
| 81 | + layout = Layout(container, radio_list) |
| 82 | + |
| 83 | + kb = KeyBindings() |
| 84 | + |
| 85 | + @kb.add("enter", eager=True) |
| 86 | + def _accept_input(event: E) -> None: |
| 87 | + "Accept input when enter has been pressed." |
| 88 | + event.app.exit(result=radio_list.current_value) |
| 89 | + |
| 90 | + @Condition |
| 91 | + def enable_abort() -> bool: |
| 92 | + return to_filter(self.enable_abort)() |
| 93 | + |
| 94 | + @kb.add("c-c", filter=enable_abort) |
| 95 | + @kb.add("<sigint>", filter=enable_abort) |
| 96 | + def _keyboard_interrupt(event: E) -> None: |
| 97 | + "Abort when Control-C has been pressed." |
| 98 | + event.app.exit(exception=self.interrupt_exception(), style="class:aborting") |
| 99 | + |
| 100 | + suspend_supported = Condition(suspend_to_background_supported) |
| 101 | + |
| 102 | + @Condition |
| 103 | + def enable_suspend() -> bool: |
| 104 | + return to_filter(self.enable_suspend)() |
| 105 | + |
| 106 | + @kb.add("c-z", filter=suspend_supported & enable_suspend) |
| 107 | + def _suspend(event: E) -> None: |
| 108 | + """ |
| 109 | + Suspend process to background. |
| 110 | + """ |
| 111 | + event.app.suspend_to_background() |
| 112 | + |
| 113 | + return Application( |
| 114 | + layout=layout, |
| 115 | + full_screen=False, |
| 116 | + mouse_support=self.mouse_support, |
| 117 | + key_bindings=kb, |
| 118 | + style=self.style, |
| 119 | + ) |
| 120 | + |
| 121 | + def prompt(self) -> _T: |
| 122 | + return self._create_application().run() |
| 123 | + |
| 124 | + async def prompt_async(self) -> _T: |
| 125 | + return await self._create_application().run_async() |
| 126 | + |
| 127 | + |
| 128 | +def select_input( |
| 129 | + message: AnyFormattedText, |
| 130 | + options: Sequence[tuple[_T, AnyFormattedText]], |
| 131 | + default: _T | None = None, |
| 132 | + mouse_support: bool = True, |
| 133 | + style: BaseStyle | None = None, |
| 134 | + symbol: str = ">", |
| 135 | + show_frame: bool = False, |
| 136 | + enable_suspend: FilterOrBool = False, |
| 137 | + enable_abort: FilterOrBool = True, |
| 138 | +) -> _T: |
| 139 | + return InputSelection( |
| 140 | + message=message, |
| 141 | + options=options, |
| 142 | + default=default, |
| 143 | + mouse_support=mouse_support, |
| 144 | + show_frame=show_frame, |
| 145 | + symbol=symbol, |
| 146 | + style=style, |
| 147 | + enable_suspend=enable_suspend, |
| 148 | + enable_abort=enable_abort, |
| 149 | + ).prompt() |
0 commit comments