Merge lp:~jelmer/brz/fix-utextwrap into lp:brz

Proposed by Jelmer Vernooij on 2018-09-14
Status: Merged
Approved by: Jelmer Vernooij on 2018-09-14
Approved revision: 7114
Merge reported by: The Breezy Bot
Merged at revision: not available
Proposed branch: lp:~jelmer/brz/fix-utextwrap
Merge into: lp:brz
Diff against target: 67 lines (+36/-1)
1 file modified
breezy/utextwrap.py (+36/-1)
To merge this branch: bzr merge lp:~jelmer/brz/fix-utextwrap
Reviewer Review Type Date Requested Status
Martin Packman 2018-09-14 Approve on 2018-09-14
Review via email: mp+354948@code.launchpad.net

Description of the change

Support max_lines parameter in UTextWrapper.

The test_textwrap tests from Python upstream that we invoke test this parameter.

(Note that we don't see this test on Debian/Ubuntu, because they don't ship test_textwrap.py as part of Python)

To post a comment you must log in.
Martin Packman (gz) wrote :

Have been meaning to bug Inada Naoki about this for a while, I feel like there *must* be a better multilingual text wrapping option now, but... maybe not?

Not mad about adding more logic to our forky-subclass, but at least it's got upstream tests.

review: Approve
Jelmer Vernooij (jelmer) wrote :

Yeah, I would expect the same - at least on Python 3. Worth pursuing..

The Breezy Bot (the-breezy-bot) wrote :

Running landing tests failed
https://ci.breezy-vcs.org/job/land-brz/478/

lp:~jelmer/brz/fix-utextwrap updated on 2018-09-14
7114. By Jelmer Vernooij on 2018-09-14

Fix test on python2.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'breezy/utextwrap.py'
2--- breezy/utextwrap.py 2018-07-01 00:07:24 +0000
3+++ breezy/utextwrap.py 2018-09-14 22:56:49 +0000
4@@ -70,6 +70,7 @@
5 else:
6 raise ValueError("ambiguous_width should be 1 or 2")
7
8+ self.max_lines = kwargs.get('max_lines', None)
9 textwrap.TextWrapper.__init__(self, width, **kwargs)
10
11 def _unicode_char_width(self, uc):
12@@ -160,6 +161,13 @@
13 lines = []
14 if self.width <= 0:
15 raise ValueError("invalid width %r (must be > 0)" % self.width)
16+ if self.max_lines is not None:
17+ if self.max_lines > 1:
18+ indent = self.subsequent_indent
19+ else:
20+ indent = self.initial_indent
21+ if self._width(indent) + self._width(self.placeholder.lstrip()) > self.width:
22+ raise ValueError("placeholder too large for max width")
23
24 # Arrange in reverse order so items can be efficiently popped
25 # from a stack of chucks.
26@@ -203,6 +211,7 @@
27 # fit on *any* line (not just this one).
28 if chunks and self._width(chunks[-1]) > width:
29 self._handle_long_word(chunks, cur_line, cur_len, width)
30+ cur_len = sum(map(len, cur_line))
31
32 # If the last chunk on this line is all whitespace, drop it.
33 if self.drop_whitespace and cur_line and not cur_line[-1].strip():
34@@ -211,7 +220,33 @@
35 # Convert current line back to a string and store it in list
36 # of all lines (return value).
37 if cur_line:
38- lines.append(indent + u''.join(cur_line))
39+ if (self.max_lines is None or
40+ len(lines) + 1 < self.max_lines or
41+ (not chunks or
42+ self.drop_whitespace and
43+ len(chunks) == 1 and
44+ not chunks[0].strip()) and cur_len <= width):
45+ # Convert current line back to a string and store it in
46+ # list of all lines (return value).
47+ lines.append(indent + u''.join(cur_line))
48+ else:
49+ while cur_line:
50+ if (cur_line[-1].strip() and
51+ cur_len + self._width(self.placeholder) <= width):
52+ cur_line.append(self.placeholder)
53+ lines.append(indent + ''.join(cur_line))
54+ break
55+ cur_len -= self._width(cur_line[-1])
56+ del cur_line[-1]
57+ else:
58+ if lines:
59+ prev_line = lines[-1].rstrip()
60+ if (self._width(prev_line) + self._width(self.placeholder) <=
61+ self.width):
62+ lines[-1] = prev_line + self.placeholder
63+ break
64+ lines.append(indent + self.placeholder.lstrip())
65+ break
66
67 return lines
68

Subscribers

People subscribed via source and target branches