_handle_long_word(chunks : [string], cur_line : [string], cur_len : int, width : int) Handle a chunk of text (most likely a word, not whitespace) that is too long to fit in any line.
(self, reversed_chunks, cur_line, cur_len, width)
| 2783 | lines.append(new_line) |
| 2784 | |
| 2785 | def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width): |
| 2786 | """_handle_long_word(chunks : [string], |
| 2787 | cur_line : [string], |
| 2788 | cur_len : int, width : int) |
| 2789 | Handle a chunk of text (most likely a word, not whitespace) that |
| 2790 | is too long to fit in any line. |
| 2791 | """ |
| 2792 | # Figure out when indent is larger than the specified width, and make |
| 2793 | # sure at least one character is stripped off on every pass |
| 2794 | if width < 1: |
| 2795 | space_left = 1 |
| 2796 | else: |
| 2797 | space_left = width - cur_len |
| 2798 | |
| 2799 | # If we're allowed to break long words, then do so: put as much |
| 2800 | # of the next chunk onto the current line as will fit. |
| 2801 | if self.break_long_words and space_left > 0: |
| 2802 | # Tabulate Custom: Build the string up piece-by-piece in order to |
| 2803 | # take each charcter's width into account |
| 2804 | chunk = reversed_chunks[-1] |
| 2805 | i = 1 |
| 2806 | # Only count printable characters, so strip_ansi first, index later. |
| 2807 | stripped_chunk = _strip_ansi(chunk) |
| 2808 | while ( |
| 2809 | i <= len(stripped_chunk) and self._len(stripped_chunk[:i]) <= space_left |
| 2810 | ): |
| 2811 | i = i + 1 |
| 2812 | # Consider escape codes when breaking words up |
| 2813 | total_escape_len = 0 |
| 2814 | last_group = 0 |
| 2815 | if _ansi_codes.search(chunk) is not None: |
| 2816 | for group, _, _, _ in _ansi_codes.findall(chunk): |
| 2817 | escape_len = len(group) |
| 2818 | if ( |
| 2819 | group |
| 2820 | in chunk[last_group : i + total_escape_len + escape_len - 1] |
| 2821 | ): |
| 2822 | total_escape_len += escape_len |
| 2823 | found = _ansi_codes.search(chunk[last_group:]) |
| 2824 | last_group += found.end() |
| 2825 | cur_line.append(chunk[: i + total_escape_len - 1]) |
| 2826 | reversed_chunks[-1] = chunk[i + total_escape_len - 1 :] |
| 2827 | |
| 2828 | # Otherwise, we have to preserve the long word intact. Only add |
| 2829 | # it to the current line if there's nothing already there -- |
| 2830 | # that minimizes how much we violate the width constraint. |
| 2831 | elif not cur_line: |
| 2832 | cur_line.append(reversed_chunks.pop()) |
| 2833 | |
| 2834 | # If we're not allowed to break long words, and there's already |
| 2835 | # text on the current line, do nothing. Next time through the |
| 2836 | # main loop of _wrap_chunks(), we'll wind up here again, but |
| 2837 | # cur_len will be zero, so the next line will be entirely |
| 2838 | # devoted to the long word that we can't handle right now. |
| 2839 | |
| 2840 | def _wrap_chunks(self, chunks): |
| 2841 | """_wrap_chunks(chunks : [string]) -> [string] |
no test coverage detected