_wrap_chunks(chunks : [string]) -> [string] Wrap a sequence of text chunks and return a list of lines of length 'self.width' or less. (If 'break_long_words' is false, some lines may be longer than this.) Chunks correspond roughly to words and the whitespace between
(self, chunks)
| 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] |
| 2842 | Wrap a sequence of text chunks and return a list of lines of |
| 2843 | length 'self.width' or less. (If 'break_long_words' is false, |
| 2844 | some lines may be longer than this.) Chunks correspond roughly |
| 2845 | to words and the whitespace between them: each chunk is |
| 2846 | indivisible (modulo 'break_long_words'), but a line break can |
| 2847 | come between any two chunks. Chunks should not have internal |
| 2848 | whitespace; ie. a chunk is either all whitespace or a "word". |
| 2849 | Whitespace chunks will be removed from the beginning and end of |
| 2850 | lines, but apart from that whitespace is preserved. |
| 2851 | """ |
| 2852 | lines = [] |
| 2853 | if self.width <= 0: |
| 2854 | raise ValueError("invalid width %r (must be > 0)" % self.width) |
| 2855 | if self.max_lines is not None: |
| 2856 | if self.max_lines > 1: |
| 2857 | indent = self.subsequent_indent |
| 2858 | else: |
| 2859 | indent = self.initial_indent |
| 2860 | if self._len(indent) + self._len(self.placeholder.lstrip()) > self.width: |
| 2861 | raise ValueError("placeholder too large for max width") |
| 2862 | |
| 2863 | # Arrange in reverse order so items can be efficiently popped |
| 2864 | # from a stack of chucks. |
| 2865 | chunks.reverse() |
| 2866 | |
| 2867 | while chunks: |
| 2868 | |
| 2869 | # Start the list of chunks that will make up the current line. |
| 2870 | # cur_len is just the length of all the chunks in cur_line. |
| 2871 | cur_line = [] |
| 2872 | cur_len = 0 |
| 2873 | |
| 2874 | # Figure out which static string will prefix this line. |
| 2875 | if lines: |
| 2876 | indent = self.subsequent_indent |
| 2877 | else: |
| 2878 | indent = self.initial_indent |
| 2879 | |
| 2880 | # Maximum width for this line. |
| 2881 | width = self.width - self._len(indent) |
| 2882 | |
| 2883 | # First chunk on line is whitespace -- drop it, unless this |
| 2884 | # is the very beginning of the text (ie. no lines started yet). |
| 2885 | if self.drop_whitespace and chunks[-1].strip() == "" and lines: |
| 2886 | del chunks[-1] |
| 2887 | |
| 2888 | while chunks: |
| 2889 | chunk_len = self._len(chunks[-1]) |
| 2890 | |
| 2891 | # Can at least squeeze this chunk onto the current line. |
| 2892 | if cur_len + chunk_len <= width: |
| 2893 | cur_line.append(chunks.pop()) |
| 2894 | cur_len += chunk_len |
| 2895 | |
| 2896 | # Nope, this line is full. |
| 2897 | else: |
nothing calls this directly
no test coverage detected