From d24c201bcbb25546a95b2f3e343381e62262968f Mon Sep 17 00:00:00 2001 From: MesonProgrammer Date: Sun, 2 Feb 2025 18:53:45 +0100 Subject: [PATCH 1/2] Add Keyboard Interrupt error handling Cleanly exit the program by pressing Ctrl+C. --- tutorial.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tutorial.py b/tutorial.py index dd5a8a9..40faf29 100644 --- a/tutorial.py +++ b/tutorial.py @@ -76,4 +76,8 @@ def main(stdscr): if ord(key) == 27: break -wrapper(main) +try: + wrapper(main) +except KeyboardInterrupt: + print("Ended typing test via keyboard interrupt.") + \ No newline at end of file From 36db0ce9b58a4af6eafebe62573535f6850d5a4f Mon Sep 17 00:00:00 2001 From: MesonProgrammer Date: Sun, 2 Feb 2025 18:56:00 +0100 Subject: [PATCH 2/2] Add return annotations to functions --- tutorial.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tutorial.py b/tutorial.py index 40faf29..21468fb 100644 --- a/tutorial.py +++ b/tutorial.py @@ -4,14 +4,14 @@ import random -def start_screen(stdscr): +def start_screen(stdscr) -> None: stdscr.clear() stdscr.addstr("Welcome to the Speed Typing Test!") stdscr.addstr("\nPress any key to begin!") stdscr.refresh() stdscr.getkey() -def display_text(stdscr, target, current, wpm=0): +def display_text(stdscr, target, current, wpm=0) -> None: stdscr.addstr(target) stdscr.addstr(1, 0, f"WPM: {wpm}") @@ -23,12 +23,12 @@ def display_text(stdscr, target, current, wpm=0): stdscr.addstr(0, i, char, color) -def load_text(): +def load_text() -> str: with open("text.txt", "r") as f: lines = f.readlines() return random.choice(lines).strip() -def wpm_test(stdscr): +def wpm_test(stdscr) -> None: target_text = load_text() current_text = [] wpm = 0 @@ -62,7 +62,7 @@ def wpm_test(stdscr): current_text.append(key) -def main(stdscr): +def main(stdscr) -> None: curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK) curses.init_pair(2, curses.COLOR_RED, curses.COLOR_BLACK) curses.init_pair(3, curses.COLOR_WHITE, curses.COLOR_BLACK)