File tree Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change @@ -1412,6 +1412,29 @@ func (dmp *DiffMatchPatch) DiffPrettyHtml(diffs []Diff) string {
1412
1412
return buff .String ()
1413
1413
}
1414
1414
1415
+ // DiffPrettyText converts a []Diff into a colored text report.
1416
+ func (dmp * DiffMatchPatch ) DiffPrettyText (diffs []Diff ) string {
1417
+ var buff bytes.Buffer
1418
+ for _ , diff := range diffs {
1419
+ text := diff .Text
1420
+
1421
+ switch diff .Type {
1422
+ case DiffInsert :
1423
+ _ , _ = buff .WriteString ("\x1b [32m" )
1424
+ _ , _ = buff .WriteString (text )
1425
+ _ , _ = buff .WriteString ("\x1b [0m" )
1426
+ case DiffDelete :
1427
+ _ , _ = buff .WriteString ("\x1b [31m" )
1428
+ _ , _ = buff .WriteString (text )
1429
+ _ , _ = buff .WriteString ("\x1b [0m" )
1430
+ case DiffEqual :
1431
+ _ , _ = buff .WriteString (text )
1432
+ }
1433
+ }
1434
+
1435
+ return buff .String ()
1436
+ }
1437
+
1415
1438
// DiffText1 computes and returns the source text (all equalities and deletions).
1416
1439
func (dmp * DiffMatchPatch ) DiffText1 (diffs []Diff ) string {
1417
1440
//StringBuilder text = new StringBuilder()
Original file line number Diff line number Diff line change @@ -746,6 +746,17 @@ func Test_diffPrettyHtml(t *testing.T) {
746
746
dmp .DiffPrettyHtml (diffs ))
747
747
}
748
748
749
+ func Test_diffPrettyText (t * testing.T ) {
750
+ dmp := New ()
751
+ // Pretty print.
752
+ diffs := []Diff {
753
+ Diff {DiffEqual , "a\n " },
754
+ Diff {DiffDelete , "<B>b</B>" },
755
+ Diff {DiffInsert , "c&d" }}
756
+ assert .Equal (t , "a\n \x1b [31m<B>b</B>\x1b [0m\x1b [32mc&d\x1b [0m" ,
757
+ dmp .DiffPrettyText (diffs ))
758
+ }
759
+
749
760
func Test_diffText (t * testing.T ) {
750
761
dmp := New ()
751
762
// Compute the source and destination texts.
You can’t perform that action at this time.
0 commit comments