@@ -96,7 +96,6 @@ pub const DiffFormatter = struct {
9696 false ,
9797 false ,
9898 true ,
99- true ,
10099 );
101100 buffered_writer .flush () catch unreachable ;
102101
@@ -114,7 +113,6 @@ pub const DiffFormatter = struct {
114113 false ,
115114 false ,
116115 true ,
117- true ,
118116 );
119117 buffered_writer .flush () catch unreachable ;
120118 }
@@ -132,14 +130,10 @@ pub const DiffFormatter = struct {
132130 return ;
133131 }
134132
135- const equal_fmt = "<d>{s}<r>" ;
136- const delete_fmt = "<red>{s}<r>" ;
137- const insert_fmt = "<green>{s}<r>" ;
138-
139133 switch (this .received .determineDiffMethod (this .expected , this .globalObject )) {
140134 .none = > {
141135 const fmt = "Expected: <green>{any}<r>\n Received: <red>{any}<r>" ;
142- var formatter = JSC.ZigConsoleClient.Formatter { .globalThis = this .globalObject , . quote_strings = true };
136+ var formatter = JSC.ZigConsoleClient.Formatter { .globalThis = this .globalObject };
143137 if (Output .enable_ansi_colors ) {
144138 try writer .print (Output .prettyFmt (fmt , true ), .{
145139 this .expected .toFmt (this .globalObject , & formatter ),
@@ -160,21 +154,49 @@ pub const DiffFormatter = struct {
160154 var diffs = try dmp .diff (default_allocator , received_slice , expected_slice , false );
161155 defer diffs .deinit (default_allocator );
162156
163- try writer .writeAll (Output .prettyFmt ("Expected: " , true ));
157+ const equal_fmt = "<d>{s}<r>" ;
158+ const delete_fmt = "<red>{s}<r>" ;
159+ const insert_fmt = "<green>{s}<r>" ;
160+
161+ try writer .writeAll ("Expected: " );
164162 for (diffs .items ) | df | {
165163 switch (df .operation ) {
166164 .delete = > continue ,
167- .insert = > try writer .print (Output .prettyFmt (insert_fmt , true ), .{df .text }),
168- .equal = > try writer .print (Output .prettyFmt (equal_fmt , true ), .{df .text }),
165+ .insert = > {
166+ if (Output .enable_ansi_colors ) {
167+ try writer .print (Output .prettyFmt (insert_fmt , true ), .{df .text });
168+ } else {
169+ try writer .print (Output .prettyFmt (insert_fmt , false ), .{df .text });
170+ }
171+ },
172+ .equal = > {
173+ if (Output .enable_ansi_colors ) {
174+ try writer .print (Output .prettyFmt (equal_fmt , true ), .{df .text });
175+ } else {
176+ try writer .print (Output .prettyFmt (equal_fmt , false ), .{df .text });
177+ }
178+ },
169179 }
170180 }
171181
172- try writer .writeAll (Output . prettyFmt ( "\n Received: " , true ) );
182+ try writer .writeAll ("\n Received: " );
173183 for (diffs .items ) | df | {
174184 switch (df .operation ) {
175185 .insert = > continue ,
176- .delete = > try writer .print (Output .prettyFmt (delete_fmt , true ), .{df .text }),
177- .equal = > try writer .print (Output .prettyFmt (equal_fmt , true ), .{df .text }),
186+ .delete = > {
187+ if (Output .enable_ansi_colors ) {
188+ try writer .print (Output .prettyFmt (delete_fmt , true ), .{df .text });
189+ } else {
190+ try writer .print (Output .prettyFmt (delete_fmt , false ), .{df .text });
191+ }
192+ },
193+ .equal = > {
194+ if (Output .enable_ansi_colors ) {
195+ try writer .print (Output .prettyFmt (equal_fmt , true ), .{df .text });
196+ } else {
197+ try writer .print (Output .prettyFmt (equal_fmt , false ), .{df .text });
198+ }
199+ },
178200 }
179201 }
180202 return ;
@@ -185,34 +207,74 @@ pub const DiffFormatter = struct {
185207 var diffs = try dmp .diffLines (default_allocator , received_slice , expected_slice );
186208 defer diffs .deinit (default_allocator );
187209
210+ const equal_fmt = "<d> {s}<r>" ;
211+ const delete_fmt = "<red>+ {s}<r>" ;
212+ const insert_fmt = "<green>- {s}<r>" ;
213+
188214 var insert_count : usize = 0 ;
189215 var delete_count : usize = 0 ;
190216
191217 for (diffs .items ) | df | {
218+ var prev : usize = 0 ;
219+ var curr : usize = 0 ;
192220 switch (df .operation ) {
193221 .equal = > {
194- try writer .print (Output .prettyFmt (equal_fmt , true ), .{df .text });
222+ while (curr < df .text .len ) {
223+ if (curr == df .text .len - 1 or df .text [curr ] == '\n ' and curr != 0 ) {
224+ if (Output .enable_ansi_colors ) {
225+ try writer .print (Output .prettyFmt (equal_fmt , true ), .{df .text [prev .. curr + 1 ]});
226+ } else {
227+ try writer .print (Output .prettyFmt (equal_fmt , false ), .{df .text [prev .. curr + 1 ]});
228+ }
229+ prev = curr + 1 ;
230+ }
231+ curr += 1 ;
232+ }
195233 },
196234 .insert = > {
197- for (df .text ) | c | {
198- if (c == '\n ' ) insert_count += 1 ;
235+ while (curr < df .text .len ) {
236+ if (curr == df .text .len - 1 or df .text [curr ] == '\n ' and curr != 0 ) {
237+ insert_count += 1 ;
238+ if (Output .enable_ansi_colors ) {
239+ try writer .print (Output .prettyFmt (insert_fmt , true ), .{df .text [prev .. curr + 1 ]});
240+ } else {
241+ try writer .print (Output .prettyFmt (insert_fmt , false ), .{df .text [prev .. curr + 1 ]});
242+ }
243+ prev = curr + 1 ;
244+ }
245+ curr += 1 ;
199246 }
200- try writer .print (Output .prettyFmt (insert_fmt , true ), .{df .text });
201247 },
202248 .delete = > {
203- for (df .text ) | c | {
204- if (c == '\n ' ) delete_count += 1 ;
249+ while (curr < df .text .len ) {
250+ if (curr == df .text .len - 1 or df .text [curr ] == '\n ' and curr != 0 ) {
251+ delete_count += 1 ;
252+ if (Output .enable_ansi_colors ) {
253+ try writer .print (Output .prettyFmt (delete_fmt , true ), .{df .text [prev .. curr + 1 ]});
254+ } else {
255+ try writer .print (Output .prettyFmt (delete_fmt , false ), .{df .text [prev .. curr + 1 ]});
256+ }
257+ prev = curr + 1 ;
258+ }
259+ curr += 1 ;
205260 }
206- try writer .print (Output .prettyFmt (delete_fmt , true ), .{df .text });
207261 },
208262 }
209263 }
210264
211- try writer .print (Output .prettyFmt ("\n\n <green>- Expected - {d}<r>\n " , true ), .{insert_count });
212- try writer .print (Output .prettyFmt ("<red>+ Received + {d}<r>" , true ), .{delete_count });
265+ if (Output .enable_ansi_colors ) {
266+ try writer .print (Output .prettyFmt ("\n\n <green>- Expected - {d}<r>\n " , true ), .{insert_count });
267+ try writer .print (Output .prettyFmt ("<red>+ Received + {d}<r>" , true ), .{delete_count });
268+ return ;
269+ }
270+ try writer .print ("\n\n - Expected - {d}\n " , .{insert_count });
271+ try writer .print ("+ Received + {d}" , .{delete_count });
213272 return ;
214273 },
215- .word = > {},
274+ .word = > {
275+ // not implemented
276+ // https://github.com/google/diff-match-patch/wiki/Line-or-Word-Diffs#word-mode
277+ },
216278 }
217279 return ;
218280 }
@@ -502,7 +564,7 @@ pub const Expect = struct {
502564 if (pass ) return thisValue ;
503565
504566 // handle failure
505- var formatter = JSC.ZigConsoleClient.Formatter { .globalThis = globalObject , . quote_strings = true };
567+ var formatter = JSC.ZigConsoleClient.Formatter { .globalThis = globalObject };
506568 if (not ) {
507569 const signature = comptime getSignature ("toBe" , "<green>expected<r>" , true );
508570 const fmt = signature ++ "\n\n Expected: not <green>{any}<r>\n " ;
@@ -1191,7 +1253,7 @@ pub const Expect = struct {
11911253 if (pass ) return thisValue ;
11921254
11931255 // handle failure
1194- var formatter = JSC.ZigConsoleClient.Formatter { .globalThis = globalObject , . quote_strings = true };
1256+ var formatter = JSC.ZigConsoleClient.Formatter { .globalThis = globalObject };
11951257 if (not ) {
11961258 if (expected_property != null ) {
11971259 const signature = comptime getSignature ("toHaveProperty" , "<green>path<r><d>, <r><green>value<r>" , true );
0 commit comments