Browse Source

Add more examples

dev
Sebastian Lohff 2 years ago
parent
commit
f20b5eac89
  1. 70
      examples/basic_usage.py
  2. 18
      examples/multiselect.py

70
examples/basic_usage.py

@ -0,0 +1,70 @@ @@ -0,0 +1,70 @@
#!/usr/bin/env python3
from clintermission import CliMenu, CliMenuTheme, CliMenuStyle, CliMenuCursor, cli_select_item
# --- basic menu ---
q = ["Foo", "Bar", "Baz"]
m = CliMenu(q, "Time to choose:\n")
print("You selected", m.get_selection())
print()
# basic menu with an item assigned to each option and detentation of selection
q = [("Foo", 23), ("Bar", 34), ("Baz", 42)]
m = CliMenu(q, "Time to choose:\n", cursor="--->", dedent_selection=True)
print("You selected {} index {} item {}".format(m.get_selection(), m.get_selection_num(), m.get_selection_item()))
print()
# --- themes ---
q = ["Foo", "Bar", "Baz"]
m = CliMenu(q, "Time to choose:\n", style=CliMenuTheme.RED)
print("You selected", m.get_selection())
print()
# --- custom themes ---
style = CliMenuStyle(option_style='blue', highlight_style='cyan', header_style='green')
q = ["Foo", "Bar", "Baz"]
m = CliMenu(q, "Choose in style:\n", style=style)
print("You selected", m.get_selection())
print()
# --- theme defaults ---
CliMenu.set_default_cursor(CliMenuCursor.BULLET)
CliMenu.set_default_style(CliMenuTheme.BOLD_HIGHLIGHT)
q = ["Foo", "Bar", "Baz"]
m = CliMenu(q, "Time to choose:\n")
print("You selected", m.get_selection())
print()
# --- multiple headers ---
m = CliMenu()
m.add_header("Time to choose:\n", indent=False)
m.add_text("=== Category 1 ===")
m.add_option("Foo")
m.add_option("Bar")
m.add_option("Baz")
m.add_header('', indent=False)
m.add_text("=== Category 2 ===")
m.add_option("Cat 1")
m.add_option("Cat 2")
m.add_option("Cat 3")
print("You selected", m.get_selection())
print()
# --- with shortcut ---
try:
result = cli_select_item(["Foo", "Bar", "Baz"], abort_text="Selection faiiiled!")
print("You selected", result)
except ValueError as e:
print(e)
print()
# --- with shortcut, shows no menu when only a single option is provided (can be disabled with return_single=False) ---
result = cli_select_item(["Single Foo"])
print("Directly selected for you as it was the only option:", result)

18
examples/multiselect.py

@ -0,0 +1,18 @@ @@ -0,0 +1,18 @@
#!/usr/bin/env python3
from clintermission import CliMultiMenu, CliMenuCursor
# --- simple multiselect ---
q = [
"Option 1",
"Option 2",
("Option 3 (preselected for your convenience)", "Option 3", True),
"Option 4"
]
m = CliMultiMenu(q, "Make your choice (<space> selects, <return> accepts):\n", cursor=CliMenuCursor.ASCII_ARROW,
unselected_icon="", selected_icon="")
m.get_selection()
print("You selected", m.get_selection())
print("You selected num:", m.get_selection_num())
print("You selected item:", m.get_selection_item())
Loading…
Cancel
Save