def _detect_arrow(self, key): arrow = None if name == 'posix' and key == '\x1b': if getch() in '[O': key = getch() nixdict = { 'A': 'A', 'B': 'B', 'C': 'C', 'D': 'D', 'H': 'H', 'F': 'E' } arrow = nixdict.get(key) if not arrow and key in '56': k = getch() if k == '~': arrow = {'5': 'U', '6': 'N'}.get(key) elif name == 'nt' and key == b'\xe0': windict = { b'H': 'A', b'P': 'B', b'M': 'C', b'K': 'D', b'G': 'H', b'O': 'E', b'I': 'U', b'Q': 'N'} key = getch() arrow = windict.get(key) return arrow
def _read_key(self): key = getch() arrow = self._detect_arrow(key) if arrow is not None: key = None return (key, arrow) if name == 'nt' and key is not None: try: key = key.decode() except UnicodeDecodeError: key = None self._error('I cannot recognise the key you just pressed') self.prev_frame = None return (key, arrow)
def activate(self): thread = None ''' Like ``FrameVisualizer``, ``AsciiVisualizer`` won't start on init - if you want to see the output, you have to invoke this method. ''' colorama.init() _clear() self._help() self.prev_frame = None print(Fore.MAGENTA + Style.BRIGHT + 'Press Any Key to begin...') self._detect_arrow(getch()) self._print_frame_diff(0) self.nextc = False self.prevspec = False try: while True: (key, arrow) = self._read_key() self.stop = True if arrow is None and key is None: continue if arrow == 'H': self._print_frame_diff(0) elif arrow == 'E': self._print_frame_diff(len(self.game_controller.jury_states) - 1) elif arrow == 'N': self._print_frame_diff( min( len(self.game_controller.jury_states) - 1, self.frame_number + ceil(len(self.game_controller.jury_states) / 20) ) ) elif arrow == 'U': self._print_frame_diff( max(0, self.frame_number - ceil(len(self.game_controller.jury_states) / 20)) ) elif arrow == 'C' or arrow is None and key in self.key_sets['next']: # next if self.frame_number < self._jury_state_count() - 1: if name == 'posix': self._print_frame_diff(self.frame_number + 1) else: self._print_frame_diff(self.frame_number + 1) else: self._print_frame_diff(self.frame_number) self._error('this is the last frame.') elif arrow == 'D' or arrow is None and key in self.key_sets['prev']: # prev if self.frame_number > 0: self._print_frame_diff(self.frame_number - 1) else: self._print_frame_diff(self.frame_number) self._error('this is the first frame.') elif arrow == 'A' or arrow is None and key in self.key_sets['auto']: while True: reply = self._prompt( 'Enter FPS and, optionally, the frame to stop on (separated by a space)') if reply: cmd = reply.split() try: speed = float(cmd[0]) except ValueError: speed = 0 if speed: jscount = self._jury_state_count() if speed >= 0: addv = 1 time = 1 / speed else: addv = -1 time = -1 / speed if len(cmd) > 1 and cmd[1].isnumeric(): endframe = int(cmd[1]) - 1 else: endframe = jscount try: if not thread or not thread.is_alive(): self.stop = False self.prev_frame = None thread = Thread(target=self.auto, args=(addv, time, jscount, endframe, name)) thread.start() except KeyboardInterrupt: _clear() self._print_frame(self.frame_number) break self._error('The speed must be a real nonzero number') elif arrow is None and key in self.key_sets['quit']: print('Quit') return None elif arrow == 'B' or arrow is None and key in self.key_sets['jump']: while True: frame = self._prompt('Enter frame number') # Add some fool-protection... if frame.isnumeric(): number = int(frame) - 1 if number >= 0 and number < self._jury_state_count(): self._print_frame(number) break else: if number < 0: self._error('Enter a positive integer.') else: self._error('No such frame.') else: self._error('Enter a positive integer.') else: self._print_frame(self.frame_number) self._help() self.prev_frame = None finally: _clear() colorama.deinit()