예제 #1
0
 def get_style_at(self, x: int, y: int) -> Style:
     try:
         line = self.lines[y]
     except IndexError:
         return Style.null()
     end = 0
     for segment in line:
         end += cell_len(segment.text)
         if x < end:
             return segment.style or Style.null()
     return Style.null()
예제 #2
0
def test_without_color():
    style = Style(bold=True, color="red", bgcolor="blue")
    colorless_style = style.without_color
    assert colorless_style.color == None
    assert colorless_style.bgcolor == None
    assert colorless_style.bold == True
    null_style = Style.null()
    assert null_style.without_color == null_style
예제 #3
0
 def get_style_at(self, x: int, y: int) -> Style:
     try:
         widget, region = self.get_widget_at(x, y)
     except NoWidget:
         return Style.null()
     if widget not in self.regions:
         return Style.null()
     lines = widget._get_lines()
     x -= region.x
     y -= region.y
     line = lines[y]
     end = 0
     for segment in line:
         end += segment.cell_length
         if x < end:
             return segment.style or Style.null()
     return Style.null()
예제 #4
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
예제 #5
0
def test_empty():
    assert Style.null() == Style()