changeset 40243:96e50dfd8c94

releasenotes: use stringutil.wrap() instead of handcrafted TextWrapper wrapper It's silly to splitlines() a joined string, but we don't care the performance here.
author Yuya Nishihara <yuya@tcha.org>
date Sat, 13 Oct 2018 12:29:43 +0200
parents 19ed212de2d1
children 1039404c5e1d
files hgext/releasenotes.py
diffstat 1 files changed, 13 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/releasenotes.py	Sat Oct 13 00:22:05 2018 -0700
+++ b/hgext/releasenotes.py	Sat Oct 13 12:29:43 2018 +0200
@@ -16,7 +16,6 @@
 import difflib
 import errno
 import re
-import textwrap
 
 from mercurial.i18n import _
 from mercurial import (
@@ -29,6 +28,9 @@
     scmutil,
     util,
 )
+from mercurial.utils import (
+    stringutil,
+)
 
 cmdtable = {}
 command = registrar.command(cmdtable)
@@ -58,20 +60,6 @@
 
 BULLET_SECTION = _('Other Changes')
 
-if pycompat.ispy3:
-    class byteswrapper(object):
-        def __init__(self, **kwargs):
-            for k in kwargs:
-                v = kwargs[k]
-                if not isinstance(v, str) and isinstance(v, bytes):
-                    kwargs[k] = v.decode('utf8')
-            self._tw = textwrap.TextWrapper(**kwargs)
-        def wrap(self, data):
-            return [
-                l.encode('utf8') for l in self._tw.wrap(data.decode('utf8'))]
-else:
-    byteswrapper = textwrap.TextWrapper
-
 class parsedreleasenotes(object):
     def __init__(self):
         self.sections = {}
@@ -457,11 +445,11 @@
             lines.append('-' * len(title))
             lines.append('')
 
-            wrapper = byteswrapper(width=78)
             for i, para in enumerate(paragraphs):
                 if i:
                     lines.append('')
-                lines.extend(wrapper.wrap(' '.join(para)))
+                lines.extend(stringutil.wrap(' '.join(para),
+                                             width=78).splitlines())
 
             lines.append('')
 
@@ -479,17 +467,17 @@
             lines.append('')
 
         for paragraphs in nontitled:
-            wrapper = byteswrapper(initial_indent='* ',
-                                   subsequent_indent='  ',
-                                   width=78)
-            lines.extend(wrapper.wrap(' '.join(paragraphs[0])))
+            lines.extend(stringutil.wrap(' '.join(paragraphs[0]),
+                                         width=78,
+                                         initindent='* ',
+                                         hangindent='  ').splitlines())
 
-            wrapper = byteswrapper(initial_indent='  ',
-                                   subsequent_indent='  ',
-                                   width=78)
             for para in paragraphs[1:]:
                 lines.append('')
-                lines.extend(wrapper.wrap(' '.join(para)))
+                lines.extend(stringutil.wrap(' '.join(para),
+                                             width=78,
+                                             initindent='  ',
+                                             hangindent='  ').splitlines())
 
             lines.append('')