Always use text as item if no item was specified

When an option is passed without an item attached we now always use the
label as item. This was the behaviour for __init__() and is now also the
behaviour of add_option().
This commit is contained in:
Sebastian Lohff 2020-08-28 00:26:32 +02:00
parent 85c3c5c947
commit 22307bc4ae
1 changed files with 9 additions and 3 deletions

View File

@ -70,6 +70,10 @@ class CliMenuTheme:
BOLD_HIGHLIGHT = CliMenuStyle(header_style='bold', highlight_style='bold fg:black bg:white')
class _EmptyParameter:
pass
class CliMenu:
default_style = CliMenuTheme.BASIC
default_cursor = CliMenuCursor.TRIANGLE
@ -110,7 +114,7 @@ class CliMenu:
elif isinstance(option, dict):
self.add_option(**option)
elif isinstance(option, str):
self.add_option(option, option)
self.add_option(option)
else:
raise ValueError("Option needs to be either tuple, dict or string, found '{}' of type {}"
.format(option, type(option)))
@ -122,7 +126,9 @@ class CliMenu:
for text in title.split('\n'):
self._items.append(_CliMenuHeader(text, indent=indent))
def add_option(self, text, item=None):
def add_option(self, text, item=_EmptyParameter):
if item == _EmptyParameter:
item = text
self._items.append(_CliMenuOption(text, self._item_num, item=item))
self._item_num += 1
@ -334,7 +340,7 @@ class CliMultiMenu(CliMenu):
self._selection_icons = selection_icons if selection_icons is not None else self.default_selection_icons
super().__init__(*args, **kwargs)
def add_option(self, text, item=None, selected=False):
def add_option(self, text, item=_EmptyParameter, selected=False):
super().add_option(text, item)
if selected:
self._multi_selected.append(len(self._items) - 1)