diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2018-08-21 17:39:55 +1000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-08-21 17:39:55 +1000 |
commit | c6a88990607cb036ba97a283586e6092c1e7c5e9 (patch) | |
tree | 7617c2320198b7a657742b977e03d65ab9510c40 /pkg | |
parent | da4c12bf9e50f2e4a03a0a2ae67cd4ba82220963 (diff) | |
parent | 45fea837713e3f471813596209cd78398df115fc (diff) |
Merge pull request #160 from remyabel/157_remove_bom
#157: clean BOM, allowing CSV files to display correctly
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/gui/view_helpers.go | 6 | ||||
-rw-r--r-- | pkg/utils/utils.go | 7 | ||||
-rw-r--r-- | pkg/utils/utils_test.go | 33 |
3 files changed, 45 insertions, 1 deletions
diff --git a/pkg/gui/view_helpers.go b/pkg/gui/view_helpers.go index 46fc79e40..dc669146b 100644 --- a/pkg/gui/view_helpers.go +++ b/pkg/gui/view_helpers.go @@ -7,6 +7,8 @@ import ( "time" "github.com/jesseduffield/gocui" + "github.com/jesseduffield/lazygit/pkg/utils" + "github.com/spkg/bom" ) var cyclableViews = []string{"status", "files", "branches", "commits", "stash"} @@ -217,7 +219,9 @@ func (gui *Gui) renderString(g *gocui.Gui, viewName, s string) error { return nil } v.Clear() - fmt.Fprint(v, s) + output := string(bom.Clean([]byte(s))) + output = utils.NormalizeLinefeeds(output) + fmt.Fprint(v, output) v.Wrap = true return nil }) diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 0333ac45a..511de1af1 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -64,6 +64,13 @@ func TrimTrailingNewline(str string) string { return str } +// NormalizeLinefeeds - Removes all Windows and Mac style line feeds +func NormalizeLinefeeds(str string) string { + str = strings.Replace(str, "\r\n", "\n", -1) + str = strings.Replace(str, "\r", "", -1) + return str +} + // GetProjectRoot returns the path to the root of the project. Only to be used // in testing contexts, as with binaries it's unlikely this path will exist on // the machine diff --git a/pkg/utils/utils_test.go b/pkg/utils/utils_test.go index 58e78cce9..46b264945 100644 --- a/pkg/utils/utils_test.go +++ b/pkg/utils/utils_test.go @@ -81,3 +81,36 @@ func TestTrimTrailingNewline(t *testing.T) { assert.EqualValues(t, s.expected, TrimTrailingNewline(s.str)) } } + +func TestNormalizeLinefeeds(t *testing.T) { + type scenario struct { + byteArray []byte + expected []byte + } + var scenarios = []scenario{ + { + // \r\n + []byte{97, 115, 100, 102, 13, 10}, + []byte{97, 115, 100, 102, 10}, + }, + { + // bash\r\nblah + []byte{97, 115, 100, 102, 13, 10, 97, 115, 100, 102}, + []byte{97, 115, 100, 102, 10, 97, 115, 100, 102}, + }, + { + // \r + []byte{97, 115, 100, 102, 13}, + []byte{97, 115, 100, 102}, + }, + { + // \n + []byte{97, 115, 100, 102, 10}, + []byte{97, 115, 100, 102, 10}, + }, + } + + for _, s := range scenarios { + assert.EqualValues(t, string(s.expected), NormalizeLinefeeds(string(s.byteArray))) + } +} |