示例#1
0
    def __rich_console__(self, console: Console,
                         options: ConsoleOptions) -> RenderResult:
        if not self._lines:
            self.render(console, options)
        style = console.get_style(self.style)
        width = self._render_width or console.width
        height = options.height or console.height
        x, y = self.offset
        window_lines = self._lines[y:y + height]

        if x:

            def width_view(line: list[Segment]) -> list[Segment]:
                _, line = Segment.divide(line, [x, x + width])
                return line

            window_lines = [width_view(line) for line in window_lines]

        missing_lines = len(window_lines) - height
        if missing_lines:
            blank_line = [Segment(" " * width, style), Segment.line()]
            window_lines.extend(blank_line for _ in range(missing_lines))

        new_line = Segment.line()
        for line in window_lines:
            yield from line
            yield new_line
示例#2
0
 def __rich_console__(self, console, options):
     if self.at_end:
         self.start = len(self)
     self.start = max(0, min(self.start, len(self) - self.window_size))
     for i, line in enumerate(self.lines[self.start:len(self)]):
         yield RawSegment(line.text)
         if i < len(self) - 1:
             yield Segment.line()
示例#3
0
 def render(self, x: int, y: int) -> Iterable[Segment]:
     move_to = Control.move_to
     new_line = Segment.line()
     for last, (offset_y, line) in loop_last(enumerate(self.lines, y)):
         yield move_to(x, offset_y).segment
         yield from line
         if not last:
             yield new_line
示例#4
0
    def __rich_console__(
        self, console: Console, options: ConsoleOptions
    ) -> RenderResult:

        new_line = Segment.line()
        for line in self.lines:
            yield from line
            yield new_line
示例#5
0
def test_segments_renderable():
    segments = Segments([Segment("foo")])
    assert list(segments.__rich_console__(None, None)) == [Segment("foo")]

    segments = Segments([Segment("foo")], new_lines=True)
    assert list(segments.__rich_console__(None, None)) == [
        Segment("foo"),
        Segment.line(),
    ]
示例#6
0
 def __rich_console__(self, console: Console,
                      options: ConsoleOptions) -> RenderResult:
     for y in range(0, 5):
         for x in range(options.max_width):
             h = x / options.max_width
             l = 0.1 + ((y / 5) * 0.7)
             r, g, b = colorsys.hls_to_rgb(h, l, 1.0)
             yield Segment(
                 "█",
                 Style(color=Color.from_rgb(r * 255, g * 255, b * 255)))
         yield Segment.line()
示例#7
0
 def __rich_console__(self, console: Console,
                      options: ConsoleOptions) -> RenderResult:
     yield Control.home().segment
     x = self.region.x
     new_line = Segment.line()
     move_to = Control.move_to
     for last, (y, line) in loop_last(enumerate(self.lines, self.region.y)):
         yield move_to(x, y).segment
         yield from line
         if not last:
             yield new_line
示例#8
0
文件: __main__.py 项目: baojd42/rich
 def __rich_console__(self, console: Console,
                      options: ConsoleOptions) -> RenderResult:
     for y in range(0, 5):
         for x in range(options.max_width):
             h = x / options.max_width
             l = 0.1 + ((y / 5) * 0.7)
             r1, g1, b1 = colorsys.hls_to_rgb(h, l, 1.0)
             r2, g2, b2 = colorsys.hls_to_rgb(h, l + 0.7 / 10, 1.0)
             bgcolor = Color.from_rgb(r1 * 255, g1 * 255, b1 * 255)
             color = Color.from_rgb(r2 * 255, g2 * 255, b2 * 255)
             yield Segment("▄", Style(color=color, bgcolor=bgcolor))
         yield Segment.line()
示例#9
0
 def render(self, x: int, y: int, width: int, height: int) -> Iterable[Segment]:
     move_to = Control.move_to
     lines = self.lines[:height]
     new_line = Segment.line()
     for last, (offset_y, (line, dirty)) in loop_last(
         enumerate(zip(lines, self._dirty), y)
     ):
         if dirty:
             yield move_to(x, offset_y).segment
             yield from Segment.adjust_line_length(line, width)
             if not last:
                 yield new_line
     self._dirty[:] = [False] * len(self.lines)
示例#10
0
 def __rich_console__(self, console: Console,
                      options: ConsoleOptions) -> Iterable[Segment]:
     height = console.size.height - 3
     for y in range(0, height):
         for x in range(options.max_width):
             h = x / options.max_width
             l = y / (height + 1)
             r1, g1, b1 = colorsys.hls_to_rgb(h, l, 1.0)
             r2, g2, b2 = colorsys.hls_to_rgb(h, l + (1 / height / 2),
                                              1.0)
             bgcolor = Color.from_rgb(r1 * 255, g1 * 255, b1 * 255)
             color = Color.from_rgb(r2 * 255, g2 * 255, b2 * 255)
             yield Segment("▄", Style(color=color, bgcolor=bgcolor))
         yield Segment.line()
示例#11
0
    def _render(self, console: "Console", options: "ConsoleOptions",
                widths: List[int]) -> "RenderResult":

        if self._dt_rows_end < 0:
            self._dt_rows_end = self.row_count

        table_style = console.get_style(self.style or "")

        border_style = table_style + console.get_style(self.border_style or "")
        rows: List[Tuple[_Cell, ...]] = list(
            zip(*(self._get_cells(column_index, column)
                  for column_index, column in enumerate(self.columns))))
        _box = (self.box.substitute(
            options, safe=pick_bool(self.safe_box, console.safe_box))
                if self.box else None)

        # _box = self.box
        new_line = Segment.line()

        columns = self.columns
        show_header = self.show_header
        show_footer = self.show_footer
        show_edge = self.show_edge
        show_lines = self.show_lines
        leading = self.leading

        _Segment = Segment
        if _box:
            box_segments = [
                (
                    _Segment(_box.head_left, border_style),
                    _Segment(_box.head_right, border_style),
                    _Segment(_box.head_vertical, border_style),
                ),
                (
                    _Segment(_box.foot_left, border_style),
                    _Segment(_box.foot_right, border_style),
                    _Segment(_box.foot_vertical, border_style),
                ),
                (
                    _Segment(_box.mid_left, border_style),
                    _Segment(_box.mid_right, border_style),
                    _Segment(_box.mid_vertical, border_style),
                ),
            ]
            if show_edge:
                yield _Segment(_box.get_top(widths), border_style)
                yield new_line
        else:
            box_segments = []

        get_row_style = self.get_row_style
        get_style = console.get_style

        start = self._dt_rows_start
        end = self._dt_rows_end

        for index, (first, last, row) in enumerate(loop_first_last(rows)):

            # Hope this does the trick
            if index < start or index >= end:
                continue

            header_row = first and show_header
            footer_row = last and show_footer
            max_height = 1
            cells: List[List[List[Segment]]] = []
            if header_row or footer_row:
                row_style = Style.null()
            else:
                row_style = get_style(
                    get_row_style(index - 1 if show_header else index))
            for width, cell, column in zip(widths, row, columns):
                render_options = options.update(
                    width=width,
                    justify=column.justify,
                    no_wrap=column.no_wrap,
                    overflow=column.overflow,
                )
                cell_style = table_style + row_style + get_style(cell.style)
                lines = console.render_lines(cell.renderable,
                                             render_options,
                                             style=cell_style)
                max_height = max(max_height, len(lines))
                cells.append(lines)

            cells[:] = [
                _Segment.set_shape(_cell, width, max_height, style=table_style)
                for width, _cell in zip(widths, cells)
            ]

            if _box:
                if last and show_footer:
                    yield _Segment(
                        _box.get_row(widths, "foot", edge=show_edge),
                        border_style)
                    yield new_line
                left, right, _divider = box_segments[0 if first else
                                                     (2 if last else 1)]

                # If the column divider is whitespace also style it with the row background
                divider = (_divider if _divider.text.strip() else _Segment(
                    _divider.text, row_style.background_style +
                    _divider.style))
                for line_no in range(max_height):
                    if show_edge:
                        yield left
                    for last_cell, rendered_cell in loop_last(cells):
                        yield from rendered_cell[line_no]
                        if not last_cell:
                            yield divider
                    if show_edge:
                        yield right
                    yield new_line
            else:
                for line_no in range(max_height):
                    for rendered_cell in cells:
                        yield from rendered_cell[line_no]
                    yield new_line
            if _box and first and show_header:
                yield _Segment(_box.get_row(widths, "head", edge=show_edge),
                               border_style)
                yield new_line
            if _box and (show_lines or leading):
                if (not last and not (show_footer and index >= len(rows) - 2)
                        and not (show_header and header_row)):
                    if leading:
                        for _ in range(leading):
                            yield _Segment(
                                _box.get_row(widths, "mid", edge=show_edge),
                                border_style,
                            )
                    else:
                        yield _Segment(
                            _box.get_row(widths, "row", edge=show_edge),
                            border_style)
                    yield new_line

        if _box and show_edge:
            yield _Segment(_box.get_bottom(widths), border_style)
            yield new_line
示例#12
0
def test_line():
    assert Segment.line() == Segment("\n")