Skip to content

Commit

Permalink
feat(path): correct drive root on Windows
Browse files Browse the repository at this point in the history
resolves #534
  • Loading branch information
JanDeDobbeleer committed Mar 17, 2021
1 parent fcf1cc3 commit d3252ac
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/segment_command_test.go
@@ -1,3 +1,5 @@
// +build !windows

package main

import (
Expand Down
22 changes: 17 additions & 5 deletions src/segment_path.go
Expand Up @@ -69,18 +69,24 @@ func (pt *path) string() string {
default:
return fmt.Sprintf("Path style: %s is not available", style)
}

formattedPath = pt.formatWindowsDrive(formattedPath)
if pt.props.getBool(EnableHyperlink, false) {
// wsl check
if pt.env.isWsl() {
cwd, _ = pt.env.runCommand("wslpath", "-m", cwd)
}
return fmt.Sprintf("[%s](file://%s)", formattedPath, cwd)
}

return formattedPath
}

func (pt *path) formatWindowsDrive(pwd string) string {
if pt.env.getRuntimeGOOS() != windowsPlatform || !strings.HasSuffix(pwd, ":") {
return pwd
}
return pwd + "\\"
}

func (pt *path) init(props *properties, env environmentInfo) {
pt.props = props
pt.env = env
Expand Down Expand Up @@ -210,6 +216,9 @@ func (pt *path) replaceMappedLocations(pwd string) string {

func (pt *path) replaceFolderSeparators(pwd string) string {
defaultSeparator := pt.env.getPathSeperator()
if pwd == defaultSeparator {
return pwd
}
folderSeparator := pt.props.getString(FolderSeparatorIcon, defaultSeparator)
if folderSeparator == defaultSeparator {
return pwd
Expand Down Expand Up @@ -244,16 +253,19 @@ func (pt *path) pathDepth(pwd string) int {

// Base returns the last element of path.
// Trailing path separators are removed before extracting the last element.
// If the path is empty, Base returns ".".
// If the path consists entirely of separators, Base returns a single separator.
func base(path string, env environmentInfo) string {
if path == "" {
return "."
if path == "/" {
return path
}
volumeName := filepath.VolumeName(path)
// Strip trailing slashes.
for len(path) > 0 && string(path[len(path)-1]) == env.getPathSeperator() {
path = path[0 : len(path)-1]
}
if volumeName == path {
return path
}
// Throw away volume name
path = path[len(filepath.VolumeName(path)):]
// Find the last element
Expand Down
21 changes: 16 additions & 5 deletions src/segment_path_test.go
Expand Up @@ -269,6 +269,7 @@ func TestAgnosterPathStyles(t *testing.T) {
HomeIcon string
FolderSeparatorIcon string
Style string
GOOS string
}{
{Style: AgnosterFull, Expected: "usr > location > whatever", HomePath: "/usr/home", Pwd: "/usr/location/whatever", PathSeperator: "/", FolderSeparatorIcon: " > "},
{Style: AgnosterShort, Expected: "usr > .. > man", HomePath: "/usr/home", Pwd: "/usr/location/whatever/man", PathSeperator: "/", FolderSeparatorIcon: " > "},
Expand All @@ -289,6 +290,7 @@ func TestAgnosterPathStyles(t *testing.T) {
env.On("getPathSeperator", nil).Return(tc.PathSeperator)
env.On("homeDir", nil).Return(tc.HomePath)
env.On("getcwd", nil).Return(tc.Pwd)
env.On("getRuntimeGOOS", nil).Return(tc.GOOS)
args := &args{
PSWD: &tc.Pswd,
}
Expand All @@ -315,7 +317,10 @@ func TestGetFullPath(t *testing.T) {
Pswd string
Expected string
DisableMappedLocations bool
GOOS string
PathSeparator string
}{
{Style: Full, FolderSeparatorIcon: "|", Pwd: "/", Expected: "/"},
{Style: Full, Pwd: "", Expected: ""},
{Style: Full, Pwd: "/", Expected: "/"},
{Style: Full, Pwd: "/usr/home", Expected: "~"},
Expand All @@ -324,32 +329,38 @@ func TestGetFullPath(t *testing.T) {
{Style: Full, Pwd: "/a/b/c/d", Expected: "/a/b/c/d"},

{Style: Full, FolderSeparatorIcon: "|", Pwd: "", Expected: ""},
{Style: Full, FolderSeparatorIcon: "|", Pwd: "/", Expected: "|"},
{Style: Full, FolderSeparatorIcon: "|", Pwd: "/usr/home", Expected: "~"},
{Style: Full, FolderSeparatorIcon: "|", Pwd: "/usr/home", Expected: "|usr|home", DisableMappedLocations: true},
{Style: Full, FolderSeparatorIcon: "|", Pwd: "/usr/home/abc", Expected: "~|abc"},
{Style: Full, FolderSeparatorIcon: "|", Pwd: "/a/b/c/d", Expected: "|a|b|c|d"},

{Style: Folder, Pwd: "", Expected: "."},
{Style: Folder, Pwd: "", Expected: ""},
{Style: Folder, Pwd: "/", Expected: "/"},
{Style: Folder, Pwd: "/usr/home", Expected: "~"},
{Style: Folder, Pwd: "/usr/home", Expected: "home", DisableMappedLocations: true},
{Style: Folder, Pwd: "/usr/home/abc", Expected: "abc"},
{Style: Folder, Pwd: "/a/b/c/d", Expected: "d"},

{Style: Folder, FolderSeparatorIcon: "|", Pwd: "", Expected: "."},
{Style: Folder, FolderSeparatorIcon: "|", Pwd: "/", Expected: "|"},
{Style: Folder, FolderSeparatorIcon: "|", Pwd: "", Expected: ""},
{Style: Folder, FolderSeparatorIcon: "|", Pwd: "/", Expected: "/"},
{Style: Folder, FolderSeparatorIcon: "|", Pwd: "/usr/home", Expected: "~"},
{Style: Folder, FolderSeparatorIcon: "|", Pwd: "/usr/home", Expected: "home", DisableMappedLocations: true},
{Style: Folder, FolderSeparatorIcon: "|", Pwd: "/usr/home/abc", Expected: "abc"},
{Style: Folder, FolderSeparatorIcon: "|", Pwd: "/a/b/c/d", Expected: "d"},

{Style: Folder, FolderSeparatorIcon: "\\", Pwd: "C:\\", Expected: "C:\\", PathSeparator: "\\", GOOS: windowsPlatform},
{Style: Full, FolderSeparatorIcon: "\\", Pwd: "C:\\Users\\Jan", Expected: "C:\\Users\\Jan", PathSeparator: "\\", GOOS: windowsPlatform},
}

for _, tc := range cases {
env := new(MockedEnvironment)
env.On("getPathSeperator", nil).Return("/")
if len(tc.PathSeparator) == 0 {
tc.PathSeparator = "/"
}
env.On("getPathSeperator", nil).Return(tc.PathSeparator)
env.On("homeDir", nil).Return("/usr/home")
env.On("getcwd", nil).Return(tc.Pwd)
env.On("getRuntimeGOOS", nil).Return(tc.GOOS)
args := &args{
PSWD: &tc.Pswd,
}
Expand Down

0 comments on commit d3252ac

Please sign in to comment.