How to use the beautifultable.utils.termwidth function in beautifultable

To help you get started, we’ve selected a few beautifultable examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pri22296 / beautifultable / beautifultable / helpers.py View on Github external
table.detect_numerics,
                        table.precision,
                        sign.value,
                    ).split("\n")
                )
        for row in map(list, zip_longest(*rows, fillvalue="")):
            for i in range(len(row)):
                row[i] = pre_process(
                    row[i], table.detect_numerics, table.precision, sign.value,
                )
            for row_ in self._clamp_row(row):
                for i in range(len(table.columns)):
                    # str.format method doesn't work for multibyte strings
                    # hence, we need to manually align the texts instead
                    # of using the align property of the str.format method
                    pad_len = width[i] - termwidth(row_[i])
                    if align[i].value == "<":
                        right_pad = " " * pad_len
                        row_[i] = to_unicode(row_[i]) + right_pad
                    elif align[i].value == ">":
                        left_pad = " " * pad_len
                        row_[i] = left_pad + to_unicode(row_[i])
                    else:
                        left_pad = " " * (pad_len // 2)
                        right_pad = " " * (pad_len - pad_len // 2)
                        row_[i] = left_pad + to_unicode(row_[i]) + right_pad
                content = []
                for j, item in enumerate(row_):
                    if j > 0:
                        content.append(
                            table.columns.separator
                            if (mask[j - 1] or mask[j])
github pri22296 / beautifultable / beautifultable / helpers.py View on Github external
right_pad = " " * (pad_len - pad_len // 2)
                        row_[i] = left_pad + to_unicode(row_[i]) + right_pad
                content = []
                for j, item in enumerate(row_):
                    if j > 0:
                        content.append(
                            table.columns.separator
                            if (mask[j - 1] or mask[j])
                            else " " * termwidth(table.columns.separator)
                        )
                    content.append(item)
                content = "".join(content)
                content = (
                    table.border.left
                    if mask[0]
                    else " " * termwidth(table.border.left)
                ) + content
                content += (
                    table.border.right
                    if mask[-1]
                    else " " * termwidth(table.border.right)
                )
                string.append(content)
        return "\n".join(string)
github pri22296 / beautifultable / beautifultable / beautifultable.py View on Github external
table_width = self._width
        lpw, rpw = self.columns.padding_left, self.columns.padding_right
        pad_widths = [(lpw[i] + rpw[i]) for i in range(len(self.columns))]
        maxwidths = [0 for index in range(len(self.columns))]
        offset = table_width - sum(self.columns.width) + sum(pad_widths)
        self._maxwidth = max(self._maxwidth, offset + len(self.columns))

        for index, header in enumerate(self.columns.header):
            max_length = 0
            for i in pre_process(
                header, self.detect_numerics, self.precision, self.sign.value
            ).split("\n"):
                output_str = pre_process(
                    i, self.detect_numerics, self.precision, self.sign.value,
                )
                max_length = max(max_length, termwidth(output_str))
            maxwidths[index] += max_length

        for index, column in enumerate(zip(*self._data)):
            max_length = maxwidths[index]
            for i in column:
                for j in pre_process(
                    i, self.detect_numerics, self.precision, self.sign.value
                ).split("\n"):
                    output_str = pre_process(
                        j,
                        self.detect_numerics,
                        self.precision,
                        self.sign.value,
                    )
                    max_length = max(max_length, termwidth(output_str))
            maxwidths[index] = max_length
github pri22296 / beautifultable / beautifultable / beautifultable.py View on Github external
def _width(self):
        """Get the actual width of the table as number of characters.

        Column width should be set prior to calling this method.

        Returns
        -------
        int
            Width of the table as number of characters.
        """
        if len(self.columns) == 0:
            return 0
        width = sum(self.columns.width)
        width += (len(self.columns) - 1) * termwidth(self.columns.separator)
        width += termwidth(self.border.left)
        width += termwidth(self.border.right)
        return width
github pri22296 / beautifultable / beautifultable / beautifultable.py View on Github external
def _width(self):
        """Get the actual width of the table as number of characters.

        Column width should be set prior to calling this method.

        Returns
        -------
        int
            Width of the table as number of characters.
        """
        if len(self.columns) == 0:
            return 0
        width = sum(self.columns.width)
        width += (len(self.columns) - 1) * termwidth(self.columns.separator)
        width += termwidth(self.border.left)
        width += termwidth(self.border.right)
        return width
github pri22296 / beautifultable / beautifultable / beautifultable.py View on Github external
# If left border is enabled and it is visible
            visible_junc = not intersect_left.isspace()
            if termwidth(self.border.left) > 0:
                if not (self.border.left.isspace() and visible_junc):
                    length = min(
                        termwidth(self.border.left), termwidth(intersect_left),
                    )
                    for i in range(length):
                        line[i] = intersect_left[i] if mask[0] else " "
            visible_junc = not intersect_right.isspace()
            # If right border is enabled and it is visible
            if termwidth(self.border.right) > 0:
                if not (self.border.right.isspace() and visible_junc):
                    length = min(
                        termwidth(self.border.right),
                        termwidth(intersect_right),
                    )
                    for i in range(length):
                        line[-i - 1] = (
                            intersect_right[-i - 1] if mask[-1] else " "
                        )
            visible_junc = not intersect_mid.isspace()
            # If column separator is enabled and it is visible
            if termwidth(self.columns.separator):
                if not (self.columns.separator.isspace() and visible_junc):
                    index = termwidth(self.border.left)
                    for i in range(len(self.columns) - 1):
                        if not mask[i]:
                            for j in range(self.columns.width[i]):
                                line[index + j] = " "
                        index += self.columns.width[i]
                        length = min(
github pri22296 / beautifultable / beautifultable / beautifultable.py View on Github external
def maxwidth(self):
        """get/set the maximum width of the table.

        The width of the table is guaranteed to not exceed this value. If it
        is not possible to print a given table with the width provided, this
        value will automatically adjust.
        """
        offset = (len(self.columns) - 1) * termwidth(self.columns.separator)
        offset += termwidth(self.border.left)
        offset += termwidth(self.border.right)
        self._maxwidth = max(self._maxwidth, offset + len(self.columns))
        return self._maxwidth
github pri22296 / beautifultable / beautifultable / rows.py View on Github external
delimiter: str
            String which is to be appended to the clamped string.

        Returns
        -------
        str
            The modified string which fits in it's column.
        """
        width = (
            self._table.column_widths[column_index]
            - self._table.left_padding_widths[column_index]
            - self._table.right_padding_widths[column_index]
        )

        if termwidth(row_item) <= width:
            return row_item
        else:
            if width - len(delimiter) >= 0:
                clamped_string = (
                    textwrap(row_item, width - len(delimiter))[0] + delimiter
                )
            else:
                clamped_string = delimiter[:width]
            return clamped_string
github pri22296 / beautifultable / beautifultable / beautifultable.py View on Github external
for i in range(length):
                        line[-i - 1] = (
                            intersect_right[-i - 1] if mask[-1] else " "
                        )
            visible_junc = not intersect_mid.isspace()
            # If column separator is enabled and it is visible
            if termwidth(self.columns.separator):
                if not (self.columns.separator.isspace() and visible_junc):
                    index = termwidth(self.border.left)
                    for i in range(len(self.columns) - 1):
                        if not mask[i]:
                            for j in range(self.columns.width[i]):
                                line[index + j] = " "
                        index += self.columns.width[i]
                        length = min(
                            termwidth(self.columns.separator),
                            termwidth(intersect_mid),
                        )
                        for j in range(length):
                            # TODO: we should also hide junctions based on mask
                            line[index + j] = (
                                intersect_mid[j]
                                if (mask[i] or mask[i + 1])
                                else " "
                            )
                        index += termwidth(self.columns.separator)

        return "".join(line)
github pri22296 / beautifultable / beautifultable / beautifultable.py View on Github external
line[-i - 1] = (
                            intersect_right[-i - 1] if mask[-1] else " "
                        )
            visible_junc = not intersect_mid.isspace()
            # If column separator is enabled and it is visible
            if termwidth(self.columns.separator):
                if not (self.columns.separator.isspace() and visible_junc):
                    index = termwidth(self.border.left)
                    for i in range(len(self.columns) - 1):
                        if not mask[i]:
                            for j in range(self.columns.width[i]):
                                line[index + j] = " "
                        index += self.columns.width[i]
                        length = min(
                            termwidth(self.columns.separator),
                            termwidth(intersect_mid),
                        )
                        for j in range(length):
                            # TODO: we should also hide junctions based on mask
                            line[index + j] = (
                                intersect_mid[j]
                                if (mask[i] or mask[i + 1])
                                else " "
                            )
                        index += termwidth(self.columns.separator)

        return "".join(line)