Esempio n. 1
0
    def render(self, size, focus):
        maxcols, maxrows = size
        rows = self._original_widget.rows((maxcols, ), focus)
        if rows <= maxrows:
            # no clipping, so just fill as normal
            return super().render(size, focus)

        # limit scroll_top to top of last page
        self.scroll_top = min(self.scroll_top, rows - maxrows)

        ocanv = self._original_widget.render((maxcols, ), focus)
        if ocanv.cursor is not None:
            # ensure selected field is within scroll window
            cx, cy = ocanv.cursor
            if self.scroll_top <= cy < self.scroll_top + maxrows:
                pass  # already in view
            elif cy < maxrows / 2:
                self.scroll_top = 0
            elif cy > rows - maxrows / 2:
                self.scroll_top = rows - maxrows
            else:
                self.scroll_top = int(cy - maxrows / 2)

        # calculate top / bottom values for pad_trim_top_bottom
        top = -self.scroll_top
        bottom = -(rows - self.scroll_top - maxrows)

        canv = CompositeCanvas(ocanv)
        canv.pad_trim_top_bottom(top, bottom)

        if rows != 0:
            # calculate top and bottom of scroll thumb as percentage of maxrows
            sf = maxrows / rows
            thumb_top = int(self.scroll_top * sf)
            thumb_bottom = int((rows - (self.scroll_top + maxrows)) * sf)
            thumb_size = maxrows - thumb_top - thumb_bottom
        else:
            thumb_size = maxrows
            thumb_top = thumb_bottom = 0
        # create canvases to draw scrollbar
        scroll_top = SolidCanvas(self.trough_char, 1, thumb_top)
        scroll_thumb = SolidCanvas(self.thumb_char, 1, thumb_size)
        scroll_bottom = SolidCanvas(self.trough_char, 1, thumb_bottom)
        scroll_bar = CanvasCombine([
            (scroll_top, None, False),
            (scroll_thumb, None, False),
            (scroll_bottom, None, False),
        ])
        return CanvasJoin([
            (canv, None, True, maxcols - 1),
            (scroll_bar, None, False, 1),
        ])
Esempio n. 2
0
    def render(self, size, focus=False):
        """
        Render the graph

        :param focus: ignored
        :type size: tuple
        :return:
        """
        self.last_size = size
        matrix = self.__get_matrix(size[0], size[1])

        rows = []
        for row in range(0, size[1]):
            line = []
            groups = ["".join(grp) for _num, grp in groupby(matrix[row])]
            for chunk in groups:
                color = self.colors[int(chunk[0])]
                char = self.chars[int(chunk[0])]
                line.append((color, len(chunk) * char))
            rows.append((Text(line).render((size[0],)), None, False))
        return CanvasCombine(rows)