def readline(cursor, max_input=20): """A rudimentary readline implementation.""" text = u'' exit_dict = dict([(label, False) for label in EXIT_LABELS]) term = cursor.term while True: inp = term.inkey() if inp.code == term.KEY_ENTER: exit_dict[ENTER] = True break elif inp.code == term.KEY_ESCAPE: text = None exit_dict[ESCAPE] = True break elif not inp.is_sequence and len(text) < max_input: text += inp cursor = csr.right_of(cursor, 1) echo_at_cell(cursor, inp) elif inp.code in (term.KEY_BACKSPACE, term.KEY_DELETE): if len(text) > 0: text = text[:-1] echo_at_cell(cursor, u' ') cursor = csr.left_of(cursor, 1) return text, exit_dict
def handle_input(self, inp): term = self.term if inp == chr(3) or self.exit_signal_received: if self.logger != None: self.logger.log("handle_input: exiting") # ^c exits return -1 elif self.command != None: self.handle_command() if self.logger != None: self.logger.log("handle_input: calling handle_command, command: {}".format(self.command)) return 0 elif inp.code == term.KEY_ESCAPE: self.read_command() if self.logger != None: self.logger.log("handle_input: called read_command, command: {}".format(self.command)) return 0 elif inp == chr(12): # ^l refreshes self.draw() if self.logger != None: self.logger.log("handle_input: refreshing, calling draw") return 0 elif inp.code in [term.KEY_BACKSPACE, term.KEY_DELETE]: echo_remove_cursor(self.focus_cursor, self.focus_screen, logger=self.logger) self.focus_cursor = csr.left_of(self.focus_cursor, 1, screen=self.focus_screen, logger=self.logger) self.focus_screen.delete_cell(self.focus_cursor, logger=self.logger) return 0 else: echo_remove_cursor(self.focus_cursor, self.focus_screen, logger=self.logger) self.focus_cursor, move_made = csr.lookup_move( inp.code, self.focus_cursor, screen=self.focus_screen, logger=self.logger ) if move_made: return 0 if input_filter(inp): self.logger.log("Attempting to write {} to screen.".format(str(inp))) self.focus_screen.write_cell(self.focus_cursor, str(inp), logger=self.logger) echo_remove_cursor(self.focus_cursor, self.focus_screen, logger=self.logger) self.focus_cursor = csr.right_of(self.focus_cursor, 1, screen=self.focus_screen, logger=self.logger) return 0 else: self.logger.log("{} not considered input.".format(str(inp))) return 1
def read_command(self): self.reading_command = True self.draw_command_bar(status=WAITING_FOR_CMD) if self.logger != None: self.logger.add_tag("read_command") self.draw_command_bar(status=WAITING_FOR_CMD) self.input_bar_cursor = csr.right_of(self.input_bar_cursor, 5) inp, exit_dict = readline(self.input_bar_cursor, max_input=self.term.width - 5) if exit_dict["enter"]: self.command = [substr for substr in inp.split(" ") if substr not in ["", None]] self.set_status(EDITING) self.input_bar_text = u"" self.input_bar_cursor = csr.home(self.input_bar_cursor) echo_at_cell(self.input_bar_cursor, self.term.clear_eol) self.reading_command = False if self.logger != None: self.logger.remove_tag()