Skip to content

Commit e618ef6

Browse files
committed
Show stats
Multidiff now has the ability to show the number of additions and deletions in the diff
1 parent bf83b3e commit e618ef6

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

multidiff/Render.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def __init__(self, encoder='hexdump', color='ansi', bytes=16, width=None):
2121

2222
self.width = width
2323
self.bytes = bytes
24+
self.stats = [0, 0]
2425

2526
def render(self, model, diff):
2627
'''Render the diff in the given model into a UTF-8 String'''
@@ -33,6 +34,8 @@ def render(self, model, diff):
3334
result.append(data, op[0], self.width, self.bytes, data2)
3435
elif type(data) == str:
3536
result.append(bytes(data, "utf8"), op[0], self.width, self.bytes, bytes(data2, "utf8"))
37+
if result.additions or result.deletions:
38+
self.stats = [result.additions, result.deletions]
3639
if self.bytes != 16:
3740
return result.reformat(result.final(data2), int(self.bytes))
3841
return result.final(data2)
@@ -49,6 +52,8 @@ def diff_render(self, model, diff):
4952
result.append(data1, op[0], self.width, self.bytes, data2)
5053
elif type(data2) == str:
5154
result.append(bytes(data1, "utf8"), op[0], self.width, self.bytes, bytes(data2, "utf8"))
55+
if result.additions or result.deletions:
56+
self.stats = [result.additions, result.deletions]
5257
if self.bytes != 16:
5358
return result.reformat(result.final(data2), int(self.bytes))
5459
return result.final(data2).rstrip()
@@ -65,6 +70,8 @@ class Utf8Encoder():
6570
def __init__(self, highligther):
6671
self.highligther = highligther
6772
self.output = ''
73+
self.additions = 0
74+
self.deletions = 0
6875

6976
def append(self, data, color, width=None, bytes=16, data2=""):
7077
self.output += self.highligther(str(data, 'utf8'), color)
@@ -80,6 +87,8 @@ class HexEncoder():
8087
def __init__(self, highligther):
8188
self.highligther = highligther
8289
self.output = ''
90+
self.additions = 0
91+
self.deletions = 0
8392

8493
def append(self, data, color, width=None, bytes=16, data2=""):
8594
data = str(binascii.hexlify(data),'utf8')
@@ -101,6 +110,8 @@ def __init__(self, highligther):
101110
self.hexrow = ''
102111
self.skipspace = False
103112
self.asciirow = ''
113+
self.additions = 0
114+
self.deletions = 0
104115

105116
def append(self, data, color, width=None, bytes=16, data2=""):
106117
if data2 == "":
@@ -121,6 +132,15 @@ def append(self, data, color, width=None, bytes=16, data2=""):
121132
consumed = self._append(data1[:16 - self.rowlen], color, width, data2[:16 - self.rowlen],)
122133
data2 = data2[consumed:]
123134

135+
def stats(self, string, op, string2):
136+
if op == 'insert':
137+
self.additions += len(string.split())
138+
if op == 'delete':
139+
self.deletions += len(string.split())
140+
if op == 'replace':
141+
self.additions += len(string.split())
142+
self.deletions += len(string2.split())
143+
124144
def _append(self, data, color, width, data2):
125145
if data2 == "":
126146
if len(data) == 0:
@@ -155,13 +175,18 @@ def _append(self, data, color, width, data2):
155175
if len(data2) == 0:
156176
hexs = str(binascii.hexlify(data1), 'utf8')
157177
hexs = ' '.join([hexs[i:i+2] for i in range(0, len(hexs), 2)])
178+
hexs2 = str(binascii.hexlify(data2), 'utf8')
179+
hexs2 = ' '.join([hexs[i:i+2] for i in range(0, len(hexs), 2)])
158180
else:
159181
self._add_hex_space()
160182
#encode to hex and add some spaces
161183
hexs = str(binascii.hexlify(data2), 'utf8')
162184
hexs = ' '.join([hexs[i:i+2] for i in range(0, len(hexs), 2)])
185+
hexs2 = str(binascii.hexlify(data), 'utf8')
186+
hexs2 = ' '.join([hexs[i:i+2] for i in range(0, len(hexs), 2)])
163187

164188
self.hexrow += self.highligther(hexs, color)
189+
self.stats(hexs, color, hexs2)
165190
if width:
166191
if len(self.hexrow) > int(width):
167192
self.hexrow = textwrap.fill(self.hexrow, int(width))

multidiff/StreamView.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def __init__(self, model, encoding='hexdump', mode='sequence', color='ansi', byt
1212
self.bytes = bytes
1313
self.diff = diff
1414
self.differ = None
15+
self.stats = [0, 0]
1516
model.add_listener(self)
1617

1718
def object_added(self, index):
@@ -28,6 +29,7 @@ def diff_added(self, diff):
2829
self.differ = self.render.diff_render(self.model, diff)
2930
else:
3031
self.differ = self.render.render(self.model, diff)
32+
self.stats = self.render.stats
3133
#StreamView is designed to run for long times so we delete
3234
#old objects and diffs to not leak memory
3335
del(self.model.diffs[0])

multidiff/command_line_interface.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def main(args=None):
2323
informat = args.informat if args.informat else 'raw'
2424
files = FileController(m, informat)
2525
files.add_paths(args.file)
26+
print('{} additions, {} deletions'.format(v.stats[0], v.stats[1]))
2627
return v.differ
2728
if args.stdin:
2829
informat = args.informat if args.informat else 'utf8'

0 commit comments

Comments
 (0)