Skip to content

Commit

Permalink
Allow appending multiple items to a menu aegirhallGH-68
Browse files Browse the repository at this point in the history
Modify `ConsoleMenu.append_item()` to accept any number of items which
are to be appended to the menu, enabling users to write cleaner menu
setup code. Also add a test for appending zero, one, and more than
one items.
  • Loading branch information
truthless-dev committed May 2, 2024
1 parent b9b2b88 commit 2f989a3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
13 changes: 8 additions & 5 deletions consolemenu/console_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,20 @@ def selected_item(self):
else:
return None

def append_item(self, item):
def append_item(self, *items):
"""
Add an item to the end of the menu before the exit item.
Add one or more items to the end of the menu before the exit item.
Args:
item (MenuItem): The item to be added.
items (list[MenuItem]): The item to be added.
"""
if len(items) == 0:
return
did_remove = self.remove_exit()
item.menu = self
self.items.append(item)
for item in items:
item.menu = self
self.items.append(item)
if did_remove:
self.add_exit()

Expand Down
12 changes: 12 additions & 0 deletions test/test_console_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ def test_currently_active_menu(self):
menu2.join(timeout=5)
menu1.join(timeout=5)

def test_append_menu_items(self):
menu = ConsoleMenu("menu1", "test_currently_active_menu_1")
item1 = MenuItem(text='itemText', menu=menu)
item2 = MenuItem(text='itemText2', menu=menu)
item3 = MenuItem(text='itemText3', menu=menu)
menu.append_item()
menu.append_item(item1)
menu.append_item(item2, item3)
self.assertIn(item1, menu.items)
self.assertIn(item2, menu.items)
self.assertIn(item3, menu.items)

def test_remove_menu_item(self):
menu = ConsoleMenu("menu1", "test_currently_active_menu_1")
item1 = MenuItem(text='itemText', menu=menu)
Expand Down

0 comments on commit 2f989a3

Please sign in to comment.