diff options
author | mjarkk <mkopenga@gmail.com> | 2021-07-23 12:04:23 +0200 |
---|---|---|
committer | mjarkk <mkopenga@gmail.com> | 2021-07-23 12:04:23 +0200 |
commit | fc76b44b4569247462462db0126a739d87781888 (patch) | |
tree | a08786563e1f48586c116d73a1d66074d1833607 /pkg | |
parent | 9a087d04ebaed311e0bd3d9cbd32dd2973f0406c (diff) |
correctly show files with special chars in commit
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/gui/filetree/file_node.go | 4 | ||||
-rw-r--r-- | pkg/gui/presentation/commit_files.go | 1 | ||||
-rw-r--r-- | pkg/gui/presentation/files.go | 12 | ||||
-rw-r--r-- | pkg/utils/lines.go | 12 | ||||
-rw-r--r-- | pkg/utils/slice_test.go | 32 |
5 files changed, 47 insertions, 14 deletions
diff --git a/pkg/gui/filetree/file_node.go b/pkg/gui/filetree/file_node.go index 4df8bf5bf..cb545d391 100644 --- a/pkg/gui/filetree/file_node.go +++ b/pkg/gui/filetree/file_node.go @@ -1,8 +1,6 @@ package filetree import ( - "fmt" - "github.com/jesseduffield/lazygit/pkg/commands/models" ) @@ -182,7 +180,7 @@ func (s *FileNode) NameAtDepth(depth int) string { prevName = join(splitPrevName[depth:]) } - return fmt.Sprintf("%s%s%s", prevName, " → ", name) + return prevName + " → " + name } return name diff --git a/pkg/gui/presentation/commit_files.go b/pkg/gui/presentation/commit_files.go index 94116066a..7e9d4bdfa 100644 --- a/pkg/gui/presentation/commit_files.go +++ b/pkg/gui/presentation/commit_files.go @@ -28,6 +28,7 @@ func GetCommitFileLine(name string, diffName string, commitFile *models.CommitFi } } + name = utils.EscapeSpecialChars(name) if commitFile == nil { return colour.Sprint(name) } diff --git a/pkg/gui/presentation/files.go b/pkg/gui/presentation/files.go index f45ef04d3..8b3aacf56 100644 --- a/pkg/gui/presentation/files.go +++ b/pkg/gui/presentation/files.go @@ -1,8 +1,6 @@ package presentation import ( - "strings" - "github.com/fatih/color" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/theme" @@ -50,15 +48,7 @@ func GetFileLine(hasUnstagedChanges bool, hasStagedChanges bool, name string, di output += restColor.Sprint(" ") } - name = strings.NewReplacer( - "\n", "\\n", - "\r", "\\r", - "\t", "\\t", - "\b", "\\b", - "\f", "\\f", - "\v", "\\v", - ).Replace(name) - output += restColor.Sprint(name) + output += restColor.Sprint(utils.EscapeSpecialChars(name)) if file != nil && file.IsSubmodule(submoduleConfigs) { output += utils.ColoredString(" (submodule)", theme.DefaultTextColor) diff --git a/pkg/utils/lines.go b/pkg/utils/lines.go index 4c654d888..9aea84bff 100644 --- a/pkg/utils/lines.go +++ b/pkg/utils/lines.go @@ -32,3 +32,15 @@ func NormalizeLinefeeds(str string) string { str = strings.Replace(str, "\r", "", -1) return str } + +// EscapeSpecialChars - Replaces all special chars like \n with \\n +func EscapeSpecialChars(str string) string { + return strings.NewReplacer( + "\n", "\\n", + "\r", "\\r", + "\t", "\\t", + "\b", "\\b", + "\f", "\\f", + "\v", "\\v", + ).Replace(str) +} diff --git a/pkg/utils/slice_test.go b/pkg/utils/slice_test.go index 858b1b904..491968cb4 100644 --- a/pkg/utils/slice_test.go +++ b/pkg/utils/slice_test.go @@ -133,3 +133,35 @@ func TestPrevIndex(t *testing.T) { }) } } + +func TestEscapeSpecialChars(t *testing.T) { + type scenario struct { + testName string + input string + expected string + } + + scenarios := []scenario{ + { + "normal string", + "ab", + "ab", + }, + { + "string with a special char", + "a\nb", + "a\\nb", + }, + { + "multiple special chars", + "\n\r\t\b\f\v", + "\\n\\r\\t\\b\\f\\v", + }, + } + + for _, s := range scenarios { + t.Run(s.testName, func(t *testing.T) { + assert.EqualValues(t, s.expected, EscapeSpecialChars(s.input)) + }) + } +} |