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
This commit is contained in:
Sebastian Lohff 2020-08-20 02:00:03 +02:00
parent 12552d8e5b
commit 411bb3a7fe
2 changed files with 8 additions and 11 deletions

View File

@ -311,18 +311,15 @@ class CliMenu:
class CliMultiMenu(CliMenu): class CliMultiMenu(CliMenu):
default_selected_icon = '[x]' default_selection_icons = ('[x]', '[ ]')
default_unselected_icon = '[ ]'
@classmethod @classmethod
def set_default_selector_icons(cls, selected_icon, unselected_icon): def set_default_selector_icons(cls, selection_icons):
cls.default_selected_icon = selected_icon cls.default_selection_icons = selection_icons
cls.default_unselected_icon = unselected_icon
def __init__(self, *args, selected_icon=None, unselected_icon=None, **kwargs): def __init__(self, *args, selection_icons=None, **kwargs):
self._multi_selected = [] self._multi_selected = []
self._selected_icon = selected_icon if selected_icon is not None else self.default_selected_icon self._selection_icons = selection_icons if selection_icons is not None else self.default_selection_icons
self._unselected_icon = unselected_icon if unselected_icon is not None else self.default_unselected_icon
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
def add_option(self, text, item=None, selected=False): def add_option(self, text, item=None, selected=False):
@ -360,9 +357,9 @@ class CliMultiMenu(CliMenu):
def _transform_prefix(self, item, lineno, prefix): def _transform_prefix(self, item, lineno, prefix):
if item.focusable: if item.focusable:
if lineno in self._multi_selected: if lineno in self._multi_selected:
icon = self._selected_icon icon = self._selection_icons[0]
else: else:
icon = self._unselected_icon icon = self._selection_icons[1]
return "{}{} ".format(prefix, icon) return "{}{} ".format(prefix, icon)
else: else:
return prefix return prefix

View File

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