Ptpython configuration option that can be shown and modified from the sidebar. :param title: Text. :param description: Text. :param get_values: Callable that returns a dictionary mapping the possible values to callbacks that activate these value. :param get_curr
| 113 | |
| 114 | |
| 115 | class Option(Generic[_T_lt]): |
| 116 | """ |
| 117 | Ptpython configuration option that can be shown and modified from the |
| 118 | sidebar. |
| 119 | |
| 120 | :param title: Text. |
| 121 | :param description: Text. |
| 122 | :param get_values: Callable that returns a dictionary mapping the |
| 123 | possible values to callbacks that activate these value. |
| 124 | :param get_current_value: Callable that returns the current, active value. |
| 125 | """ |
| 126 | |
| 127 | def __init__( |
| 128 | self, |
| 129 | title: str, |
| 130 | description: str, |
| 131 | get_current_value: Callable[[], _T_lt], |
| 132 | # We accept `object` as return type for the select functions, because |
| 133 | # often they return an unused boolean. Maybe this can be improved. |
| 134 | get_values: Callable[[], Mapping[_T_lt, Callable[[], object]]], |
| 135 | ) -> None: |
| 136 | self.title = title |
| 137 | self.description = description |
| 138 | self.get_current_value = get_current_value |
| 139 | self.get_values = get_values |
| 140 | |
| 141 | @property |
| 142 | def values(self) -> Mapping[_T_lt, Callable[[], object]]: |
| 143 | return self.get_values() |
| 144 | |
| 145 | def activate_next(self, _previous: bool = False) -> None: |
| 146 | """ |
| 147 | Activate next value. |
| 148 | """ |
| 149 | current = self.get_current_value() |
| 150 | options = sorted(self.values.keys()) |
| 151 | |
| 152 | # Get current index. |
| 153 | try: |
| 154 | index = options.index(current) |
| 155 | except ValueError: |
| 156 | index = 0 |
| 157 | |
| 158 | # Go to previous/next index. |
| 159 | if _previous: |
| 160 | index -= 1 |
| 161 | else: |
| 162 | index += 1 |
| 163 | |
| 164 | # Call handler for this option. |
| 165 | next_option = options[index % len(options)] |
| 166 | self.values[next_option]() |
| 167 | |
| 168 | def activate_previous(self) -> None: |
| 169 | """ |
| 170 | Activate previous value. |
| 171 | """ |
| 172 | self.activate_next(_previous=True) |
no outgoing calls
no test coverage detected