Commit bdbebada authored by Chris Lamb's avatar Chris Lamb 💬

presenters: Use an optimised indentation routine throughout all text presenters.

Signed-off-by: Chris Lamb's avatarChris Lamb <lamby@debian.org>
parent 5197c6e9
......@@ -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))
......@@ -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)
......
......@@ -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)]
......
......@@ -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)),
)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment