Generate generates a diff between a and b, in color.
(a, b string)
| 24 | |
| 25 | // Generate generates a diff between a and b, in color. |
| 26 | func Generate(a, b string) string { |
| 27 | dmp := diffmatchpatch.New() |
| 28 | |
| 29 | wSrc, wDst, warray := dmp.DiffLinesToRunes(a, b) |
| 30 | diffs := dmp.DiffMainRunes(wSrc, wDst, false) |
| 31 | diffs = dmp.DiffCharsToLines(diffs, warray) |
| 32 | var buff bytes.Buffer |
| 33 | for _, diff := range diffs { |
| 34 | text := diff.Text |
| 35 | |
| 36 | switch diff.Type { |
| 37 | case diffmatchpatch.DiffInsert: |
| 38 | _, _ = buff.WriteString("\x1b[32m") |
| 39 | _, _ = buff.WriteString(prefixLines(text, "+")) |
| 40 | _, _ = buff.WriteString("\x1b[0m") |
| 41 | case diffmatchpatch.DiffDelete: |
| 42 | _, _ = buff.WriteString("\x1b[31m") |
| 43 | _, _ = buff.WriteString(prefixLines(text, "-")) |
| 44 | _, _ = buff.WriteString("\x1b[0m") |
| 45 | case diffmatchpatch.DiffEqual: |
| 46 | _, _ = buff.WriteString(prefixLines(text, " ")) |
| 47 | } |
| 48 | } |
| 49 | return buff.String() |
| 50 | } |
| 51 | |
| 52 | func prefixLines(s, prefix string) string { |
| 53 | var buf bytes.Buffer |
no test coverage detected