summaryrefslogtreecommitdiffstats
path: root/terminaltables/github_table.py
blob: 7eb1be7917ef31e4673367d95cfabecd89f5cc17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""GithubFlavoredMarkdownTable class."""

from terminaltables.ascii_table import AsciiTable
from terminaltables.build import combine


class GithubFlavoredMarkdownTable(AsciiTable):
    """Github flavored markdown table.

    https://help.github.com/articles/github-flavored-markdown/#tables

    :ivar iter table_data: List (empty or list of lists of strings) representing the table.
    :ivar dict justify_columns: Horizontal justification. Keys are column indexes (int). Values are right/left/center.
    """

    def __init__(self, table_data):
        """Constructor.

        :param iter table_data: List (empty or list of lists of strings) representing the table.
        """
        # Github flavored markdown table won't support title.
        super(GithubFlavoredMarkdownTable, self).__init__(table_data)

    def horizontal_border(self, _, outer_widths):
        """Handle the GitHub heading border.

        E.g.:
        |:---|:---:|---:|----|

        :param _: Unused.
        :param iter outer_widths: List of widths (with padding) for each column.

        :return: Prepared border strings in a generator.
        :rtype: iter
        """
        horizontal = str(self.CHAR_INNER_HORIZONTAL)
        left = self.CHAR_OUTER_LEFT_VERTICAL
        intersect = self.CHAR_INNER_VERTICAL
        right = self.CHAR_OUTER_RIGHT_VERTICAL

        columns = list()
        for i, width in enumerate(outer_widths):
            justify = self.justify_columns.get(i)
            width = max(3, width)  # Width should be at least 3 so justification can be applied.
            if justify == 'left':
                columns.append(':' + horizontal * (width - 1))
            elif justify == 'right':
                columns.append(horizontal * (width - 1) + ':')
            elif justify == 'center':
                columns.append(':' + horizontal * (width - 2) + ':')
            else:
                columns.append(horizontal * width)

        return combine(columns, left, intersect, right)

    def gen_table(self, inner_widths, inner_heights, outer_widths):
        """Combine everything and yield every line of the entire table with borders.

        :param iter inner_widths: List of widths (no padding) for each column.
        :param iter inner_heights: List of heights (no padding) for each row.
        :param iter outer_widths: List of widths (with padding) for each column.
        :return:
        """
        for i, row in enumerate(self.table_data):
            # Yield the row line by line (e.g. multi-line rows).
            for line in self.gen_row_lines(row, 'row', inner_widths, inner_heights[i]):
                yield line
            # Yield heading separator.
            if i == 0:
                yield self.horizontal_border(None, outer_widths)