aegirhall / console-menu

A simple Python menu system for building terminal user interfaces.
MIT License
370 stars 58 forks source link

Custom Exit Text for Main Menu #22

Closed jah-mann closed 5 years ago

jah-mann commented 5 years ago

Hi there,

Thanks for this menu, I enjoy the simplicity. The following is an enhancement request which I have already implemented on my instance of your code:

I'd like to allow custom exit text on the main menu screen. Currently, it is hard coded to show "Exit" as the option, even if we set the Console Menu parameter show_exit_option=False, and then add an ExitItem to the console menu with text="alternative exit text".

I think this would be useful for situations where the console menu does not start up directly upon running a script, so more accurate exit text might be "return to [...]". Also, this could just be useful for anyone who has a different idea of what the exit prompt should look like.

Here is how I would propose you can do this, within console_menu.py > class ExitItem:

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

def show(self, index):
    """
    This class overrides this method
    """
    if self.menu and self.menu.parent:
        self.text = "Return to %s" % self.menu.parent.title
        # Check if menu title ends with menu. (Some menus will include Menu in the name).
        if not self.text.strip().lower().endswith("menu"):
            self.text += " menu"
    else:
        if self.text=='': self.text = "Exit"
    return super(ExitItem, self).show(index)`

On the second to last line (which is line 435 in console_menu.py), add the if statement before deciding to use the default exit prompt text "Exit". This way if someone creates and enables an ExitItem with custom text, it will show that text.

aegirhall commented 5 years ago

The fix will be in v0.6.0.

softwareguycoder commented 5 years ago

I am getting the same issue. According to the readthedocs.io, the exit_option_text parameter to the __init__ should allow me to set the custom Exit menu item text. however, when I attempt to call it, I get a TypeError: __init__() got an unexpected keyword argument 'exit_option_text'.

I am confused, was this parameter there, and then not updated, and then removed? I am utilizing ConsoleMenu with python3. Has this been brought to the python3 version yet?

hisaza commented 5 years ago

I am getting the same issue. According to the readthedocs.io, the exit_option_text parameter to the __init__ should allow me to set the custom Exit menu item text. however, when I attempt to call it, I get a TypeError: __init__() got an unexpected keyword argument 'exit_option_text'.

I am confused, was this parameter there, and then not updated, and then removed? I am utilizing ConsoleMenu with python3. Has this been brought to the python3 version yet?

Yeah I'm having the same issue. The example code shows it can be done but it doesn't seem to be implemented yet. Looking forward for that fix.

aegirhall commented 5 years ago

This issue should be resolved in v0.6.0 release.