/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/utextwrap.py

  • Committer: Jelmer Vernooij
  • Date: 2018-05-06 11:48:54 UTC
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@jelmer.uk-20180506114854-h4qd9ojaqy8wxjsd
Move .mailmap to root.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
 
26
26
from __future__ import absolute_import
27
27
 
 
28
import sys
28
29
import textwrap
29
30
from unicodedata import east_asian_width as _eawidth
30
31
 
32
33
 
33
34
__all__ = ["UTextWrapper", "fill", "wrap"]
34
35
 
35
 
 
36
36
class UTextWrapper(textwrap.TextWrapper):
37
37
    """
38
38
    Extend TextWrapper for Unicode.
60
60
    def __init__(self, width=None, **kwargs):
61
61
        if width is None:
62
62
            width = (osutils.terminal_width() or
63
 
                     osutils.default_terminal_width) - 1
 
63
                        osutils.default_terminal_width) - 1
64
64
 
65
65
        ambi_width = kwargs.pop('ambiguous_width', 1)
66
66
        if ambi_width == 1:
70
70
        else:
71
71
            raise ValueError("ambiguous_width should be 1 or 2")
72
72
 
73
 
        self.max_lines = kwargs.get('max_lines', None)
74
73
        textwrap.TextWrapper.__init__(self, width, **kwargs)
75
74
 
76
75
    def _unicode_char_width(self, uc):
118
117
        to use unicode always.
119
118
        """
120
119
        i = 0
121
 
        L = len(chunks) - 1
 
120
        L = len(chunks)-1
122
121
        patsearch = self.sentence_end_re.search
123
122
        while i < L:
124
 
            if chunks[i + 1] == u" " and patsearch(chunks[i]):
125
 
                chunks[i + 1] = u"  "
 
123
            if chunks[i+1] == u" " and patsearch(chunks[i]):
 
124
                chunks[i+1] = u"  "
126
125
                i += 2
127
126
            else:
128
127
                i += 1
161
160
        lines = []
162
161
        if self.width <= 0:
163
162
            raise ValueError("invalid width %r (must be > 0)" % self.width)
164
 
        if self.max_lines is not None:
165
 
            if self.max_lines > 1:
166
 
                indent = self.subsequent_indent
167
 
            else:
168
 
                indent = self.initial_indent
169
 
            if self._width(indent) + self._width(self.placeholder.lstrip()) > self.width:
170
 
                raise ValueError("placeholder too large for max width")
171
163
 
172
164
        # Arrange in reverse order so items can be efficiently popped
173
165
        # from a stack of chucks.
211
203
            # fit on *any* line (not just this one).
212
204
            if chunks and self._width(chunks[-1]) > width:
213
205
                self._handle_long_word(chunks, cur_line, cur_len, width)
214
 
                cur_len = sum(map(len, cur_line))
215
206
 
216
207
            # If the last chunk on this line is all whitespace, drop it.
217
208
            if self.drop_whitespace and cur_line and not cur_line[-1].strip():
220
211
            # Convert current line back to a string and store it in list
221
212
            # of all lines (return value).
222
213
            if cur_line:
223
 
                if (self.max_lines is None or
224
 
                    len(lines) + 1 < self.max_lines or
225
 
                    (not chunks or
226
 
                        self.drop_whitespace and
227
 
                     len(chunks) == 1 and
228
 
                     not chunks[0].strip()) and cur_len <= width):
229
 
                    # Convert current line back to a string and store it in
230
 
                    # list of all lines (return value).
231
 
                    lines.append(indent + u''.join(cur_line))
232
 
                else:
233
 
                    while cur_line:
234
 
                        if (cur_line[-1].strip() and
235
 
                                cur_len + self._width(self.placeholder) <= width):
236
 
                            cur_line.append(self.placeholder)
237
 
                            lines.append(indent + ''.join(cur_line))
238
 
                            break
239
 
                        cur_len -= self._width(cur_line[-1])
240
 
                        del cur_line[-1]
241
 
                    else:
242
 
                        if lines:
243
 
                            prev_line = lines[-1].rstrip()
244
 
                            if (self._width(prev_line) + self._width(self.placeholder) <=
245
 
                                    self.width):
246
 
                                lines[-1] = prev_line + self.placeholder
247
 
                                break
248
 
                        lines.append(indent + self.placeholder.lstrip())
249
 
                    break
 
214
                lines.append(indent + u''.join(cur_line))
250
215
 
251
216
        return lines
252
217
 
253
218
    def _split(self, text):
254
 
        chunks = textwrap.TextWrapper._split(self, osutils.safe_unicode(text))
 
219
        chunks = textwrap.TextWrapper._split(self, unicode(text))
255
220
        cjk_split_chunks = []
256
221
        for chunk in chunks:
257
222
            prev_pos = 0
260
225
                    if prev_pos < pos:
261
226
                        cjk_split_chunks.append(chunk[prev_pos:pos])
262
227
                    cjk_split_chunks.append(char)
263
 
                    prev_pos = pos + 1
 
228
                    prev_pos = pos+1
264
229
            if prev_pos < len(chunk):
265
230
                cjk_split_chunks.append(chunk[prev_pos:])
266
231
        return cjk_split_chunks
267
232
 
268
233
    def wrap(self, text):
269
234
        # ensure text is unicode
270
 
        return textwrap.TextWrapper.wrap(self, osutils.safe_unicode(text))
 
235
        return textwrap.TextWrapper.wrap(self, unicode(text))
271
236
 
272
237
# -- Convenience interface ---------------------------------------------
273
238
 
274
 
 
275
239
def wrap(text, width=None, **kwargs):
276
240
    """Wrap a single paragraph of text, returning a list of wrapped lines.
277
241
 
284
248
    """
285
249
    return UTextWrapper(width=width, **kwargs).wrap(text)
286
250
 
287
 
 
288
251
def fill(text, width=None, **kwargs):
289
252
    """Fill a single paragraph of text, returning a new string.
290
253
 
295
258
    available keyword args to customize wrapping behaviour.
296
259
    """
297
260
    return UTextWrapper(width=width, **kwargs).fill(text)
 
261