示例#1
0
    def table(self):
        """Return a large string of the entire table ready to be printed to the terminal."""
        widths = [
            c + self.padding_left + self.padding_right
            for c in self.column_widths
        ]
        inner_widths, inner_heights = width_and_alignment.max_dimensions(
            self.table_data)
        final_table_data = list()

        # Append top border.
        if self.outer_border:
            final_table_data.append(''.join(
                build_border(
                    widths, self.CHAR_HORIZONTAL, self.CHAR_CORNER_UPPER_LEFT,
                    self.CHAR_INTERSECT_TOP if self.inner_column_border else
                    '', self.CHAR_CORNER_UPPER_RIGHT, self.title)))

        # Build table body.
        indexes = range(len(self.table_data))
        for i in indexes:
            for line in self.gen_cell_lines(self.table_data[i], inner_widths,
                                            inner_heights[i]):
                final_table_data.append(''.join(line))

            # Insert row separator.
            if i == indexes[-1]:
                continue  # Last row.
            if self.inner_row_border or (self.inner_heading_row_border
                                         and i == 0):
                final_table_data.append(''.join(
                    build_border(
                        widths, self.CHAR_HORIZONTAL,
                        self.CHAR_INTERSECT_LEFT if self.outer_border else '',
                        self.CHAR_INTERSECT_CENTER if self.inner_column_border
                        else '', self.CHAR_INTERSECT_RIGHT
                        if self.outer_border else '')))

            if i == indexes[-2] and self.inner_footing_row_border:
                final_table_data.append(''.join(
                    build_border(
                        widths, self.CHAR_HORIZONTAL,
                        self.CHAR_INTERSECT_LEFT if self.outer_border else '',
                        self.CHAR_INTERSECT_CENTER if self.inner_column_border
                        else '', self.CHAR_INTERSECT_RIGHT
                        if self.outer_border else '')))

        # Append bottom border.
        if self.outer_border:
            final_table_data.append(''.join(
                build_border(
                    widths, self.CHAR_HORIZONTAL, self.CHAR_CORNER_LOWER_LEFT,
                    self.CHAR_INTERSECT_BOTTOM if self.inner_column_border else
                    '', self.CHAR_CORNER_LOWER_RIGHT)))

        return '\n'.join(final_table_data)
示例#2
0
    def table(self):
        """Return a large string of the entire table ready to be printed to the terminal."""
        widths = [c + self.padding_left + self.padding_right for c in self.column_widths]
        inner_widths, inner_heights = width_and_alignment.max_dimensions(self.table_data)
        final_table_data = list()

        # Append top border.
        if self.outer_border:
            final_table_data.append(''.join(build_border(
                widths,
                self.CHAR_HORIZONTAL,
                self.CHAR_CORNER_UPPER_LEFT,
                self.CHAR_INTERSECT_TOP if self.inner_column_border else '',
                self.CHAR_CORNER_UPPER_RIGHT,
                self.title
            )))

        # Build table body.
        indexes = range(len(self.table_data))
        for i in indexes:
            for line in self.gen_cell_lines(self.table_data[i], inner_widths, inner_heights[i]):
                final_table_data.append(''.join(line))

            # Insert row separator.
            if i == indexes[-1]:
                continue  # Last row.
            if self.inner_row_border or (self.inner_heading_row_border and i == 0):
                final_table_data.append(''.join(build_border(
                    widths,
                    self.CHAR_HORIZONTAL,
                    self.CHAR_INTERSECT_LEFT if self.outer_border else '',
                    self.CHAR_INTERSECT_CENTER if self.inner_column_border else '',
                    self.CHAR_INTERSECT_RIGHT if self.outer_border else ''
                )))

            if i == indexes[-2] and self.inner_footing_row_border:
                final_table_data.append(''.join(build_border(
                    widths,
                    self.CHAR_HORIZONTAL,
                    self.CHAR_INTERSECT_LEFT if self.outer_border else '',
                    self.CHAR_INTERSECT_CENTER if self.inner_column_border else '',
                    self.CHAR_INTERSECT_RIGHT if self.outer_border else ''
                )))

        # Append bottom border.
        if self.outer_border:
            final_table_data.append(''.join(build_border(
                widths,
                self.CHAR_HORIZONTAL,
                self.CHAR_CORNER_LOWER_LEFT,
                self.CHAR_INTERSECT_BOTTOM if self.inner_column_border else '',
                self.CHAR_CORNER_LOWER_RIGHT
            )))

        return '\n'.join(final_table_data)
def test_non_string(outer_widths, title, expected):
    """Test with non-string values.

    :param iter outer_widths: List of integers representing column widths with padding.
    :param title: Title in border.
    :param str expected: Expected output.
    """
    actual = build_border(outer_widths, '-', '<', '+', '>', title=title)
    assert ''.join(actual) == expected
def test_no_intersect(outer_widths, left, right, expected):
    """Test with no column dividers.

    :param iter outer_widths: List of integers representing column widths.
    :param str left: Left border.
    :param str right: Right border.
    :param str expected: Expected output.
    """
    if left and right:
        expected = left + expected + right
    actual = build_border(outer_widths, '-', left, '', right, title='Applications')
    assert ''.join(actual) == expected
def test_no_title(outer_widths, horizontal, left, intersect, right, expected):
    """Test without title.

    :param iter outer_widths: List of integers representing column widths with padding.
    :param str horizontal: Character to stretch across each column.
    :param str left: Left border.
    :param str intersect: Column separator.
    :param str right: Right border.
    :param str expected: Expected output.
    """
    actual = build_border(outer_widths, horizontal, left, intersect, right)
    assert ''.join(actual) == expected
def test_first_column_fit(outer_widths, left, intersect, right, expected):
    """Test with title that fits in the first column.

    :param iter outer_widths: List of integers representing column widths with padding.
    :param str left: Left border.
    :param str intersect: Column separator.
    :param str right: Right border.
    :param str expected: Expected output.
    """
    if left and right:
        expected = left + expected + right
    actual = build_border(outer_widths, '-', left, intersect, right, title='Applications')
    assert ''.join(actual) == expected
def test_rtl(outer_widths, left, intersect, right, expected):
    """Test with RTL characters in title.

    :param iter outer_widths: List of integers representing column widths.
    :param str left: Left border.
    :param str intersect: Column separator.
    :param str right: Right border.
    :param str expected: Expected output.
    """
    if left and right:
        expected = left + expected + right
    actual = build_border(outer_widths, '-', left, intersect, right, title=u'معرب')
    assert ''.join(actual) == expected
def test_colors(outer_widths, left, intersect, right, title, expected):
    """Test with color title characters.

    :param iter outer_widths: List of integers representing column widths with padding.
    :param str left: Left border.
    :param str intersect: Column separator.
    :param str right: Right border.
    :param title: Title in border with color codes.
    :param str expected: Expected output.
    """
    if left and right:
        expected = left + expected + right
    actual = build_border(outer_widths, '-', left, intersect, right, title=title)
    assert ''.join(actual) == expected
示例#9
0
    def horizontal_border(self, style, outer_widths):
        """Build any kind of horizontal border for the table.

        :param str style: Type of border to return.
        :param iter outer_widths: List of widths (with padding) for each column.

        :return: Prepared border as a tuple of strings.
        :rtype: tuple
        """
        if style == 'top':
            horizontal = self.CHAR_OUTER_TOP_HORIZONTAL
            left = self.CHAR_OUTER_TOP_LEFT
            intersect = self.CHAR_OUTER_TOP_INTERSECT if self.inner_column_border else ''
            right = self.CHAR_OUTER_TOP_RIGHT
            title = self.title
        elif style == 'bottom':
            horizontal = self.CHAR_OUTER_BOTTOM_HORIZONTAL
            left = self.CHAR_OUTER_BOTTOM_LEFT
            intersect = self.CHAR_OUTER_BOTTOM_INTERSECT if self.inner_column_border else ''
            right = self.CHAR_OUTER_BOTTOM_RIGHT
            title = None
        elif style == 'heading':
            horizontal = self.CHAR_H_INNER_HORIZONTAL
            left = self.CHAR_H_OUTER_LEFT_INTERSECT if self.outer_border else ''
            intersect = self.CHAR_H_INNER_INTERSECT if self.inner_column_border else ''
            right = self.CHAR_H_OUTER_RIGHT_INTERSECT if self.outer_border else ''
            title = None
        elif style == 'footing':
            horizontal = self.CHAR_F_INNER_HORIZONTAL
            left = self.CHAR_F_OUTER_LEFT_INTERSECT if self.outer_border else ''
            intersect = self.CHAR_F_INNER_INTERSECT if self.inner_column_border else ''
            right = self.CHAR_F_OUTER_RIGHT_INTERSECT if self.outer_border else ''
            title = None
        else:
            horizontal = self.CHAR_INNER_HORIZONTAL
            left = self.CHAR_OUTER_LEFT_INTERSECT if self.outer_border else ''
            intersect = self.CHAR_INNER_INTERSECT if self.inner_column_border else ''
            right = self.CHAR_OUTER_RIGHT_INTERSECT if self.outer_border else ''
            title = None
        return build_border(outer_widths, horizontal, left, intersect, right,
                            title)
示例#10
0
    def horizontal_border(self, style, outer_widths):
        """Build any kind of horizontal border for the table.

        :param str style: Type of border to return.
        :param iter outer_widths: List of widths (with padding) for each column.

        :return: Prepared border as a tuple of strings.
        :rtype: tuple
        """
        if style == 'top':
            horizontal = self.CHAR_OUTER_TOP_HORIZONTAL
            left = self.CHAR_OUTER_TOP_LEFT
            intersect = self.CHAR_OUTER_TOP_INTERSECT if self.inner_column_border else ''
            right = self.CHAR_OUTER_TOP_RIGHT
            title = self.title
        elif style == 'bottom':
            horizontal = self.CHAR_OUTER_BOTTOM_HORIZONTAL
            left = self.CHAR_OUTER_BOTTOM_LEFT
            intersect = self.CHAR_OUTER_BOTTOM_INTERSECT if self.inner_column_border else ''
            right = self.CHAR_OUTER_BOTTOM_RIGHT
            title = None
        elif style == 'heading':
            horizontal = self.CHAR_H_INNER_HORIZONTAL
            left = self.CHAR_H_OUTER_LEFT_INTERSECT if self.outer_border else ''
            intersect = self.CHAR_H_INNER_INTERSECT if self.inner_column_border else ''
            right = self.CHAR_H_OUTER_RIGHT_INTERSECT if self.outer_border else ''
            title = None
        elif style == 'footing':
            horizontal = self.CHAR_F_INNER_HORIZONTAL
            left = self.CHAR_F_OUTER_LEFT_INTERSECT if self.outer_border else ''
            intersect = self.CHAR_F_INNER_INTERSECT if self.inner_column_border else ''
            right = self.CHAR_F_OUTER_RIGHT_INTERSECT if self.outer_border else ''
            title = None
        else:
            horizontal = self.CHAR_INNER_HORIZONTAL
            left = self.CHAR_OUTER_LEFT_INTERSECT if self.outer_border else ''
            intersect = self.CHAR_INNER_INTERSECT if self.inner_column_border else ''
            right = self.CHAR_OUTER_RIGHT_INTERSECT if self.outer_border else ''
            title = None
        return build_border(outer_widths, horizontal, left, intersect, right, title)