No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

basic_usage.py 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/usr/bin/env python3
  2. from clintermission import CliMenu, CliMenuTheme, CliMenuStyle, CliMenuCursor, cli_select_item
  3. # --- basic menu ---
  4. q = ["Foo", "Bar", "Baz"]
  5. m = CliMenu(q, "Time to choose:\n")
  6. print("You selected", m.get_selection())
  7. print()
  8. # basic menu with an item assigned to each option and detentation of selection
  9. q = [("Foo", 23), ("Bar", 34), ("Baz", 42)]
  10. m = CliMenu(q, "Time to choose:\n", cursor="--->", dedent_selection=True)
  11. print("You selected {} index {} item {}".format(m.get_selection(), m.get_selection_num(), m.get_selection_item()))
  12. print()
  13. # --- themes ---
  14. q = ["Foo", "Bar", "Baz"]
  15. m = CliMenu(q, "Time to choose:\n", style=CliMenuTheme.RED)
  16. print("You selected", m.get_selection())
  17. print()
  18. # --- custom themes ---
  19. style = CliMenuStyle(option='blue', highlighted='cyan', text='green')
  20. q = ["Foo", "Bar", "Baz"]
  21. m = CliMenu(q, "Choose in style:\n", style=style)
  22. print("You selected", m.get_selection())
  23. print()
  24. # --- theme defaults ---
  25. CliMenu.set_default_cursor(CliMenuCursor.BULLET)
  26. CliMenu.set_default_style(CliMenuTheme.BOLD_HIGHLIGHT)
  27. q = ["Foo", "Bar", "Baz"]
  28. m = CliMenu(q, "Time to choose:\n")
  29. print("You selected", m.get_selection())
  30. print()
  31. # --- multiple headers ---
  32. m = CliMenu()
  33. m.add_header("Time to choose:\n", indent=False)
  34. m.add_text("=== Category 1 ===")
  35. m.add_option("Foo")
  36. m.add_option("Bar")
  37. m.add_option("Baz")
  38. m.add_header('', indent=False)
  39. m.add_text("=== Category 2 ===")
  40. m.add_option("Cat 1")
  41. m.add_option("Cat 2")
  42. m.add_option("Cat 3")
  43. print("You selected", m.get_selection())
  44. print()
  45. # --- with shortcut ---
  46. try:
  47. result = cli_select_item(["Foo", "Bar", "Baz"], abort_text="Selection faiiiled!")
  48. print("You selected", result)
  49. except ValueError as e:
  50. print(e)
  51. print()
  52. # --- with shortcut, shows no menu when only a single option is provided (can be disabled with return_single=False) ---
  53. result = cli_select_item(["Single Foo"])
  54. print("Directly selected for you as it was the only option:", result)
  55. print()
  56. # --- prefix/suffix ---
  57. q = ["Foo", "Bar", "Baz"]
  58. m = CliMenu(q, "Time to choose:\n", option_prefix=' <<<', option_suffix='>>>')
  59. print("You selected", m.get_selection())
  60. print()