Skip to content

Remove color escape after newline from diff line #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions difflib/difflib.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ func (d *Differ) StructuredDump(tag byte, x []string, low int, high int) (out []
size := high - low
out = make([]DiffLine, size)
for i := 0; i < size; i++ {
out[i] = NewDiffLine(tag, x[i + low])
out[i] = NewDiffLine(tag, x[i+low])
}
return out
}
Expand Down Expand Up @@ -877,9 +877,8 @@ func WriteUnifiedDiff(writer io.Writer, diff LineDiffParams) error {
equalFormat := " %s"

if diff.Colored {
addFormat = "\x1b[32m+%s" // Foreground Green
removeFormat = "\x1b[31m-%s" // Foreground Red
equalFormat = "\x1b[0m %s" // No Color
addFormat = "\x1b[32m+%s\x1b[0m" // Foreground Green

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does this do, reset the color?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it removes the color code. Since this is a line level diff we should be able to add and remove the color code for each write and have a line level color code. We need to remove it to cover the case where the diff hits EOF and no additional context lines would be written. In that case, we don't want to leave the foreground color in the + or - color or the next addition to that text buffer/string will also be in that color.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it, agreed

removeFormat = "\x1b[31m-%s\x1b[0m" // Foreground Red
}

for _, c := range g {
Expand Down
48 changes: 46 additions & 2 deletions difflib/difflib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
"runtime"
"strings"
"testing"
"./tester"

"github.com/mongodb-forks/go-difflib/difflib/tester"
)

func assertAlmostEqual(t *testing.T, a, b float64, places int) {
Expand Down Expand Up @@ -602,7 +603,7 @@ func TestGetUnifiedDiffString(t *testing.T) {
// Build diff
diff := LineDiffParams{A: SplitLines(A),
FromFile: "file", FromDate: "then",
B: SplitLines(B),
B: SplitLines(B),
ToFile: "tile", ToDate: "now", Eol: "", Context: 1}
// Run test
diffStr, err := GetUnifiedDiffString(diff)
Expand All @@ -614,3 +615,46 @@ func TestGetUnifiedDiffString(t *testing.T) {
t.Fatal("GetUnifiedDiffString failure:", diffStr)
}
}

func TestColoredDiffString(t *testing.T) {
A := "one\ntwo\nthree\nfour"
B := "one\ntwo\nthr33\nfour"

// Build diff
diff := LineDiffParams{
A: SplitLines(A),
B: SplitLines(B),
Context: 3,
Colored: true,
}
// string builder always returns a nil error so it's safe to ignore here
diffStr, err := GetUnifiedDiffString(diff)
if err != nil {
t.Fatal("Failed")
}

if !strings.Contains(diffStr, "\x1b[31m-three\n\x1b[0m") {
t.Fatal("Failed to remove removeFormat formatting")
}

if !strings.Contains(diffStr, "one\n") {
t.Fatal("Neutal Line contained color format")
}

// Reverse diff
diff = LineDiffParams{
A: SplitLines(B),
B: SplitLines(A),
Context: 3,
Colored: true,
}
// string builder always returns a nil error so it's safe to ignore here
diffStr, err = GetUnifiedDiffString(diff)
if err != nil {
t.Fatal("Failed")
}

if !strings.Contains(diffStr, "\x1b[32m+three\n\x1b[0m") {
t.Fatal("Failed to remove addFormat formatting")
}
}