From e98332455a2a231c1b41adc9fc11dcd3e88ee275 Mon Sep 17 00:00:00 2001 From: Ratatoskr Date: Sat, 2 Dec 2023 13:45:50 +0100 Subject: [PATCH] =?UTF-8?q?L=C3=B6schen=20geht=20noch=20nicht?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tipptrainer.py | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/tipptrainer.py b/tipptrainer.py index 7894902..7ea8579 100644 --- a/tipptrainer.py +++ b/tipptrainer.py @@ -3,6 +3,7 @@ from curses import wrapper import time import random import locale +import sys # Set the locale to support UTF-8 locale.setlocale(locale.LC_ALL, '') @@ -66,7 +67,7 @@ def start_scr(stdscr): elif key == curses.KEY_DOWN and current_row_idx < len(menu) - 1: current_row_idx += 1 elif key == curses.KEY_ENTER or key in [10, 13] and current_row_idx == 0: - wpm_test(stdscr) + wpm_test(stdscr, filename) elif key == curses.KEY_ENTER or key in [10, 13] and current_row_idx == 1: quit() print_menu(stdscr, current_row_idx) @@ -86,15 +87,15 @@ def display_text(stdscr, target, current, wpm=0, cpm=0): stdscr.addstr(1, i, char, color) -def load_text(): - with open("text.txt", "r", encoding="utf-8") as f: +def load_text(filename): + with open(filename, "r", encoding="utf-8") as f: lines = f.readlines() return random.choice(lines).strip() # -*- coding: utf-8 -*- -def wpm_test(stdscr): - target_text = load_text() +def wpm_test(stdscr, filename): + target_text = load_text(filename) current_text = [] wpm = 0 cpm = 0 @@ -132,17 +133,23 @@ def wpm_test(stdscr): except KeyboardInterrupt: pass # Ignore KeyboardInterrupt to exit gracefully -def main(stdscr): +def main(stdscr, filename): 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) + curses.init_pair(5, curses.COLOR_WHITE, curses.COLOR_BLACK) start_scr(stdscr) while True: - wpm_test(stdscr) - stdscr.addstr(5, 0, "Press [ENTER] key to continue or [ESC] to go back!") + wpm_test(stdscr, filename) + stdscr.addstr(6, 0, "Press [ENTER] key to continue or [ESC] to go back!") key = stdscr.getkey() - if ord(key) == 27: + if ord(key) == 28: break -wrapper(main) +if __name__ == "__main__": + if len(sys.argv) != 2: + print("Usage: python tipptrainer.py ") + sys.exit(1) + + filename = sys.argv[1] + wrapper(main, filename)