/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 bzrlib/utextwrap.py

  • Committer: INADA Naoki
  • Date: 2011-05-11 16:52:02 UTC
  • mto: This revision was merged to the branch mainline in revision 5874.
  • Revision ID: songofacandy@gmail.com-20110511165202-w5c0eh0b753zeef5
Fix wrong import.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2011 Canonical Ltd
 
2
#
 
3
# UTextWrapper._handle_long_word, UTextWrapper._wrap_chunks,
 
4
# wrap and fill is copied from Python's textwrap module
 
5
# (under PSF license) and modified for support CJK.
 
6
# Original Copyright for these functions:
 
7
#
 
8
# Copyright (C) 1999-2001 Gregory P. Ward.
 
9
# Copyright (C) 2002, 2003 Python Software Foundation.
 
10
#
 
11
# Written by Greg Ward <gward@python.net>
 
12
# This program is free software; you can redistribute it and/or modify
 
13
# it under the terms of the GNU General Public License as published by
 
14
# the Free Software Foundation; either version 2 of the License, or
 
15
# (at your option) any later version.
 
16
#
 
17
# This program is distributed in the hope that it will be useful,
 
18
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
19
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
20
# GNU General Public License for more details.
 
21
#
 
22
# You should have received a copy of the GNU General Public License
 
23
# along with this program; if not, write to the Free Software
 
24
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
25
 
 
26
import sys
 
27
import textwrap
 
28
from unicodedata import east_asian_width as _eawidth
 
29
 
 
30
from bzrlib import osutils
 
31
 
 
32
__all__ = ["UTextWrapper", "fill", "wrap"]
 
33
 
 
34
class UTextWrapper(textwrap.TextWrapper):
 
35
    """
 
36
    Extend TextWrapper for Unicode.
 
37
 
 
38
    This textwrapper handles east asian double width and split word
 
39
    even if !break_long_words when word contains double width
 
40
    characters.
 
41
 
 
42
    :param ambiguous_width: (keyword argument) width for character when
 
43
                            unicodedata.east_asian_width(c) == 'A'
 
44
                            (default: 1)
 
45
 
 
46
    Limitations:
 
47
    * expand_tabs doesn't fixed. It uses len() for calculating width
 
48
      of string on left of TAB.
 
49
    * Handles one codeunit as a single character having 1 or 2 width.
 
50
      This is not correct when there are surrogate pairs, combined
 
51
      characters or zero-width characters.
 
52
    * Treats all asian character are line breakable. But it is not
 
53
      true because line breaking is prohibited around some characters.
 
54
      (For example, breaking before punctation mark is prohibited.)
 
55
      See UAX # 14 "UNICODE LINE BREAKING ALGORITHM"
 
56
    """
 
57
 
 
58
    def __init__(self, width=None, **kwargs):
 
59
        if width is None:
 
60
            width = (osutils.terminal_width() or
 
61
                        osutils.default_terminal_width) - 1
 
62
 
 
63
        ambi_width = kwargs.pop('ambiguous_width', 1)
 
64
        if ambi_width == 1:
 
65
            self._east_asian_doublewidth = 'FW'
 
66
        elif ambi_width == 2:
 
67
            self._east_asian_doublewidth = 'FWA'
 
68
        else:
 
69
            raise ValueError("ambiguous_width should be 1 or 2")
 
70
 
 
71
        # No drop_whitespace param before Python 2.6 it was always dropped
 
72
        if sys.version_info < (2, 6):
 
73
            self.drop_whitespace = kwargs.pop("drop_whitespace", True)
 
74
            if not self.drop_whitespace:
 
75
                raise ValueError("TextWrapper version must drop whitespace")
 
76
        textwrap.TextWrapper.__init__(self, width, **kwargs)
 
77
 
 
78
    def _unicode_char_width(self, uc):
 
79
        """Return width of character `uc`.
 
80
 
 
81
        :param:     uc      Single unicode character.
 
82
        """
 
83
        # 'A' means width of the character is not be able to determine.
 
84
        # We assume that it's width is 2 because longer wrap may over
 
85
        # terminal width but shorter wrap may be acceptable.
 
86
        return (_eawidth(uc) in self._east_asian_doublewidth and 2) or 1
 
87
 
 
88
    def _width(self, s):
 
89
        """Returns width for s.
 
90
        
 
91
        When s is unicode, take care of east asian width.
 
92
        When s is bytes, treat all byte is single width character.
 
93
        """
 
94
        assert isinstance(s, unicode)
 
95
        charwidth = self._unicode_char_width
 
96
        return sum(charwidth(c) for c in s)
 
97
 
 
98
    def _cut(self, s, width):
 
99
        """Returns head and rest of s. (head+rest == s)
 
100
 
 
101
        Head is large as long as _width(head) <= width.
 
102
        """
 
103
        assert isinstance(s, unicode)
 
104
        w = 0
 
105
        charwidth = self._unicode_char_width
 
106
        for pos, c in enumerate(s):
 
107
            w += charwidth(c)
 
108
            if w > width:
 
109
                return s[:pos], s[pos:]
 
110
        return s, u''
 
111
 
 
112
    def _handle_long_word(self, chunks, cur_line, cur_len, width):
 
113
        # Figure out when indent is larger than the specified width, and make
 
114
        # sure at least one character is stripped off on every pass
 
115
        if width < 2:
 
116
            space_left = chunks[-1] and self._width(chunks[-1][0]) or 1
 
117
        else:
 
118
            space_left = width - cur_len
 
119
 
 
120
        # If we're allowed to break long words, then do so: put as much
 
121
        # of the next chunk onto the current line as will fit.
 
122
        if self.break_long_words:
 
123
            head, rest = self._cut(chunks[-1], space_left)
 
124
            cur_line.append(head)
 
125
            if rest:
 
126
                chunks[-1] = rest
 
127
            else:
 
128
                del chunks[-1]
 
129
 
 
130
        # Otherwise, we have to preserve the long word intact.  Only add
 
131
        # it to the current line if there's nothing already there --
 
132
        # that minimizes how much we violate the width constraint.
 
133
        elif not cur_line:
 
134
            cur_line.append(chunks.pop())
 
135
 
 
136
        # If we're not allowed to break long words, and there's already
 
137
        # text on the current line, do nothing.  Next time through the
 
138
        # main loop of _wrap_chunks(), we'll wind up here again, but
 
139
        # cur_len will be zero, so the next line will be entirely
 
140
        # devoted to the long word that we can't handle right now.
 
141
 
 
142
    def _wrap_chunks(self, chunks):
 
143
        lines = []
 
144
        if self.width <= 0:
 
145
            raise ValueError("invalid width %r (must be > 0)" % self.width)
 
146
 
 
147
        # Arrange in reverse order so items can be efficiently popped
 
148
        # from a stack of chucks.
 
149
        chunks.reverse()
 
150
 
 
151
        while chunks:
 
152
 
 
153
            # Start the list of chunks that will make up the current line.
 
154
            # cur_len is just the length of all the chunks in cur_line.
 
155
            cur_line = []
 
156
            cur_len = 0
 
157
 
 
158
            # Figure out which static string will prefix this line.
 
159
            if lines:
 
160
                indent = self.subsequent_indent
 
161
            else:
 
162
                indent = self.initial_indent
 
163
 
 
164
            # Maximum width for this line.
 
165
            width = self.width - len(indent)
 
166
 
 
167
            # First chunk on line is whitespace -- drop it, unless this
 
168
            # is the very beginning of the text (ie. no lines started yet).
 
169
            if self.drop_whitespace and chunks[-1].strip() == '' and lines:
 
170
                del chunks[-1]
 
171
 
 
172
            while chunks:
 
173
                # Use _width instead of len for east asian width
 
174
                l = self._width(chunks[-1])
 
175
 
 
176
                # Can at least squeeze this chunk onto the current line.
 
177
                if cur_len + l <= width:
 
178
                    cur_line.append(chunks.pop())
 
179
                    cur_len += l
 
180
 
 
181
                # Nope, this line is full.
 
182
                else:
 
183
                    break
 
184
 
 
185
            # The current line is full, and the next chunk is too big to
 
186
            # fit on *any* line (not just this one).
 
187
            if chunks and self._width(chunks[-1]) > width:
 
188
                self._handle_long_word(chunks, cur_line, cur_len, width)
 
189
 
 
190
            # If the last chunk on this line is all whitespace, drop it.
 
191
            if self.drop_whitespace and cur_line and cur_line[-1].strip() == '':
 
192
                del cur_line[-1]
 
193
 
 
194
            # Convert current line back to a string and store it in list
 
195
            # of all lines (return value).
 
196
            if cur_line:
 
197
                lines.append(indent + ''.join(cur_line))
 
198
 
 
199
        return lines
 
200
 
 
201
    def _split(self, text):
 
202
        chunks = textwrap.TextWrapper._split(self, unicode(text))
 
203
        cjk_split_chunks = []
 
204
        for chunk in chunks:
 
205
            assert chunk # TextWrapper._split removes empty chunk
 
206
            prev_pos = 0
 
207
            for pos, char in enumerate(chunk):
 
208
                if _eawidth(char) in 'FWA':
 
209
                    if prev_pos < pos:
 
210
                        cjk_split_chunks.append(chunk[prev_pos:pos])
 
211
                    cjk_split_chunks.append(char)
 
212
                    prev_pos = pos+1
 
213
            if prev_pos < len(chunk):
 
214
                cjk_split_chunks.append(chunk[prev_pos:])
 
215
        return cjk_split_chunks
 
216
 
 
217
    def wrap(self, text):
 
218
        # ensure text is unicode
 
219
        return textwrap.TextWrapper.wrap(self, unicode(text))
 
220
 
 
221
# -- Convenience interface ---------------------------------------------
 
222
 
 
223
def wrap(text, width=None, **kwargs):
 
224
    """Wrap a single paragraph of text, returning a list of wrapped lines.
 
225
 
 
226
    Reformat the single paragraph in 'text' so it fits in lines of no
 
227
    more than 'width' columns, and return a list of wrapped lines.  By
 
228
    default, tabs in 'text' are expanded with string.expandtabs(), and
 
229
    all other whitespace characters (including newline) are converted to
 
230
    space.  See TextWrapper class for available keyword args to customize
 
231
    wrapping behaviour.
 
232
    """
 
233
    return UTextWrapper(width=width, **kwargs).wrap(text)
 
234
 
 
235
def fill(text, width=None, **kwargs):
 
236
    """Fill a single paragraph of text, returning a new string.
 
237
 
 
238
    Reformat the single paragraph in 'text' to fit in lines of no more
 
239
    than 'width' columns, and return a new string containing the entire
 
240
    wrapped paragraph.  As with wrap(), tabs are expanded and other
 
241
    whitespace characters converted to space.  See TextWrapper class for
 
242
    available keyword args to customize wrapping behaviour.
 
243
    """
 
244
    return UTextWrapper(width=width, **kwargs).fill(text)
 
245