aegirhall / console-menu

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

add support for selecting by pressing number or letter #73

Open Quantlyx opened 2 years ago

Quantlyx commented 2 years ago

[Label: enhancement]

Maybe you could add support for the following: When you are in a menu you just have to press the associated number or letter for the option to be selected. So not like with writing the number down and pressing enter.

BTW: big fan of the package, keep it up!

croisez commented 1 year ago

Hi, I needed this new feature, so I coded it. Would you like to test my code, and possibly to merge it ?

croisez commented 1 year ago

Here is a test.py script, to see how I implemented it. Look at "exit_menu_char" option to ConsoleMenu() object. Look also at "menu_char" option to all other Items() objects, like SubmenuItem() or FunctionItem(), for instance.

from consolemenu import *
from consolemenu.items import *

def MyTestFunction():
     Screen().println("In function MyTestFunction()")

mainMenu = ConsoleMenu("My test Menu", "main menu",   clear_screen=False, exit_menu_char='q')
subMenu  = ConsoleMenu("My test subMenu", "sub menu", clear_screen=False, exit_menu_char='q')

mainMenu_item1 = SubmenuItem("menu item1",  submenu=subMenu, menu_char='c')
mainMenu_item2 = FunctionItem("function test item", MyTestFunction, menu_char='t')

mainMenu.append_item(mainMenu_item1)
mainMenu.append_item(mainMenu_item2)

mainMenu.show()
croisez commented 1 year ago

Here is the result of running this test.py file:

  ┌─────────────────────────────────────────────────────────────────────────┐
  │                                                                         │
  │  My test Menu                                                           │
  │                                                                         │
  │  main menu                                                              │
  │                                                                         │
  │                                                                         │
  │    c - menu item1                                                       │
  │    t - function test item                                               │
  │    q - Exit                                                             │
  │                                                                         │
  │                                                                         │
  └─────────────────────────────────────────────────────────────────────────┘
  >> c

  ┌─────────────────────────────────────────────────────────────────────────┐
  │                                                                         │
  │  My test subMenu                                                        │
  │                                                                         │
  │  sub menu                                                               │
  │                                                                         │
  │                                                                         │
  │    q - Exit                                                             │
  │                                                                         │
  │                                                                         │
  └─────────────────────────────────────────────────────────────────────────┘
  >> q

  ┌─────────────────────────────────────────────────────────────────────────┐
  │                                                                         │
  │  My test Menu                                                           │
  │                                                                         │
  │  main menu                                                              │
  │                                                                         │
  │                                                                         │
  │    c - menu item1                                                       │
  │    t - function test item                                               │
  │    q - Exit                                                             │
  │                                                                         │
  │                                                                         │
  └─────────────────────────────────────────────────────────────────────────┘
  >> t
In function MyTestFunction()

  ┌─────────────────────────────────────────────────────────────────────────┐
  │                                                                         │
  │  My test Menu                                                           │
  │                                                                         │
  │  main menu                                                              │
  │                                                                         │
  │                                                                         │
  │    c - menu item1                                                       │
  │    t - function test item                                               │
  │    q - Exit                                                             │
  │                                                                         │
  │                                                                         │
  └─────────────────────────────────────────────────────────────────────────┘
  >> q