XZise has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/168967

Change subject: [FEAT] Diff: Use PatchManager for showDiff
......................................................................

[FEAT] Diff: Use PatchManager for showDiff

This uses PatchManager for pywikibot.showDiff and also includes now a
header.

It also fixes the missing space in front of a line if it was added or
removed. This makes it consistent with lines where only parts of it were
changed.

Also a minor change that the state which stores if it ends with the
default color is now represented as a boolean.

Change-Id: Ic59df944b4c64b8536a5178e1aabc0b1adfeab47
---
M pywikibot/__init__.py
M pywikibot/diff.py
2 files changed, 14 insertions(+), 64 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/67/168967/1

diff --git a/pywikibot/__init__.py b/pywikibot/__init__.py
index d70dc86..cf6ffa5 100644
--- a/pywikibot/__init__.py
+++ b/pywikibot/__init__.py
@@ -9,7 +9,6 @@
 __version__ = '$Id$'
 
 import datetime
-import difflib
 import math
 import re
 import sys
@@ -602,60 +601,7 @@
     The differences are highlighted (only on compatible systems) to show which
     changes were made.
     """
-    # This is probably not portable to non-terminal interfaces....
-    # For information on difflib, see http://pydoc.org/2.1/difflib.html
-    color = {
-        '+': 'lightgreen',
-        '-': 'lightred',
-    }
-    diff = u''
-    colors = []
-    # This will store the last line beginning with + or -.
-    lastline = None
-    # For testing purposes only: show original, uncolored diff
-    #     for line in difflib.ndiff(oldtext.splitlines(), 
newtext.splitlines()):
-    #         print line
-    for line in difflib.ndiff(oldtext.splitlines(), newtext.splitlines()):
-        if line.startswith('?'):
-            # initialize color vector with None, which means default color
-            lastcolors = [None for c in lastline]
-            # colorize the + or - sign
-            lastcolors[0] = color[lastline[0]]
-            # colorize changed parts in red or green
-            for i in range(min(len(line), len(lastline))):
-                if line[i] != ' ':
-                    lastcolors[i] = color[lastline[0]]
-            diff += lastline + '\n'
-            # append one None (default color) for the newline character
-            colors += lastcolors + [None]
-        elif lastline:
-            diff += lastline + '\n'
-            # colorize the + or - sign only
-            lastcolors = [None for c in lastline]
-            lastcolors[0] = color[lastline[0]]
-            colors += lastcolors + [None]
-        lastline = None
-        if line[0] in ('+', '-'):
-            lastline = line
-    # there might be one + or - line left that wasn't followed by a ? line.
-    if lastline:
-        diff += lastline + '\n'
-        # colorize the + or - sign only
-        lastcolors = [None for c in lastline]
-        lastcolors[0] = color[lastline[0]]
-        colors += lastcolors + [None]
-
-    result = u''
-    lastcolor = None
-    for i in range(len(diff)):
-        if colors[i] != lastcolor:
-            if lastcolor is None:
-                result += '\03{%s}' % colors[i]
-            else:
-                result += '\03{default}'
-        lastcolor = colors[i]
-        result += diff[i]
-    output(result)
+    pywikibot.diff.PatchManager(oldtext, newtext).print_hunks()
 
 
 # Throttle and thread handling
diff --git a/pywikibot/diff.py b/pywikibot/diff.py
index 7ac30dc..3c25fe7 100644
--- a/pywikibot/diff.py
+++ b/pywikibot/diff.py
@@ -79,13 +79,13 @@
         for tag, i1, i2, j1, j2 in self.group:
             if tag == 'equal':
                 for line in self.a[i1:i2]:
-                    yield ' ' + check_line(line)
+                    yield '  ' + check_line(line)
             if tag in ('delete'):
                 for line in self.a[i1:i2]:
-                    yield '-' + check_line(line)
+                    yield '- ' + check_line(line)
             if tag in ('insert'):
                 for line in self.b[j1:j2]:
-                    yield '+' + check_line(line)
+                    yield '+ ' + check_line(line)
             if tag in ('replace'):
                 for line in difflib.ndiff(self.a[i1:i2], self.b[j1:j2]):
                     yield check_line(line)
@@ -132,20 +132,20 @@
                 return line
 
         colored_line = u''
-        state = 'Close'
+        color_closed = True
         for char, char_ref in zip_longest(line, line_ref.strip(), fillvalue=' 
'):
             char_tagged = char
-            if state == 'Close':
+            if color_closed:
                 if char_ref != ' ':
                     char_tagged = '\03{%s}%s' % (self.colors[color], char)
-                    state = 'Open'
-            elif state == 'Open':
+                    color_closed = False
+            else:
                 if char_ref == ' ':
                     char_tagged = '\03{default}%s' % char
-                    state = 'Close'
+                    color_closed = True
             colored_line += char_tagged
 
-        if state == 'Open':
+        if not color_closed:
             colored_line += '\03{default}'
 
         return colored_line
@@ -233,6 +233,10 @@
 
         return blocks
 
+    def print_hunks(self):
+        for hunk in self.hunks:
+            pywikibot.output(hunk.header + hunk.diff_text)
+
     def review_hunks(self):
         "Review hunks."
 

-- 
To view, visit https://gerrit.wikimedia.org/r/168967
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic59df944b4c64b8536a5178e1aabc0b1adfeab47
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: XZise <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to