From 64f64d20c06916f957e860e78cc8074b8a53df86 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Tue, 17 Nov 2020 19:22:56 +0100 Subject: [PATCH] feat: align short path resolves #164 --- segment_path.go | 30 ++++++++++-------------------- segment_path_test.go | 24 ++++++++++++++++-------- 2 files changed, 26 insertions(+), 28 deletions(-) diff --git a/segment_path.go b/segment_path.go index f49a866e..05f07505 100644 --- a/segment_path.go +++ b/segment_path.go @@ -61,10 +61,12 @@ func (pt *path) init(props *properties, env environmentInfo) { func (pt *path) getShortPath() string { pwd := pt.env.getcwd() + if strings.HasPrefix(pwd, "Microsoft.PowerShell.Core\\FileSystem::") { + pwd = strings.Replace(pwd, "Microsoft.PowerShell.Core\\FileSystem::", "", 1) + } mappedLocations := map[string]string{ - "HKCU:": pt.props.getString(WindowsRegistryIcon, "\uE0B1"), - "HKLM:": pt.props.getString(WindowsRegistryIcon, "\uE0B1"), - "Microsoft.PowerShell.Core\\FileSystem::": "", + "HKCU:": pt.props.getString(WindowsRegistryIcon, "\uE0B1"), + "HKLM:": pt.props.getString(WindowsRegistryIcon, "\uE0B1"), pt.env.homeDir(): pt.props.getString(HomeIcon, "~"), } for location, value := range mappedLocations { @@ -76,9 +78,9 @@ func (pt *path) getShortPath() string { } func (pt *path) getAgnosterPath() string { - pwd := pt.env.getcwd() buffer := new(bytes.Buffer) - buffer.WriteString(pt.rootLocation(pwd)) + pwd := pt.getShortPath() + buffer.WriteString(pt.rootLocation()) pathDepth := pt.pathDepth(pwd) for i := 1; i < pathDepth; i++ { buffer.WriteString(fmt.Sprintf("%s%s", pt.props.getString(FolderSeparatorIcon, pt.env.getPathSeperator()), pt.props.getString(FolderIcon, ".."))) @@ -90,7 +92,7 @@ func (pt *path) getAgnosterPath() string { } func (pt *path) getAgnosterFullPath() string { - pwd := pt.env.getcwd() + pwd := pt.getShortPath() pathSeparator := pt.env.getPathSeperator() folderSeparator := pt.props.getString(FolderSeparatorIcon, pathSeparator) if string(pwd[0]) == pathSeparator { @@ -103,23 +105,11 @@ func (pt *path) inHomeDir(pwd string) bool { return strings.HasPrefix(pwd, pt.env.homeDir()) } -func (pt *path) rootLocation(pwd string) string { - // See https://community.idera.com/database-tools/powershell/powertips/b/tips/posts/correcting-powershell-paths - if strings.HasPrefix(pwd, "Microsoft.PowerShell.Core\\FileSystem::") { - pwd = strings.Replace(pwd, "Microsoft.PowerShell.Core\\FileSystem::", "", 1) - } - if pt.inHomeDir(pwd) { - return pt.props.getString(HomeIcon, "~") - } +func (pt *path) rootLocation() string { + pwd := pt.getShortPath() pwd = strings.TrimPrefix(pwd, pt.env.getPathSeperator()) splitted := strings.Split(pwd, pt.env.getPathSeperator()) rootLocation := splitted[0] - mappedLocations := map[string]string{ - "HKCU:": pt.props.getString(WindowsRegistryIcon, "\uE0B1"), - } - if val, ok := mappedLocations[rootLocation]; ok { - return val - } return rootLocation } diff --git a/segment_path_test.go b/segment_path_test.go index 991cd330..106afe8e 100644 --- a/segment_path_test.go +++ b/segment_path_test.go @@ -155,13 +155,14 @@ func TestRootLocationHome(t *testing.T) { } home := "/home/bill/" env := new(MockedEnvironment) - env.On("homeDir", nil).Return("/home/bill") + env.On("homeDir", nil).Return(home) + env.On("getcwd", nil).Return(home) env.On("getPathSeperator", nil).Return("/") path := &path{ env: env, props: props, } - got := path.rootLocation(home) + got := path.rootLocation() assert.EqualValues(t, expected, got) } @@ -171,12 +172,13 @@ func TestRootLocationOutsideHome(t *testing.T) { } env := new(MockedEnvironment) env.On("homeDir", nil).Return("/home/bill") + env.On("getcwd", nil).Return("/usr/error/what") env.On("getPathSeperator", nil).Return("/") path := &path{ env: env, props: props, } - got := path.rootLocation("/usr/error/what") + got := path.rootLocation() assert.EqualValues(t, "usr", got) } @@ -186,12 +188,13 @@ func TestRootLocationWindowsDrive(t *testing.T) { } env := new(MockedEnvironment) env.On("homeDir", nil).Return("C:\\Users\\Bill") + env.On("getcwd", nil).Return("C:\\Program Files\\Go") env.On("getPathSeperator", nil).Return("\\") path := &path{ env: env, props: props, } - got := path.rootLocation("C:\\Program Files\\Go") + got := path.rootLocation() assert.EqualValues(t, "C:", got) } @@ -202,12 +205,13 @@ func TestRootLocationWindowsRegistry(t *testing.T) { } env := new(MockedEnvironment) env.On("homeDir", nil).Return("C:\\Users\\Bill") + env.On("getcwd", nil).Return("HKCU:\\Program Files\\Go") env.On("getPathSeperator", nil).Return("\\") path := &path{ env: env, props: props, } - got := path.rootLocation("HKCU:\\Program Files\\Go") + got := path.rootLocation() assert.EqualValues(t, expected, got) } @@ -218,12 +222,13 @@ func TestRootLocationWindowsPowerShellHome(t *testing.T) { } env := new(MockedEnvironment) env.On("homeDir", nil).Return("C:\\Users\\Bill") + env.On("getcwd", nil).Return("Microsoft.PowerShell.Core\\FileSystem::C:\\Users\\Bill") env.On("getPathSeperator", nil).Return("\\") path := &path{ env: env, props: props, } - got := path.rootLocation("Microsoft.PowerShell.Core\\FileSystem::C:\\Users\\Bill") + got := path.rootLocation() assert.EqualValues(t, expected, got) } @@ -233,12 +238,13 @@ func TestRootLocationWindowsPowerShellOutsideHome(t *testing.T) { } env := new(MockedEnvironment) env.On("homeDir", nil).Return("C:\\Program Files\\Go") + env.On("getcwd", nil).Return("Microsoft.PowerShell.Core\\FileSystem::C:\\Users\\Bill") env.On("getPathSeperator", nil).Return("\\") path := &path{ env: env, props: props, } - got := path.rootLocation("Microsoft.PowerShell.Core\\FileSystem::C:\\Users\\Bill") + got := path.rootLocation() assert.EqualValues(t, "C:", got) } @@ -248,12 +254,13 @@ func TestRootLocationEmptyDir(t *testing.T) { } env := new(MockedEnvironment) env.On("homeDir", nil).Return("/home/bill") + env.On("getcwd", nil).Return("") env.On("getPathSeperator", nil).Return("/") path := &path{ env: env, props: props, } - got := path.rootLocation("") + got := path.rootLocation() assert.EqualValues(t, "", got) } @@ -358,6 +365,7 @@ func TestGetAgnosterFullPath(t *testing.T) { pwd := "/usr/location/whatever" env := new(MockedEnvironment) env.On("getPathSeperator", nil).Return("/") + env.On("homeDir", nil).Return("/usr/home") env.On("getcwd", nil).Return(pwd) path := &path{ env: env,