diff --git a/pkg/gui/view_helpers.go b/pkg/gui/view_helpers.go index 5a46fc383..c99609e3a 100644 --- a/pkg/gui/view_helpers.go +++ b/pkg/gui/view_helpers.go @@ -7,6 +7,7 @@ import ( "time" "github.com/jesseduffield/gocui" + "github.com/jesseduffield/lazygit/pkg/utils" "github.com/spkg/bom" ) @@ -218,8 +219,7 @@ func (gui *Gui) renderString(g *gocui.Gui, viewName, s string) error { return nil } v.Clear() - output := string(bom.Clean([]byte(s))) - output = strings.Replace(output, "\r", "\r\n", -1) + output := utils.NormalizeLinefeeds(string(bom.Clean([]byte(s)))) fmt.Fprint(v, output) v.Wrap = true return nil diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 68438246a..d53ddc6dd 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -63,3 +63,9 @@ func TrimTrailingNewline(str string) string { } return str } + +func NormalizeLinefeeds(str string) string { + str = strings.Replace(str, "\r\n", "\n", -1) + str = strings.Replace(str, "\r", "\n", -1) + return str +} diff --git a/pkg/utils/utils_test.go b/pkg/utils/utils_test.go new file mode 100644 index 000000000..3a9f6817b --- /dev/null +++ b/pkg/utils/utils_test.go @@ -0,0 +1,34 @@ +package utils + +import "testing" + +var testCases = []struct { + Input []byte + Expected []byte +}{ + { + // \r\n + Input: []byte{97, 115, 100, 102, 13, 10}, + Expected: []byte{97, 115, 100, 102, 10}, + }, + { + // \r + Input: []byte{97, 115, 100, 102, 13}, + Expected: []byte{97, 115, 100, 102, 10}, + }, + { + // \n + Input: []byte{97, 115, 100, 102, 10}, + Expected: []byte{97, 115, 100, 102, 10}, + }, + +} + +func TestNormalizeLinefeeds(t *testing.T) { + + for _, tc := range testCases { + if NormalizeLinefeeds(string(tc.Input)) != string(tc.Expected) { + t.Error("Error") + } + } +} diff --git a/test/repos/bom.sh b/test/repos/bom.sh index 9599f80e6..e8642491a 100644 --- a/test/repos/bom.sh +++ b/test/repos/bom.sh @@ -3,6 +3,18 @@ set -ex; rm -rf repo; mkdir repo; cd repo git init +cat <> windowslf.txt +asdf +asdf +EOT + +unix2dos windowslf.txt + +cat <> linuxlf.txt +asdf +asdf +EOT + cat <> bomtest.txt A,B,C,D,E F,G,H,I,J