def key_printer(self, win, key): """Print required key to terminal. Args: win (any): Curses window object. key (string): Individual characters are returned as 1-character strings, and special keys such as function keys return longer strings containing a key name such as KEY_UP or ^G. """ # Reset test if is_escape(key): self.reset_test() elif is_ctrl_c(key): sys.exit(0) # Handle resizing elif is_resize(key): self.resize(win) # Check for backspace elif is_backspace(key): self.erase_key() # Ignore spaces at the start of the word (Plover support) elif key == " ": if self.current_word != "": self.check_word() elif is_valid_initial_key(key): self.appendkey(key) # Update state of window self.update_state(win)
def replay(self, win): """Play out a recording of the users last session. Args: win (any): Curses window. """ win.clear() self.print_stats(win) win.addstr(self.line_count + 2, 0, " " * self.window_width) curses.curs_set(1) win.addstr( 0, int(self.window_width) - 14, " " + str(self.current_speed_wpm) + " ", curses.color_pair(5), ) win.addstr(" WPM ") self.setup_print(win) win.timeout(10) for j in self.key_strokes: time.sleep(j[0]) key = self.keyinput(win) if is_escape(key) or is_ctrl_c(key): sys.exit(0) self.key_printer(win, j[1]) win.timeout(100)
def main(self, win): """Respond to user inputs. This is where the infinite loop is executed to continuously serve events. Args: win (any): Curses window object. """ # Initialize windows self.initialize(win) while True: # Typing mode key = self.keyinput(win) if is_escape(key) and not self.first_key_pressed: sys.exit(0) if is_ctrl_c(key): sys.exit(0) if is_left_arrow_key(key) and not self.first_key_pressed: self.switch_text(win, -1) if is_right_arrow_key(key) and not self.first_key_pressed: self.switch_text(win, 1) # Test mode if self.mode == 0: self.typing_mode(win, key) # Again mode elif self.mode == 1 and is_tab(key): win.clear() self.reset_test() self.setup_print(win) self.update_state(win) # Replay mode elif self.mode == 1 and is_enter(key): # Start replay if enter key is pressed self.replay(win) # Share result on Twitter elif self.mode == 1 and is_ctrl_t(key): # Opens twitter with pre-typed result self.share_result() # Refresh for changes to show up on window win.refresh()