From 47a63d6b9b77b5173ff25522bae7d34804fad785 Mon Sep 17 00:00:00 2001 From: Doman Date: Sun, 6 Mar 2022 18:24:00 +0100 Subject: [PATCH] stats working again, which means restarting and exiting as well --- fasttyper/application.py | 12 +++++++++--- fasttyper/buffer.py | 38 +++++++++++++++++++++++++++++++++++++- fasttyper/stats.py | 22 +++++++++++++++++++--- 3 files changed, 65 insertions(+), 7 deletions(-) diff --git a/fasttyper/application.py b/fasttyper/application.py index e4f2e99..298c1d7 100644 --- a/fasttyper/application.py +++ b/fasttyper/application.py @@ -7,14 +7,17 @@ class Application: self.listener = listener self.buffer = buffer self.config = config - self.finished = False self.silent_exit = False + @property + def finished(self): + return self.buffer.stats.finished + def start(self): pass def running(self): - return True + return self.buffer.stats.running() def action(self, screen): try: @@ -23,13 +26,16 @@ class Application: except StoppingSignal as e: if e.silent: self.silent_exit = True + self.buffer.stats.signal_stop() def summarize(self): if self.finished: self.buffer.stats.summarize(self.config.get("summary_template")) self.buffer.stats.export_to_datafile(self.config.get("summary_datafile")) try: - readchar.readchar() + c = readchar.readchar() + if ord(c) == 3: + raise KeyboardInterrupt except KeyboardInterrupt: sys.exit(1) diff --git a/fasttyper/buffer.py b/fasttyper/buffer.py index f9187b7..73d3e98 100644 --- a/fasttyper/buffer.py +++ b/fasttyper/buffer.py @@ -21,6 +21,7 @@ class Buffer: self.stats = stats self.text_box.set_buffer(self) + self.stats.set_buffer(self) def _write(self, char): while self.current_word >= len(self.user_words): @@ -45,11 +46,19 @@ class Buffer: self.user_words[self.current_word] += char self.current_char += 1 + if self.current_word == self.total_words - 1 and self.current_char >= len( + self.reference_words[self.current_word] + ): + self.stats.signal_stop(True) + def _next_word(self): self.current_word += 1 self.current_char = 0 self.stats.signal_valid() # space is a char after all + if self.current_word >= self.total_words: + self.stats.signal_stop(True) + def _del_char(self): try: self.user_words[self.current_word] = self.user_words[self.current_word][:-1] @@ -81,7 +90,8 @@ class Buffer: elif action == Action.del_word: self._del_word() - self.text_box.update_current_word(self.current_word) + if self.stats.running(): + self.text_box.update_current_word(self.current_word) def get_word(self, index): reference_word = self.reference_words[index] @@ -100,3 +110,29 @@ class Buffer: word += [(c, CharType.reference) for c in reference_word[len(word) :]] return word + + @property + def correct_words(self): + count = 0 + + for i, w in enumerate(self.user_words): + if i == self.total_words: + break + + if w == self.reference_words[i]: + count += 1 + + return count + + @property + def incorrect_words(self): + count = 0 + + for i, w in enumerate(self.user_words): + if i == self.total_words: + break + + if w != self.reference_words[i]: + count += 1 + + return count diff --git a/fasttyper/stats.py b/fasttyper/stats.py index 6b9fe40..853a676 100644 --- a/fasttyper/stats.py +++ b/fasttyper/stats.py @@ -7,14 +7,21 @@ import pathlib class Stats: def __init__(self): - self.correct_words = 0 self.correct_chars = 0 - self.incorrect_words = 0 self.incorrect_chars = 0 self.start_dtime = None self.stop_dtime = None + self.buffer = None + self.finished = False + + def set_buffer(self, buffer): + self.buffer = buffer + + def running(self): + return self.stop_dtime is None + def signal_running(self): if self.start_dtime is None: self.start_dtime = datetime.now() @@ -27,8 +34,17 @@ class Stats: self.signal_running() self.incorrect_chars += 1 - def signal_stop(self): + def signal_stop(self, finished=False): self.stop_dtime = datetime.now() + self.finished = finished + + @property + def correct_words(self): + return self.buffer.correct_words + + @property + def incorrect_words(self): + return self.buffer.incorrect_words @property def total_seconds(self):