2024-06-05 23:31:56 -04:00
|
|
|
from prompt_toolkit.key_binding import KeyBindings, merge_key_bindings
|
|
|
|
from prompt_toolkit.key_binding.bindings.basic import load_basic_bindings
|
|
|
|
from prompt_toolkit.shortcuts import prompt, PromptSession
|
2024-06-06 20:44:35 -04:00
|
|
|
from prompt_toolkit.validation import Validator
|
2024-06-05 23:31:56 -04:00
|
|
|
from rich.table import Table
|
|
|
|
from rich.console import Console
|
|
|
|
|
|
|
|
keybindings = load_basic_bindings()
|
|
|
|
|
|
|
|
@keybindings.add('c-q')
|
|
|
|
def exit_(event):
|
|
|
|
""" Pressing 'c-q' will exit the application. """
|
|
|
|
exit(0)
|
|
|
|
|
|
|
|
@keybindings.add('c-m')
|
|
|
|
def open_menu(event):
|
|
|
|
""" Pressing 'c-m' will open the menu. """
|
|
|
|
show_keybindings_menu()
|
|
|
|
event.app.exit()
|
|
|
|
|
|
|
|
@keybindings.add('enter')
|
|
|
|
def process_input(event):
|
|
|
|
""" Pressing 'enter' will process the input. """
|
|
|
|
print(dir(event))
|
|
|
|
event.app.exit(result=event.current_buffer.text)
|
|
|
|
|
|
|
|
bindings_info = [
|
|
|
|
{"Key": "Ctrl-Q", "Action": "Exit the application"},
|
|
|
|
{"Key": "Ctrl-M", "Action": "Open the menu"},
|
|
|
|
]
|
|
|
|
|
2024-06-06 20:44:35 -04:00
|
|
|
def is_y_n(text):
|
|
|
|
return text in ["y", "n", "Y", "N", ""]
|
|
|
|
|
|
|
|
yes_no_validator = Validator.from_callable(
|
|
|
|
is_y_n,
|
|
|
|
error_message="Please enter 'y' or 'n'",
|
|
|
|
move_cursor_to_end=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
def confirm(message: str = "Confirm?", suffix: str = " (y/n) \u276f ") -> bool:
|
|
|
|
result = prompt(f"{message}{suffix}", validator=yes_no_validator)
|
|
|
|
return result.lower() == "y" or result == ""
|
|
|
|
|
|
|
|
|
2024-06-05 23:31:56 -04:00
|
|
|
console = Console()
|
|
|
|
|
|
|
|
|
|
|
|
def show_keybindings_menu():
|
|
|
|
table = Table(title="Keybindings")
|
|
|
|
|
|
|
|
table.add_column("Key", justify="right", style="cyan", no_wrap=True)
|
|
|
|
table.add_column("Action", style="magenta")
|
|
|
|
|
|
|
|
for binding in bindings_info:
|
|
|
|
table.add_row(binding["Key"], binding["Action"])
|
|
|
|
|
|
|
|
console.print(table)
|
|
|
|
|
|
|
|
|
|
|
|
def main_menu():
|
2024-06-06 20:44:35 -04:00
|
|
|
session = PromptSession("\u276f ", key_bindings=keybindings)
|
2024-06-05 23:31:56 -04:00
|
|
|
while True:
|
|
|
|
show_keybindings_menu()
|
|
|
|
|
|
|
|
user_input = session.prompt()
|
|
|
|
console.print(f"You entered: {user_input}")
|
|
|
|
|
2024-06-06 20:44:35 -04:00
|
|
|
result = confirm("Do you want to continue?")
|
|
|
|
|
|
|
|
if not result:
|
|
|
|
break
|
|
|
|
|
|
|
|
|
2024-06-05 23:31:56 -04:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main_menu()
|