Skip to content

Commit

Permalink
This branch implements feature request from aegirhall#73
Browse files Browse the repository at this point in the history
  • Loading branch information
JP-MCA committed Feb 27, 2023
1 parent 5155ae2 commit cc83e56
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 17 deletions.
26 changes: 19 additions & 7 deletions consolemenu/console_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ConsoleMenu(object):

def __init__(self, title=None, subtitle=None, screen=None, formatter=None,
prologue_text=None, epilogue_text=None, clear_screen=True,
show_exit_option=True, exit_option_text='Exit'):
show_exit_option=True, exit_option_text='Exit', exit_menu_char=None):
if screen is None:
screen = Screen()
self.screen = screen
Expand All @@ -65,7 +65,7 @@ def __init__(self, title=None, subtitle=None, screen=None, formatter=None,

self.parent = None

self.exit_item = ExitItem(menu=self, text=exit_option_text)
self.exit_item = ExitItem(menu=self, text=exit_option_text, menu_char=exit_menu_char)

self.current_option = 0
self.selected_option = -1
Expand Down Expand Up @@ -313,6 +313,12 @@ def process_user_input(self):
self.should_exit = True
return

for i, cm in enumerate(self.items):
if cm.menu_char == user_input:
self.current_option = i
self.select()
return user_input

try:
num = int(user_input)
except Exception:
Expand Down Expand Up @@ -403,8 +409,8 @@ class MenuItem(object):
"""
A generic menu item
"""

def __init__(self, text, menu=None, should_exit=False):
def __init__(self, text, menu=None, should_exit=False, menu_char=None):
"""
:ivar str text: The text shown for this menu item
:ivar ConsoleMenu menu: The menu to which this item belongs
Expand All @@ -414,6 +420,7 @@ def __init__(self, text, menu=None, should_exit=False):
self.menu = menu
self.should_exit = should_exit
self.index_item_separator = " - "
self.menu_char = menu_char

def __str__(self):
return "%s %s" % (self.menu.get_title(), self.get_text())
Expand All @@ -432,7 +439,12 @@ def show(self, index):
:return: The representation of the item to be shown in a menu
:rtype: str
"""
return "%2d%s%s" % (index + 1, self.index_item_separator, self.get_text())
self.index = index + 1
if self.menu_char == None:
ret = "%2d%s%s" % (index + 1, self.index_item_separator, self.get_text())
else:
ret = " %c%s%s" % (self.menu_char, self.index_item_separator, self.get_text())
return ret

def set_up(self):
"""
Expand Down Expand Up @@ -472,8 +484,8 @@ class ExitItem(MenuItem):
Used to exit the current menu. Handled by :class:`consolemenu.ConsoleMenu`
"""

def __init__(self, text="Exit", menu=None):
super(ExitItem, self).__init__(text=text, menu=menu, should_exit=True)
def __init__(self, text="Exit", menu=None, menu_char=None):
super(ExitItem, self).__init__(text=text, menu=menu, should_exit=True, menu_char=menu_char)

def show(self, index, available_width=None):
"""
Expand Down
4 changes: 2 additions & 2 deletions consolemenu/items/command_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ class CommandItem(ExternalItem):
A menu item to execute a console command
"""

def __init__(self, text, command, arguments=None, menu=None, should_exit=False):
def __init__(self, text, command, arguments=None, menu=None, should_exit=False, menu_char=None):
"""
:ivar str text: The text shown for this menu item
:ivar str command: The console command to be executed
:ivar list[str] arguments: An optional list of string arguments to be passed to the command
:ivar ConsoleMenu menu: The menu to which this item belongs
:ivar bool should_exit: Whether the menu should exit once this item's action is done
"""
super(CommandItem, self).__init__(text=text, menu=menu, should_exit=should_exit)
super(CommandItem, self).__init__(text=text, menu=menu, should_exit=should_exit, menu_char=menu_char)
self.command = command

if arguments:
Expand Down
4 changes: 2 additions & 2 deletions consolemenu/items/external_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ class ExternalItem(MenuItem):
Should probably be subclassed.
"""

def __init__(self, text, menu=None, should_exit=False):
def __init__(self, text, menu=None, should_exit=False, menu_char=None):
# Here so Sphinx doesn't copy extraneous info from the superclass's docstring
super(ExternalItem, self).__init__(text=text, menu=menu, should_exit=should_exit)
super(ExternalItem, self).__init__(text=text, menu=menu, should_exit=should_exit, menu_char=menu_char)

def set_up(self):
"""
Expand Down
4 changes: 2 additions & 2 deletions consolemenu/items/function_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class FunctionItem(ExternalItem):
A menu item to call a Python function
"""

def __init__(self, text, function, args=None, kwargs=None, menu=None, should_exit=False):
def __init__(self, text, function, args=None, kwargs=None, menu=None, should_exit=False, menu_char=None):
"""
:ivar str text: The text shown for this menu item
:ivar function: The function to be called
Expand All @@ -15,7 +15,7 @@ def __init__(self, text, function, args=None, kwargs=None, menu=None, should_exi
:ivar ConsoleMenu menu: The menu to which this item belongs
:ivar bool should_exit: Whether the menu should exit once this item's action is done
"""
super(FunctionItem, self).__init__(text=text, menu=menu, should_exit=should_exit)
super(FunctionItem, self).__init__(text=text, menu=menu, should_exit=should_exit, menu_char=menu_char)

self.function = function

Expand Down
4 changes: 2 additions & 2 deletions consolemenu/items/selection_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ class SelectionItem(MenuItem):
The item type used in :class:`consolemenu.SelectionMenu`
"""

def __init__(self, text, index, menu=None):
def __init__(self, text, index, menu=None, menu_char=None):
"""
:ivar str text: The text shown for this menu item
:ivar int index: The index of this item in the list used to initialize the :class:`consolemenu.SelectionMenu`
:ivar ConsoleMenu menu: The menu to which this item belongs
"""
super(SelectionItem, self).__init__(text=text, menu=menu, should_exit=True)
super(SelectionItem, self).__init__(text=text, menu=menu, should_exit=True, menu_char=menu_char)
self.index = index

def get_return(self):
Expand Down
4 changes: 2 additions & 2 deletions consolemenu/items/submenu_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ class SubmenuItem(MenuItem):
A menu item to open a submenu
"""

def __init__(self, text, submenu, menu=None, should_exit=False):
def __init__(self, text, submenu, menu=None, should_exit=False, menu_char=None):
"""
:ivar str text: The text shown for this menu item
:ivar ConsoleMenu submenu: The submenu to be opened when this item is selected
:ivar ConsoleMenu menu: The menu to which this item belongs
:ivar bool should_exit: Whether the menu should exit once this item's action is done
"""
super(SubmenuItem, self).__init__(text=text, menu=menu, should_exit=should_exit)
super(SubmenuItem, self).__init__(text=text, menu=menu, should_exit=should_exit, menu_char=menu_char)

self.submenu = submenu
if menu:
Expand Down

0 comments on commit cc83e56

Please sign in to comment.