Compare commits

...

3 Commits

Author SHA1 Message Date
Sebastian Lohff b44a1f3543 Allow disabled options to be created
add_option() now has a "disabled" parameter, which will add the given
option as a simple indented text. This is very similar as calling
add_text(). The advantage of having this as a parameter to add_option()
is that disabled options can now be created from the shorthand function
cli_select_item() by passing each option as a tuple or dict, which has
disabled=True set.

This is a breaking change, as the parameter order of add_option()
changes.
2023-02-16 17:55:01 +01:00
Sebastian Lohff 26412d400f Fix off-by-one in initial pos for menus with no header
Menus without a header had their initial position set to the second
element by default. This was due to most menus having a header and us
calling next_item() unconditionally. Now we first go to the first
focusable item and then call next_item() as many times as needed to get
to the specified initial_pos.
2023-02-16 10:53:02 +01:00
Sebastian Lohff 33aa41ac6e Return with "no success" when no options are given
If the user gave us no options then we used to just raise a ValueError
(due to initial_pos being out of range). In most - if not all - cases
it would be more favorable to act the same way as if the selection has
been aborted, so that's what we're doing now.
2023-02-16 10:52:14 +01:00
1 changed files with 22 additions and 8 deletions

View File

@ -132,12 +132,16 @@ class CliMenu:
for text in title.split('\n'): for text in title.split('\n'):
self._items.append(_CliMenuHeader(text, indent=indent, style=style)) self._items.append(_CliMenuHeader(text, indent=indent, style=style))
def add_option(self, text, item=_EmptyParameter, style=None, highlighted_style=None): def add_option(self, text, item=_EmptyParameter, disabled=False, style=None, highlighted_style=None):
if item == _EmptyParameter: if disabled:
item = text # this is basically a text option and we just throw the item away
self._items.append(_CliMenuOption(text, self._item_num, item=item, self.add_text(title=text, style=style)
style=style, highlighted_style=highlighted_style)) else:
self._item_num += 1 if item == _EmptyParameter:
item = text
opt = _CliMenuOption(text, self._item_num, item=item, style=style, highlighted_style=highlighted_style)
self._items.append(opt)
self._item_num += 1
@property @property
def success(self): def success(self):
@ -272,6 +276,10 @@ class CliMenu:
event.app.exit() event.app.exit()
def _run(self): def _run(self):
if self._item_num == 0:
self._success = False
return
self._preflight() self._preflight()
class MenuColorizer(Processor): class MenuColorizer(Processor):
@ -284,6 +292,7 @@ class CliMenu:
@self._kb.add('q', filter=~is_searching) @self._kb.add('q', filter=~is_searching)
@self._kb.add('c-c') @self._kb.add('c-c')
def quit(event): def quit(event):
self._success = False
event.app.exit() event.app.exit()
@self._kb.add('down', filter=~is_searching) @self._kb.add('down', filter=~is_searching)
@ -336,7 +345,10 @@ class CliMenu:
self._searchbar]) self._searchbar])
# set initial pos # set initial pos
for _ in range(self._initial_pos + 1): while not self._items[self._pos].focusable:
self._pos += 1
for _ in range(self._initial_pos):
self.next_item(1) self.next_item(1)
app = Application(layout=Layout(split), app = Application(layout=Layout(split),
@ -361,9 +373,11 @@ class CliMultiMenu(CliMenu):
self._selection_icons = selection_icons if selection_icons is not None else self.default_selection_icons self._selection_icons = selection_icons if selection_icons is not None else self.default_selection_icons
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
def add_option(self, text, item=_EmptyParameter, selected=False, def add_option(self, text, item=_EmptyParameter, selected=False, disabled=False,
style=None, highlighted_style=None, selected_style=None, selected_highlighted_style=None): style=None, highlighted_style=None, selected_style=None, selected_highlighted_style=None):
super().add_option(text, item, style, highlighted_style=highlighted_style) super().add_option(text, item, style, highlighted_style=highlighted_style)
if disabled:
return
self._items[-1].selected_style = selected_style self._items[-1].selected_style = selected_style
self._items[-1].selected_highlighted_style = selected_highlighted_style self._items[-1].selected_highlighted_style = selected_highlighted_style
if selected: if selected: