Velocity/core/utils.py

52 lines
1.4 KiB
Python
Raw Normal View History

2022-03-14 07:21:55 +00:00
from sys import exit
import curses
2022-03-17 21:47:19 +00:00
def refresh_window_size(screen, data):
# Get the height and width of the screen
data["height"], data["width"] = screen.getmaxyx()
# Return the data as changes may have been made
return data
def clear_line(screen, data, line):
# Clear the specified line
screen.addstr(line, 0, " " * (data["width"] - 1), curses.color_pair(1))
2022-03-14 07:21:55 +00:00
2022-03-15 22:12:52 +00:00
2022-03-17 21:47:19 +00:00
def prompt(screen, data, text):
2022-03-14 07:21:55 +00:00
# Print the prompt
2022-03-17 21:47:19 +00:00
screen.addstr(data["height"] - 1, 0, text, curses.color_pair(11))
2022-03-14 07:21:55 +00:00
# Wait for and capture a key press from the user
2022-03-17 21:47:19 +00:00
return screen.getch()
def goodbye(screen, data):
# Create a goodbye prompt
key = prompt(screen, data, "Really quit? (y or n): ")
# Clear the bottom line
clear_line(screen, data, data["height"] - 1)
2022-03-14 07:21:55 +00:00
if key == ord("y"):
# Only exit if the key was "y", a confirmation
exit()
# Clear the bottom line again
2022-03-17 21:47:19 +00:00
clear_line(screen, data, data["height"] - 1)
2022-03-14 07:21:55 +00:00
2022-03-17 21:47:19 +00:00
def error(screen, data, error_msg):
2022-03-15 15:37:38 +00:00
# Print the error message to the bottom line
error_msg = f"ERROR: {error_msg}"
2022-03-17 21:47:19 +00:00
screen.addstr(data["height"] - 1, 0, error_msg, curses.color_pair(3))
screen.addstr(data["height"] - 1, len(error_msg) + 1, "(press any key) ", curses.color_pair(1))
2022-03-15 15:37:38 +00:00
# Wait for a key to be pressed
2022-03-17 21:47:19 +00:00
screen.getch()
2022-03-15 15:37:38 +00:00
# Clear the bottom line
2022-03-17 21:47:19 +00:00
clear_line(screen, data, data["height"] - 1)