[diffoscope] 01/02: presenters: Use an optimised indentation routine throughout all text presenters.

Chris Lamb chris at chris-lamb.co.uk
Sat Jan 21 12:16:35 CET 2017


This is an automated email from the git hooks/post-receive script.

lamby pushed a commit to branch master
in repository diffoscope.

commit bdbebadae1a64d8688d23096cf99f87ef878a7de
Author: Chris Lamb <lamby at debian.org>
Date:   Sat Jan 21 18:09:24 2017 +1100

    presenters: Use an optimised indentation routine throughout all text presenters.
    
    Signed-off-by: Chris Lamb <lamby at debian.org>
---
 diffoscope/presenters/base.py             | 10 ++++++++++
 diffoscope/presenters/markdown.py         |  5 ++---
 diffoscope/presenters/restructuredtext.py |  5 ++---
 diffoscope/presenters/text.py             |  9 +++------
 4 files changed, 17 insertions(+), 12 deletions(-)

diff --git a/diffoscope/presenters/base.py b/diffoscope/presenters/base.py
index 844edfe..613da38 100644
--- a/diffoscope/presenters/base.py
+++ b/diffoscope/presenters/base.py
@@ -17,6 +17,7 @@
 # You should have received a copy of the GNU General Public License
 # along with diffoscope.  If not, see <https://www.gnu.org/licenses/>.
 
+
 class Presenter(object):
     def __init__(self):
         self.depth = 0
@@ -33,3 +34,12 @@ class Presenter(object):
 
     def visit_difference(self, difference):
         raise NotImplementedError()
+
+    @classmethod
+    def indent(cls, val, prefix):
+        # As an optimisation, output as much as possible in one go to avoid
+        # unnecessary splitting, interpolating, etc.
+        #
+        # We don't use textwrap.indent as that unnecessarily calls
+        # str.splitlines, etc.
+        return prefix + val.rstrip().replace('\n', '\n{}'.format(prefix))
diff --git a/diffoscope/presenters/markdown.py b/diffoscope/presenters/markdown.py
index df847c4..f2d800d 100644
--- a/diffoscope/presenters/markdown.py
+++ b/diffoscope/presenters/markdown.py
@@ -17,8 +17,6 @@
 # You should have received a copy of the GNU General Public License
 # along with diffoscope.  If not, see <https://www.gnu.org/licenses/>.
 
-import textwrap
-
 from .base import Presenter
 
 
@@ -41,7 +39,8 @@ class MarkdownTextPresenter(Presenter):
             self.print_func()
 
         if difference.unified_diff:
-            self.print_func(textwrap.indent(difference.unified_diff, '    '))
+            self.print_func(self.indent(difference.unified_diff, '    '))
+            self.print_func()
 
     def title(self, val):
         prefix = '#' * min(self.depth + 1, 6)
diff --git a/diffoscope/presenters/restructuredtext.py b/diffoscope/presenters/restructuredtext.py
index 6f711e6..b2ba1e1 100644
--- a/diffoscope/presenters/restructuredtext.py
+++ b/diffoscope/presenters/restructuredtext.py
@@ -17,8 +17,6 @@
 # You should have received a copy of the GNU General Public License
 # along with diffoscope.  If not, see <https://www.gnu.org/licenses/>.
 
-import textwrap
-
 from .base import Presenter
 
 
@@ -45,7 +43,8 @@ class RestructuredTextPresenter(Presenter):
         if difference.unified_diff:
             self.print_func('::')
             self.print_func()
-            self.print_func(textwrap.indent(difference.unified_diff, '    '))
+            self.print_func(self.indent(difference.unified_diff, '    '))
+            self.print_func()
 
     def title(self, val):
         char = self.TITLE_CHARS[self.depth % len(self.TITLE_CHARS)]
diff --git a/diffoscope/presenters/text.py b/diffoscope/presenters/text.py
index 971e7c1..7e172c3 100644
--- a/diffoscope/presenters/text.py
+++ b/diffoscope/presenters/text.py
@@ -53,9 +53,6 @@ class TextPresenter(Presenter):
             self.output(color_unified_diff(diff) if self.color else diff, True)
 
     def output(self, val, raw=False):
-        # As an optimisation, output as much as possible in one go to avoid
-        # unnecessary splitting, interpolating, etc.
-        self.print_func(self.RE_PREFIX.sub(
-            r'\1{}'.format(self.PREFIX * (self.depth + 0 if raw else -1)),
-            val.rstrip(),
-        ))
+        self.print_func(
+            self.indent(val, self.PREFIX * (self.depth + 0 if raw else -1)),
+        )

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/diffoscope.git


More information about the diffoscope mailing list