😎 Improve modularisation
This commit is contained in:
parent
2cde5fa49f
commit
36db673a6b
@ -7,6 +7,11 @@ import sys
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
def clear(instance, y: int, x: int):
|
||||||
|
# Clear the line at the screen at position y, x
|
||||||
|
instance.screen.insstr(y, x, " " * (instance.width - x))
|
||||||
|
|
||||||
|
|
||||||
def gracefully_exit():
|
def gracefully_exit():
|
||||||
# Close the curses window
|
# Close the curses window
|
||||||
curses.endwin()
|
curses.endwin()
|
||||||
@ -15,6 +20,14 @@ def gracefully_exit():
|
|||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
|
|
||||||
|
def pause(message: str):
|
||||||
|
# End the curses session
|
||||||
|
curses.endwin()
|
||||||
|
|
||||||
|
# Print the message and wait for enter key
|
||||||
|
input(f"{message}\n\n Press enter to continue...")
|
||||||
|
|
||||||
|
|
||||||
def load_json(file: str) -> dict:
|
def load_json(file: str) -> dict:
|
||||||
# Load the json file with read permissions
|
# Load the json file with read permissions
|
||||||
with open(file, "r") as f:
|
with open(file, "r") as f:
|
||||||
@ -27,14 +40,10 @@ def load_config() -> dict:
|
|||||||
|
|
||||||
# Only if the config file exists, attempt to load it
|
# Only if the config file exists, attempt to load it
|
||||||
if os.path.exists(config_file):
|
if os.path.exists(config_file):
|
||||||
|
# Return the loaded config as a dictionary
|
||||||
return load_json(config_file)
|
return load_json(config_file)
|
||||||
|
|
||||||
|
|
||||||
def clear(instance, y: int, x: int):
|
|
||||||
# Clear the line at the screen at position y, x
|
|
||||||
instance.screen.insstr(y, x, " " * (instance.width - x))
|
|
||||||
|
|
||||||
|
|
||||||
def welcome(screen):
|
def welcome(screen):
|
||||||
# Get window height and width
|
# Get window height and width
|
||||||
height, width = screen.getmaxyx()
|
height, width = screen.getmaxyx()
|
||||||
@ -121,23 +130,16 @@ def prompt(instance, message: str, color: int = 1) -> (list, None):
|
|||||||
instance.screen.addstr(instance.height - 1, len(message), input_text)
|
instance.screen.addstr(instance.height - 1, len(message), input_text)
|
||||||
|
|
||||||
|
|
||||||
def goodbye(instance):
|
def press_key_to_continue(instance, message: str):
|
||||||
choice = prompt(instance, "Really quit lambda? (y/n): ", 11)
|
# Hide the cursor
|
||||||
if choice and choice[0] == "y":
|
curses.curs_set(0)
|
||||||
curses.endwin()
|
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
else:
|
# Clear the bottom of the screen
|
||||||
clear(instance, instance.height - 1, 0)
|
clear(instance, instance.height - 1, 0)
|
||||||
|
|
||||||
|
|
||||||
def error(instance, message: str):
|
|
||||||
# Parse the error message
|
|
||||||
error_message = f"ERROR: {message}"
|
|
||||||
|
|
||||||
# Write the entire message to the screen
|
# Write the entire message to the screen
|
||||||
instance.screen.addstr(instance.height - 1, 0, f"ERROR: {message}", curses.color_pair(3))
|
instance.screen.addstr(instance.height - 1, 0, message, curses.color_pair(3))
|
||||||
instance.screen.addstr(instance.height - 1, len(error_message) + 1, f"(press any key)")
|
instance.screen.addstr(instance.height - 1, len(message) + 1, f"(press any key)")
|
||||||
|
|
||||||
# Wait for a keypress
|
# Wait for a keypress
|
||||||
instance.screen.getch()
|
instance.screen.getch()
|
||||||
@ -145,9 +147,20 @@ def error(instance, message: str):
|
|||||||
# Clear the bottom of the screen
|
# Clear the bottom of the screen
|
||||||
clear(instance, instance.height - 1, 0)
|
clear(instance, instance.height - 1, 0)
|
||||||
|
|
||||||
|
# Show the cursor
|
||||||
|
curses.curs_set(1)
|
||||||
|
|
||||||
|
|
||||||
|
def error(instance, message: str):
|
||||||
|
# Parse the error message
|
||||||
|
error_message = f"ERROR: {message}"
|
||||||
|
|
||||||
|
# Create a prompt
|
||||||
|
press_key_to_continue(instance, error_message)
|
||||||
|
|
||||||
|
|
||||||
def fatal_error(exception: Exception):
|
def fatal_error(exception: Exception):
|
||||||
# Clean up the screen
|
# End the curses session
|
||||||
curses.endwin()
|
curses.endwin()
|
||||||
|
|
||||||
# Print the error message and traceback
|
# Print the error message and traceback
|
||||||
@ -157,3 +170,13 @@ def fatal_error(exception: Exception):
|
|||||||
|
|
||||||
# Exit, with an error exit code
|
# Exit, with an error exit code
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
|
|
||||||
|
def goodbye(instance):
|
||||||
|
choice = prompt(instance, "Really quit lambda? (y/n): ", 11)
|
||||||
|
if choice and choice[0] == "y":
|
||||||
|
curses.endwin()
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
else:
|
||||||
|
clear(instance, instance.height - 1, 0)
|
||||||
|
Loading…
Reference in New Issue
Block a user