1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-06 03:53:59 +02:00

correctly show files with special chars in commit

This commit is contained in:
mjarkk 2021-07-23 12:04:23 +02:00
parent 9a087d04eb
commit fc76b44b45
5 changed files with 47 additions and 14 deletions

View File

@ -1,8 +1,6 @@
package filetree package filetree
import ( import (
"fmt"
"github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/models"
) )
@ -182,7 +180,7 @@ func (s *FileNode) NameAtDepth(depth int) string {
prevName = join(splitPrevName[depth:]) prevName = join(splitPrevName[depth:])
} }
return fmt.Sprintf("%s%s%s", prevName, " → ", name) return prevName + " → " + name
} }
return name return name

View File

@ -28,6 +28,7 @@ func GetCommitFileLine(name string, diffName string, commitFile *models.CommitFi
} }
} }
name = utils.EscapeSpecialChars(name)
if commitFile == nil { if commitFile == nil {
return colour.Sprint(name) return colour.Sprint(name)
} }

View File

@ -1,8 +1,6 @@
package presentation package presentation
import ( import (
"strings"
"github.com/fatih/color" "github.com/fatih/color"
"github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/theme" "github.com/jesseduffield/lazygit/pkg/theme"
@ -50,15 +48,7 @@ func GetFileLine(hasUnstagedChanges bool, hasStagedChanges bool, name string, di
output += restColor.Sprint(" ") output += restColor.Sprint(" ")
} }
name = strings.NewReplacer( output += restColor.Sprint(utils.EscapeSpecialChars(name))
"\n", "\\n",
"\r", "\\r",
"\t", "\\t",
"\b", "\\b",
"\f", "\\f",
"\v", "\\v",
).Replace(name)
output += restColor.Sprint(name)
if file != nil && file.IsSubmodule(submoduleConfigs) { if file != nil && file.IsSubmodule(submoduleConfigs) {
output += utils.ColoredString(" (submodule)", theme.DefaultTextColor) output += utils.ColoredString(" (submodule)", theme.DefaultTextColor)

View File

@ -32,3 +32,15 @@ func NormalizeLinefeeds(str string) string {
str = strings.Replace(str, "\r", "", -1) str = strings.Replace(str, "\r", "", -1)
return str 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)
}

View File

@ -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))
})
}
}