fix(path): validate index before assignment

resolves #3768
This commit is contained in:
Jan De Dobbeleer 2023-04-27 19:59:00 +02:00 committed by Jan De Dobbeleer
parent ca49814058
commit f0c88efb40
2 changed files with 39 additions and 3 deletions

View file

@ -249,7 +249,10 @@ func (pt *Path) getAgnosterPath() string {
for i := 1; i < n; i++ { for i := 1; i < n; i++ {
elements = append(elements, folderIcon) elements = append(elements, folderIcon)
} }
elements = append(elements, splitted[n-1])
if len(splitted) > 0 {
elements = append(elements, splitted[n-1])
}
return pt.colorizePath(pt.root, elements) return pt.colorizePath(pt.root, elements)
} }
@ -299,7 +302,10 @@ func (pt *Path) getLetterPath() string {
letter := pt.getRelevantLetter(splitted[i]) letter := pt.getRelevantLetter(splitted[i])
elements = append(elements, letter) elements = append(elements, letter)
} }
elements = append(elements, splitted[n-1])
if len(splitted) > 0 {
elements = append(elements, splitted[n-1])
}
return pt.colorizePath(pt.root, elements) return pt.colorizePath(pt.root, elements)
} }
@ -349,7 +355,10 @@ func (pt *Path) getUniqueLettersPath(maxWidth int) string {
} }
} }
} }
elements = append(elements, splitted[n-1])
if len(elements) > 0 {
elements = append(elements, splitted[n-1])
}
return pt.colorizePath(pt.root, elements) return pt.colorizePath(pt.root, elements)
} }
@ -592,6 +601,10 @@ func (pt *Path) colorizePath(root string, elements []string) string {
return fmt.Sprintf("<%s>%s</>", cycle[0], element) return fmt.Sprintf("<%s>%s</>", cycle[0], element)
} }
if len(elements) == 0 {
return colorizeElement(root)
}
colorizeSeparator := func() string { colorizeSeparator := func() string {
if skipColorize || !colorSeparator { if skipColorize || !colorSeparator {
return folderSeparator return folderSeparator

View file

@ -198,6 +198,14 @@ func TestAgnosterPathStyles(t *testing.T) {
PathSeparator: "\\", PathSeparator: "\\",
FolderSeparatorIcon: " > ", FolderSeparatorIcon: " > ",
}, },
{
Style: Unique,
Expected: "a",
HomePath: homeDir,
Pwd: "/ab",
PathSeparator: "/",
FolderSeparatorIcon: " > ",
},
{ {
Style: Powerlevel, Style: Powerlevel,
@ -350,6 +358,14 @@ func TestAgnosterPathStyles(t *testing.T) {
PathSeparator: "\\", PathSeparator: "\\",
FolderSeparatorIcon: " > ", FolderSeparatorIcon: " > ",
}, },
{
Style: Letter,
Expected: "w",
HomePath: homeDir,
Pwd: "/whatever",
PathSeparator: "/",
FolderSeparatorIcon: " > ",
},
{ {
Style: Mixed, Style: Mixed,
@ -1092,6 +1108,13 @@ func TestAgnosterPath(t *testing.T) {
Cycle: []string{"blue", "yellow"}, Cycle: []string{"blue", "yellow"},
ColorSeparator: true, ColorSeparator: true,
}, },
{
Case: "Unix one level",
Expected: "mnt",
Home: homeDir,
PWD: "/mnt",
PathSeparator: "/",
},
} }
for _, tc := range cases { for _, tc := range cases {