Browse Source

Group selection icons in CliMultiMenu API

The select/unselect icons of a CliMultiMenu will in nearly all cases be
changed together, therefore it makes sense to let the API be less
verbose for this case.

This is an API breaking change for CliMultiMenu
Sebastian Lohff 1 year ago
parent
commit
411bb3a7fe
2 changed files with 8 additions and 11 deletions
  1. 7
    10
      clintermission/climenu.py
  2. 1
    1
      examples/multiselect.py

+ 7
- 10
clintermission/climenu.py View File

@@ -311,18 +311,15 @@ class CliMenu:
311 311
 
312 312
 
313 313
 class CliMultiMenu(CliMenu):
314
-    default_selected_icon = '[x]'
315
-    default_unselected_icon = '[ ]'
314
+    default_selection_icons = ('[x]', '[ ]')
316 315
 
317 316
     @classmethod
318
-    def set_default_selector_icons(cls, selected_icon, unselected_icon):
319
-        cls.default_selected_icon = selected_icon
320
-        cls.default_unselected_icon = unselected_icon
317
+    def set_default_selector_icons(cls, selection_icons):
318
+        cls.default_selection_icons = selection_icons
321 319
 
322
-    def __init__(self, *args, selected_icon=None, unselected_icon=None, **kwargs):
320
+    def __init__(self, *args, selection_icons=None, **kwargs):
323 321
         self._multi_selected = []
324
-        self._selected_icon = selected_icon if selected_icon is not None else self.default_selected_icon
325
-        self._unselected_icon = unselected_icon if unselected_icon is not None else self.default_unselected_icon
322
+        self._selection_icons = selection_icons if selection_icons is not None else self.default_selection_icons
326 323
         super().__init__(*args, **kwargs)
327 324
 
328 325
     def add_option(self, text, item=None, selected=False):
@@ -360,9 +357,9 @@ class CliMultiMenu(CliMenu):
360 357
     def _transform_prefix(self, item, lineno, prefix):
361 358
         if item.focusable:
362 359
             if lineno in self._multi_selected:
363
-                icon = self._selected_icon
360
+                icon = self._selection_icons[0]
364 361
             else:
365
-                icon = self._unselected_icon
362
+                icon = self._selection_icons[1]
366 363
             return "{}{} ".format(prefix, icon)
367 364
         else:
368 365
             return prefix

+ 1
- 1
examples/multiselect.py View File

@@ -10,7 +10,7 @@ q = [
10 10
     "Option 4"
11 11
 ]
12 12
 m = CliMultiMenu(q, "Make your choice (<space> selects, <return> accepts):\n", cursor=CliMenuCursor.ASCII_ARROW,
13
-                 unselected_icon="✖", selected_icon="✔")
13
+                 selection_icons=("✔", "✖"))
14 14
 
15 15
 print("You selected", m.get_selection())
16 16
 print("You selected num:", m.get_selection_num())

Loading…
Cancel
Save