From f20b5eac896d9fd16cae002cb8c8974b524152c3 Mon Sep 17 00:00:00 2001 From: Sebastian Lohff Date: Sun, 16 Aug 2020 14:24:47 +0200 Subject: [PATCH] Add more examples --- examples/basic_usage.py | 70 +++++++++++++++++++++++++++++++++++++++++ examples/multiselect.py | 18 +++++++++++ 2 files changed, 88 insertions(+) create mode 100644 examples/basic_usage.py create mode 100644 examples/multiselect.py diff --git a/examples/basic_usage.py b/examples/basic_usage.py new file mode 100644 index 0000000..b14b2ef --- /dev/null +++ b/examples/basic_usage.py @@ -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) diff --git a/examples/multiselect.py b/examples/multiselect.py new file mode 100644 index 0000000..08b8849 --- /dev/null +++ b/examples/multiselect.py @@ -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 ( selects, 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())