Add confirmation popup when delete

This commit is contained in:
Digital Studium 2024-12-17 19:44:39 +03:00
parent 2044c33ee3
commit 7b74528cca
1 changed files with 27 additions and 2 deletions

29
kls
View File

@ -219,6 +219,31 @@ def handle_horizontal_navigation(key: str, menu: Menu):
globals().update(selected_menu=next_menu) globals().update(selected_menu=next_menu)
def confirm_action(message: str) -> bool:
"""Display a confirmation popup and return True if the user confirms."""
rows, cols = SCREEN.getmaxyx() # Get screen size
popup_height = 5
popup_width = len(message) + 10
start_y = (rows - popup_height) // 2
start_x = (cols - popup_width) // 2
# Create a new window for the popup
popup = curses.newwin(popup_height, popup_width, start_y, start_x)
popup.box() # Draw a border around the popup
popup.addstr(2, 2, message) # Display the message
popup.addstr(3, 2, "Press 'y' to confirm, 'n' to cancel")
# Refresh the popup window and wait for input
popup.refresh()
while True:
key = popup.getkey() # Wait for a key press
if key.lower() == 'y':
return True
elif key.lower() == 'n':
return False
def catch_input(menu: Menu): def catch_input(menu: Menu):
while True: # refresh third menu until key pressed while True: # refresh third menu until key pressed
try: try:
@ -233,9 +258,9 @@ def catch_input(menu: Menu):
handle_vertical_navigation(key, menu) handle_vertical_navigation(key, menu)
elif key == "KEY_MOUSE": elif key == "KEY_MOUSE":
handle_mouse(menu) handle_mouse(menu)
elif key in KEY_BINDINGS.keys(): elif key == "KEY_DC" and confirm_action("Are you sure you want to delete this resource?"):
handle_key_bindings(key, namespace(), api_resource(), resource()) handle_key_bindings(key, namespace(), api_resource(), resource())
elif curses.ascii.unctrl(key) in KEY_BINDINGS.keys(): elif key != "KEY_DC" and curses.ascii.unctrl(key) in KEY_BINDINGS.keys():
handle_key_bindings(curses.ascii.unctrl(key), namespace(), api_resource(), resource()) handle_key_bindings(curses.ascii.unctrl(key), namespace(), api_resource(), resource())
elif key in ["/", "\x1b", "KEY_BACKSPACE", elif key in ["/", "\x1b", "KEY_BACKSPACE",
"\x08"] or key.isalnum() or key == "-": # \x1b - escape, \x08 - backspace "\x08"] or key.isalnum() or key == "-": # \x1b - escape, \x08 - backspace