@@ -21,6 +21,7 @@ def __init__(self, encoder='hexdump', color='ansi', bytes=16, width=None):
21
21
22
22
self .width = width
23
23
self .bytes = bytes
24
+ self .stats = [0 , 0 ]
24
25
25
26
def render (self , model , diff ):
26
27
'''Render the diff in the given model into a UTF-8 String'''
@@ -33,6 +34,8 @@ def render(self, model, diff):
33
34
result .append (data , op [0 ], self .width , self .bytes , data2 )
34
35
elif type (data ) == str :
35
36
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 ]
36
39
if self .bytes != 16 :
37
40
return result .reformat (result .final (data2 ), int (self .bytes ))
38
41
return result .final (data2 )
@@ -49,6 +52,8 @@ def diff_render(self, model, diff):
49
52
result .append (data1 , op [0 ], self .width , self .bytes , data2 )
50
53
elif type (data2 ) == str :
51
54
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 ]
52
57
if self .bytes != 16 :
53
58
return result .reformat (result .final (data2 ), int (self .bytes ))
54
59
return result .final (data2 ).rstrip ()
@@ -65,6 +70,8 @@ class Utf8Encoder():
65
70
def __init__ (self , highligther ):
66
71
self .highligther = highligther
67
72
self .output = ''
73
+ self .additions = 0
74
+ self .deletions = 0
68
75
69
76
def append (self , data , color , width = None , bytes = 16 , data2 = "" ):
70
77
self .output += self .highligther (str (data , 'utf8' ), color )
@@ -80,6 +87,8 @@ class HexEncoder():
80
87
def __init__ (self , highligther ):
81
88
self .highligther = highligther
82
89
self .output = ''
90
+ self .additions = 0
91
+ self .deletions = 0
83
92
84
93
def append (self , data , color , width = None , bytes = 16 , data2 = "" ):
85
94
data = str (binascii .hexlify (data ),'utf8' )
@@ -101,6 +110,8 @@ def __init__(self, highligther):
101
110
self .hexrow = ''
102
111
self .skipspace = False
103
112
self .asciirow = ''
113
+ self .additions = 0
114
+ self .deletions = 0
104
115
105
116
def append (self , data , color , width = None , bytes = 16 , data2 = "" ):
106
117
if data2 == "" :
@@ -121,6 +132,15 @@ def append(self, data, color, width=None, bytes=16, data2=""):
121
132
consumed = self ._append (data1 [:16 - self .rowlen ], color , width , data2 [:16 - self .rowlen ],)
122
133
data2 = data2 [consumed :]
123
134
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
+
124
144
def _append (self , data , color , width , data2 ):
125
145
if data2 == "" :
126
146
if len (data ) == 0 :
@@ -155,13 +175,18 @@ def _append(self, data, color, width, data2):
155
175
if len (data2 ) == 0 :
156
176
hexs = str (binascii .hexlify (data1 ), 'utf8' )
157
177
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 )])
158
180
else :
159
181
self ._add_hex_space ()
160
182
#encode to hex and add some spaces
161
183
hexs = str (binascii .hexlify (data2 ), 'utf8' )
162
184
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 )])
163
187
164
188
self .hexrow += self .highligther (hexs , color )
189
+ self .stats (hexs , color , hexs2 )
165
190
if width :
166
191
if len (self .hexrow ) > int (width ):
167
192
self .hexrow = textwrap .fill (self .hexrow , int (width ))
0 commit comments