def speed_in_cpm(text, start_time): """Calculate typing speed in CPM. Args: text (list): List of words from sample text. start_time (float): The time when user starts typing the sample text. Returns: string: Speed in CPM up to 2 decimal places. """ time_taken = timer.get_elapsed_minutes_since_first_keypress(start_time) cpm = 60 * len(" ".join(text)) / 5 / time_taken return "{0:.2f}".format(cpm)
def update_state(self, win): """Report on typing session results. Args: win (any): Curses window. """ win.addstr(self.line_count, 0, " " * self.window_width) win.addstr(self.line_count + 2, 0, " " * self.window_width) win.addstr(self.line_count + 4, 0, " " * self.window_width) if len(self.current_word) >= self.current_word_limit: win.addstr(self.line_count, 0, self.current_word, curses.color_pair(2)) else: win.addstr(self.line_count, 0, self.current_word) win.addstr(2, 0, self.text, curses.A_BOLD) win.addstr(2, 0, self.text[0 : len(self.current_string)], curses.A_DIM) index = first_index_at_which_strings_differ(self.current_string, self.text) win.addstr( 2 + index // self.window_width, index % self.window_width, self.text[index : len(self.current_string)], curses.color_pair(2), ) if index == len(self.text): curses.curs_set(0) win.addstr(self.line_count, 0, " Your typing speed is ") if self.mode == 0: self.current_speed_wpm = speed_in_wpm(self.tokens, self.start_time) wrongly_typed_chars = self.total_chars_typed - len( self.text_without_spaces ) self.accuracy = accuracy(self.total_chars_typed, wrongly_typed_chars) self.time_taken = get_elapsed_minutes_since_first_keypress( self.start_time ) win.addstr(" " + self.current_speed_wpm + " ", curses.color_pair(6)) win.addstr(" WPM ") win.addstr(self.window_height - 1, 0, " " * (self.window_width - 1)) win.addstr(self.line_count + 2, 1, " Enter ", curses.color_pair(7)) win.addstr(" to see replay, ") win.addstr(" Tab ", curses.color_pair(7)) win.addstr(" to retry ") self.print_stats(win) if self.mode == 0: self.mode = 1 for k in range(len(self.key_strokes) - 1, 0, -1): self.key_strokes[k][0] -= self.key_strokes[k - 1][0] self.key_strokes[0][0] = 0 self.first_key_pressed = False self.end_time = time.time() self.current_string = "" self.current_word = "" self.i = 0 self.start_time = 0 if not self.test_complete: win.refresh() save_history( self.text_id, self.current_speed_wpm, "{:.2f}".format(self.accuracy) ) self.test_complete = True win.refresh()