From f0c88efb401cd85659d4a47b47ff7b129e58bd6f Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Thu, 27 Apr 2023 19:59:00 +0200 Subject: [PATCH] fix(path): validate index before assignment resolves #3768 --- src/segments/path.go | 19 ++++++++++++++++--- src/segments/path_test.go | 23 +++++++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/src/segments/path.go b/src/segments/path.go index ad49dbdf..64565370 100644 --- a/src/segments/path.go +++ b/src/segments/path.go @@ -249,7 +249,10 @@ func (pt *Path) getAgnosterPath() string { for i := 1; i < n; i++ { 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) } @@ -299,7 +302,10 @@ func (pt *Path) getLetterPath() string { letter := pt.getRelevantLetter(splitted[i]) 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) } @@ -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) } @@ -592,6 +601,10 @@ func (pt *Path) colorizePath(root string, elements []string) string { return fmt.Sprintf("<%s>%s", cycle[0], element) } + if len(elements) == 0 { + return colorizeElement(root) + } + colorizeSeparator := func() string { if skipColorize || !colorSeparator { return folderSeparator diff --git a/src/segments/path_test.go b/src/segments/path_test.go index dc6e048e..e5701e55 100644 --- a/src/segments/path_test.go +++ b/src/segments/path_test.go @@ -198,6 +198,14 @@ func TestAgnosterPathStyles(t *testing.T) { PathSeparator: "\\", FolderSeparatorIcon: " > ", }, + { + Style: Unique, + Expected: "a", + HomePath: homeDir, + Pwd: "/ab", + PathSeparator: "/", + FolderSeparatorIcon: " > ", + }, { Style: Powerlevel, @@ -350,6 +358,14 @@ func TestAgnosterPathStyles(t *testing.T) { PathSeparator: "\\", FolderSeparatorIcon: " > ", }, + { + Style: Letter, + Expected: "w", + HomePath: homeDir, + Pwd: "/whatever", + PathSeparator: "/", + FolderSeparatorIcon: " > ", + }, { Style: Mixed, @@ -1092,6 +1108,13 @@ func TestAgnosterPath(t *testing.T) { Cycle: []string{"blue", "yellow"}, ColorSeparator: true, }, + { + Case: "Unix one level", + Expected: "mnt", + Home: homeDir, + PWD: "/mnt", + PathSeparator: "/", + }, } for _, tc := range cases {