def render(self) -> Screen: render_screen = Screen(self.rect.width, self.rect.height) top_edge = (self.theme[Border.TOPLEFT] + self.theme[Border.HORIZONTAL] * self.screen.width + self.theme[Border.TOPRIGHT]) if self.title != "": top_edge = (self.theme[Border.TOPLEFT] + " {title} ".format(title=self.title).center( self.screen.width, self.theme[Border.HORIZONTAL]) + self.theme[Border.TOPRIGHT]) bottom_edge = (self.theme[Border.BOTTOMLEFT] + self.theme[Border.HORIZONTAL] * self.screen.width + self.theme[Border.BOTTOMRIGHT]) render_screen.draw((0, 0), top_edge) for y in range(self.screen.height): render_screen.draw((0, y + 1), self.theme[Border.VERTICAL]) render_screen.draw(Point(self.screen.width + 1, y + 1), self.theme[Border.VERTICAL]) render_screen.draw((0, self.rect.height - 1), bottom_edge) render_screen.draw(Point(1, 1), self.screen) return render_screen
def get_parent_center(self): """ Get the center of the parent as a Point """ center_x = self.parent.width // 2 center_y = self.parent.height // 2 return Point(center_x, center_y)
def _draw_screencell_array(self, offset, screencell_array): offset = Point(offset) try: screen_line = self.contents[offset.y] except IndexError: # trying to draw off screen return for index, screencell in enumerate(screencell_array): try: screen_line[offset.x + index] = screencell except IndexError: pass
def __init__(self, width, height, parent, offset=Point(0, 0)): """ Defined by a width and a height. """ self._width = width self._height = height self._offset = offset if isinstance(parent, Screen): self.parent = parent else: raise TypeError("Parent viewport must be a Screen")
def render(self, width, height) -> Screen: render_screen = Screen(width, height) scroll_threshold = int(height / 2) sub_contents = self.contents if self.pointer < height - scroll_threshold: sub_contents = self.contents[:height] elif self.pointer > len(self.contents) - scroll_threshold: sub_contents = self.contents[-height:] else: sub_contents = self.contents[self.pointer - height + scroll_threshold : self.pointer + scroll_threshold] for index, item in enumerate(sub_contents): render_screen = self._render_item(Point(0, index), item, render_screen) if sub_contents[0] != self.contents[0]: render_screen.draw((-1, 0), unicodedata.lookup("UPWARDS ARROW")) if sub_contents[-1] != self.contents[-1]: render_screen.draw((-1, -1), unicodedata.lookup("DOWNWARDS ARROW")) return render_screen
def draw(self, offset, content, underline=False, reverse=False, bold=False, colour=None): attributes = TextAttributes(underline=underline, reverse=reverse, bold=bold, colour=colour) offset = Point(offset) if isinstance(content, (int, float)): content = str(content) if isinstance(content, Screen): self._draw_screen(offset, content, attributes=attributes) return if isinstance(content, list): for index, text in enumerate(content): line_offset = copy.copy( offset) # needs to be copied, not referenced line_offset.y += index self._draw_string(line_offset, str(text), attributes=attributes) return if isinstance(content, str): self._draw_string(offset, content, attributes=attributes) return raise TypeError( "Can't draw object of type {type}".format(type=type(content)))
def get_rendered_screen(self, width, height): screen = Screen(width, height) screen_width = screen.width column_widths = self.get_column_widths(screen_width) column_headers = self.get_column_headers() header_line = "" under_line = "" for width, header in zip(column_widths, column_headers): header_line += header.center(width - 1)[: width - 1] + Border.NORMAL[Border.VERTICAL] under_line += Border.NORMAL[Border.HORIZONTAL] * (width - 1) + Border.NORMAL[Border.INTERSECTION] screen.draw((0, 0), header_line) screen.draw((0, 1), under_line) for row_index in range(height): line = "" for column_index, column_width in enumerate(column_widths): line += ( " " + self.columns[column_index].formatData(self.get_cell(column_index, row_index), column_width) + Border.NORMAL[Border.VERTICAL] ) line = line screen.draw(Point(0, row_index + 2), line) return screen
def __init__(self, origin_x, origin_y, width, height): self.origin = Point(origin_x, origin_y) self._width = width self._height = height
def _draw_screen(self, offset, screen, attributes=None): for index, line in enumerate(screen.as_list()): line_offset = Point(offset.x, offset.y + index) for cell in line: cell.attributes |= attributes self._draw_screencell_array(line_offset, line)
def __init__(self, width, height): self.size = Point(width, height) self.clear()