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 draw(self): """Draw the screen.""" if self.invalid == True: self.stack_layers() start_r, start_c = self.start end_r, end_c = self.end term = self.term _screen = self._screen for r, c in sorted(self._screen): if start_r <= r <= end_r and start_c <= c <= end_c: echo_at_cell(Cursor(r, c, term), _screen[r, c])
def draw_command_bar(self, status=EDITING): # draw separator echo_at_cell(self.command_bar_separator_cursor, DOUBLE_HORIZONTAL * self.term.width) # write status bar self.set_status(status) if status == WAITING_FOR_CMD: self.input_bar_text = self.term.bold_white(u"cmd: ") self.input_bar_cursor = csr.home(self.input_bar_cursor) echo_at_cell(self.input_bar_cursor, self.input_bar_text) echo_at_cell(self.status_bar_cursor, self.status_bar_text + self.status_descriptor) echo_at_cell(self.input_bar_cursor, self.input_bar_text)
def set_status(self, status): echo_at_cell(csr.home(self.status_bar_cursor, logger=self.logger), self.term.clear_eol) if status == EDITING: fscreen = self.focus_screen min_row, min_col = fscreen.origin max_row, max_col = fscreen.origin + fscreen.display_size - 1 offset_row, offset_col = fscreen.offset fcursor = self.focus_cursor self.status_descriptor = "{} | ({}:{}:{}, {}:{}:{}) + ({}, {})".format( self.focus_screen_index, min_row, fcursor.r, max_row, min_col, fcursor.c, max_col, offset_row, offset_col, ) elif status == WAITING_FOR_CMD: self.status_descriptor = "~"
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()
def draw_separators(self): # print intersection if self.horizontal_separator_end_col != None and self.vertical_separator_col != None: if self.num_divisions == 3: echo_at_cell( csr.Cursor(self.horizontal_separator_row, self.vertical_separator_col, self.term), HEAVY_THREE_INTERSECTION, ) if self.num_divisions == 4: echo_at_cell( csr.Cursor(self.horizontal_separator_row, self.vertical_separator_col, self.term), HEAVY_FOUR_INTERSECTION, ) # print vertical separators if self.vertical_separator_col != None: display_height = self.term.height - self.command_bar_height c = self.vertical_separator_col reserved_r = self.horizontal_separator_row term = self.term for i in xrange(display_height): if i == reserved_r: continue else: echo_at_cell(csr.Cursor(i, c, term), HEAVY_VERTICAL) # print horizontal separators if self.horizontal_separator_row != None: reserved_c = self.vertical_separator_col r = self.horizontal_separator_row term = self.term for i in xrange(self.horizontal_separator_start_col, self.horizontal_separator_end_col): if i == reserved_c: continue else: echo_at_cell(csr.Cursor(r, i, term), HEAVY_HORIZONTAL)