Package: clean-menu
Clean MenuTable of contenttable of contentInstallationNoteKnown issuesUseCreate an instanceBind actions to optionsUse the menuInstallationYou can install the package by running the following command :python3 -m pip install --upgrade clean-menuNotePlease notethat the package is currently inbetaversion and may encounterbugsorunexpected behavioursfor whichI am not responsible.Known issues :Linux :The keys are captured but not interceptednavigate in the menu browse your terminal historywhen you pressenterto validate, it can trigger one of your previous commandsI tried the same fix than windows but I haven't tested it yet.Windows :To disable key propagation, I set up a filter using thewin32_event_filterkwargIt will blockarrows up and down,enter, andspace(only if you use thespacekey to validate)
These issues are all related, and a safe way to use it would be to override the defaultvalidation_keyand set it tospace.MacOS :I actually don't have a mac to test it, but I think it won't work as expected, because filters are not the same as windows. Furthermore, keyboard monitoring requires elevated (root) privileges.UseCreate an instanceSimply import theMenuobject from the package into your script and start use it.fromclean_menuimportMenumenu=Menu(title,# menu titleoptions,# list of optionssubtitle="",# menu subtitlesubtitle_color=None,# subtitle colorexit_text="Exit",# exit textexit_function=sys.exit,# exit function called whenart_title=True,# enable/disable ascii art titletitle_font="",# ascii art title fontdefault_pointer_index=0,# default option index in option listmargin="    ",# characters before options, better result when longer than pointertitle_color="red",# title colortext_color="white",# color for not pointed optionspointer_style=None,# pointer style, if None, no pointerpointer_color="green",# pointer colorpointed_text_color="white",# pointed option text colorpointed_background_color="None",# pointed option background colorvalidation_key="enter",# key to validate an option)so the following code :fromclean_menuimportMenumenu=Menu("Test",["Option 1","Option 2","Option 3"],title_font="rounded",title_color="blue",margin="        ",pointer_style=["==>","<=="],pointer_color="red",pointed_background_color="white",pointed_text_color="green",exit_text="Quit me forever...",text_color="magenta",validation_key="enter",)will render this :Click to see the image (redirection)Bind actions to options:You can attach a function or method to each action in the menu, except for theexit, which is  handled by the parameterexit_function.
To do so, you just have to call thebind()method, with the index of the option and the function as parameters.menu.bind(0,lambda:print("Option 1"))menu.bind(1,lambda:print("Option 2"))menu.bind(2,lambda:print("Option 3"))in case the given index is not handled, you will get an error like this :menu.bind(3,lambda:print("Option 4"))=======================================BindError:IndexoutofrangeListofavailableoptionstoassignfunctions:0:Option1# you will see all the options1:Option2# available for binding and their2:Option3# index to specify as parameterTraceback(mostrecentcalllast):File"c:\Users\mathe\Documents\code\cleanMenu\clean-menu\main.py",line158,in<module>example()File"c:\Users\mathe\Documents\code\cleanMenu\clean-menu\main.py",line152,inexamplemenu.bind(3,lambda:print("Option 4"))File"c:\Users\mathe\Documents\code\cleanMenu\clean-menu\main.py",line114,inbindraiseIndexErrorIndexErrorUse the menuYou have to options to use the menu :You can bind functions to options and let the menu execute them with therunmethodyou can just execute the menu screen to get the index of the selected optionexamples :menu.run()# and that's pretty much all you need to do...index=menu.get_index()# will return the index of the selected optionprint(menu.options[index])# should display the option you selected