From 47265b1bc19714ca6ff6900d65657502fc6f714a Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Wed, 3 Aug 2022 08:39:49 +0200 Subject: [PATCH] fix(path): map Parent correctly --- src/segments/path.go | 13 +++++++++++- src/segments/path_test.go | 36 ++++++++++++++++++++++++++++++++++ website/docs/segments/path.mdx | 2 +- 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/segments/path.go b/src/segments/path.go index 0c307483..1bf48e5b 100644 --- a/src/segments/path.go +++ b/src/segments/path.go @@ -109,7 +109,18 @@ func (pt *Path) Enabled() bool { } func (pt *Path) Parent() string { - return filepath.Dir(pt.pwd) + if pt.pwd == pt.env.Home() { + return "" + } + parent := filepath.Dir(pt.pwd) + if pt.pwd == parent { + return "" + } + separator := pt.env.PathSeparator() + if parent == pt.rootLocation() || parent == separator { + separator = "" + } + return pt.replaceMappedLocations(parent) + separator } func (pt *Path) formatWindowsDrive(pwd string) string { diff --git a/src/segments/path_test.go b/src/segments/path_test.go index 524694d7..fc164f7f 100644 --- a/src/segments/path_test.go +++ b/src/segments/path_test.go @@ -5,6 +5,7 @@ import ( "oh-my-posh/mock" "oh-my-posh/properties" "oh-my-posh/template" + "runtime" "strings" "testing" @@ -112,6 +113,41 @@ func TestRootLocationHome(t *testing.T) { } } +func TestParent(t *testing.T) { + // there's no Windows support/validation for this just yet + // mainly due to root being a special case + if runtime.GOOS == environment.WINDOWS { + return + } + cases := []struct { + Case string + Expected string + HomePath string + Pwd string + PathSeparator string + }{ + {Case: "Home folder", Expected: "", HomePath: "/home/bill", Pwd: "/home/bill", PathSeparator: "/"}, + {Case: "Inside home folder", Expected: "~/", HomePath: "/home/bill", Pwd: "/home/bill/test", PathSeparator: "/"}, + {Case: "Root", Expected: "", HomePath: "/home/bill", Pwd: "/", PathSeparator: "/"}, + {Case: "Root + 1", Expected: "/", HomePath: "/home/bill", Pwd: "/usr", PathSeparator: "/"}, + } + for _, tc := range cases { + env := new(mock.MockedEnvironment) + env.On("Home").Return(tc.HomePath) + env.On("Pwd").Return(tc.Pwd) + env.On("Flags").Return(&environment.Flags{}) + env.On("PathSeparator").Return(tc.PathSeparator) + env.On("GOOS").Return(environment.DARWIN) + path := &Path{ + env: env, + props: properties.Map{}, + } + path.pwd = tc.Pwd + got := path.Parent() + assert.EqualValues(t, tc.Expected, got, tc.Case) + } +} + func TestIsInHomeDirFalse(t *testing.T) { home := homeBill env := new(mock.MockedEnvironment) diff --git a/website/docs/segments/path.mdx b/website/docs/segments/path.mdx index 8bddb8cd..d07260da 100644 --- a/website/docs/segments/path.mdx +++ b/website/docs/segments/path.mdx @@ -147,7 +147,7 @@ folders at the same level, so if `C:\projectA\dev` and `C:\projectB\dev` exist, ### Properties - `.Path`: `string` - the current directory (based on the `style` property) -- `.Parent`: `string` - the current directory's parent folder +- `.Parent`: `string` - the current directory's parent folder (designed for use with style `folder`) - `.Location`: `string` - the current directory (raw value) - `.StackCount`: `int` - the stack count - `.Writable`: `boolean` - is the current directory writable by the user or not