alice1017 / SelectMenu

SelectMenu is the input form to choose from menu by arrow keys.
MIT License
22 stars 10 forks source link

Add select_action, select_index, actions/functions and dictionary support #3

Closed rickerp closed 3 years ago

rickerp commented 3 years ago

Features added:

Actions

Add support for actions, so when you call select_action() the respective action (function) for the option selected is called For example:

>>> menu = SelectMenu()
>>> menu.add_choices(['1. Hello!','2. Goodbye.', '0. Exit'], [lambda: print('Hi!'), lambda: print('Bye'), exit])
>>> menu.select_action('Print Menu')
Print Menu (Use arrow keys)
 > 1. Hello!
   2. Goodbye.
   0. Exit
(...)
Hi!

Dictionary support

Add support to add choices and actions by a dictionary

>>> menu = SelectAction()
>>> menu.add_choices({'1. Hello!': lambda: print('Hi!'), '2. Goodbye.': lambda: print('Bye'), '0. Exit': exit})

__init__

Add the possibility to initialize the class with choices and action instead of later add it with add_choices

>>> SelectMenu()
<selectmenu.core.SelectMenu object at 0x7fe14854bee0>
>>> SelectMenu(['1. Hello!','2. Goodbye.', '0. Exit'])
<selectmenu.core.SelectMenu object at 0x7fe148587af0>
>>> SelectMenu(['1. Hello!','2. Goodbye.', '0. Exit'], [lambda: print('Hi!'), lambda: print('Bye'), exit])
<selectmenu.core.SelectMenu object at 0x7fe148614460>
>>> SelectMenu({'1. Hello!': lambda: print('Hi!'), '2. Goodbye.': lambda: print('Bye'), '0. Exit': exit})
<selectmenu.core.SelectMenu object at 0x7fe14854bee0>

select_index

Function that does the same that select() but it returns the index

>>> menu = SelectMenu({'1. Hello!': lambda: print('Hi!'), '2. Goodbye.': lambda: print('Bye'), '0. Exit': exit})
>>> menu.select_index()
   1. Hello!
 > 2. Goodbye.
   0. Exit
(...)
1

select_action

Function that does the same that select() but when the option is selected it calls the respective action/function of that option

>>> menu = SelectMenu({'1. Hello!': lambda: print('Hi!'), '2. Goodbye.': lambda: print('Bye'), '0. Exit': exit})
>>> menu.select_action('Print Menu')
Print Menu (Use arrow keys)
 > 1. Hello!
   2. Goodbye.
   0. Exit
(...)
Hi!
alice1017 commented 3 years ago

@rickerp Thank you for your pull request.

alice1017 commented 3 years ago

@rickerp Could you tell me where do you found out about this project?

rickerp commented 3 years ago

@rickerp Could you tell me where do you found out about this project?

Literally search python terminal menu and this popped out in PyPi

alice1017 commented 3 years ago

@rickerp Could you tell me where do you found out about this project?

Literally search python terminal menu and this popped out in PyPi

I see. thanks.