

def _TextJustify(self, text, col_size):
    'Formats text within column with white space padding.\n\n    A single space is prefixed, and a number of spaces are added as a\n    suffix such that the length of the resultant string equals the col_size.\n\n    If the length of the text exceeds the column width available then it\n    is split into words and returned as a list of string, each string\n    contains one or more words padded to the column size.\n\n    Args:\n      text: String of text to format.\n      col_size: integer size of column to pad out the text to.\n\n    Returns:\n      List of strings col_size in length.\n\n    Raises:\n      TableError: If col_size is too small to fit the words in the text.\n    '
    result = []
    if ('\n' in text):
        for paragraph in text.split('\n'):
            result.extend(self._TextJustify(paragraph, col_size))
        return result
    wrapper = textwrap.TextWrapper(width=(col_size - 2), break_long_words=False, expand_tabs=False)
    try:
        text_list = wrapper.wrap(text)
    except ValueError:
        raise TableError('Field too small (minimum width: 3)')
    if (not text_list):
        return [(' ' * col_size)]
    for current_line in text_list:
        stripped_len = len(terminal.StripAnsiText(current_line))
        ansi_color_adds = (len(current_line) - stripped_len)
        if ((stripped_len + 2) > col_size):
            raise TableError('String contains words that do not fit in column.')
        result.append((' %-*s' % (((col_size - 1) + ansi_color_adds), current_line)))
    return result

dataset/ETHPy150Open google/textfsm/texttable.py TextTable._TextJustify/original