feat(path): replace child locations

relates to #2962
This commit is contained in:
Jan De Dobbeleer 2022-11-04 20:51:34 +01:00 committed by Jan De Dobbeleer
parent efd4b7d832
commit c156d17577
3 changed files with 43 additions and 5 deletions

View file

@ -415,6 +415,10 @@ func (pt *Path) replaceMappedLocations() (string, string) {
for _, key := range keys {
keyRoot, keyRelative := pt.parsePath(key)
matchSubFolders := strings.HasSuffix(keyRelative, "*")
if matchSubFolders && len(keyRelative) > 1 {
keyRelative = keyRelative[0 : len(keyRelative)-1] // remove trailing /* or \*
}
if keyRoot != rootN || !strings.HasPrefix(relativeN, keyRelative) {
continue
}
@ -429,7 +433,7 @@ func (pt *Path) replaceMappedLocations() (string, string) {
return value, strings.Trim(relative, pathSeparator)
}
// match several prefix elements
if overflow[0:1] == pt.env.PathSeparator() {
if matchSubFolders || overflow[0:1] == pt.env.PathSeparator() {
return value, strings.Trim(overflow, pathSeparator)
}
}

View file

@ -1335,3 +1335,36 @@ func TestNormalizePath(t *testing.T) {
assert.Equal(t, tc.Expected, got)
}
}
func TestReplaceMappedLocations(t *testing.T) {
cases := []struct {
Case string
Pwd string
Expected string
}{
{Pwd: "/f/g/h", Expected: "/f/g/h"},
{Pwd: "/f/g/h/e", Expected: "^/e"},
{Pwd: "/a/b/c/d", Expected: "#"},
{Pwd: "/a/b/c/d/e", Expected: "#/e"},
}
for _, tc := range cases {
env := new(mock.MockedEnvironment)
env.On("PathSeparator").Return("/")
env.On("Pwd").Return(tc.Pwd)
env.On("Shell").Return(shell.FISH)
env.On("GOOS").Return(environment.DARWIN)
path := &Path{
env: env,
props: properties.Map{
MappedLocationsEnabled: false,
MappedLocations: map[string]string{
"/a/b/c/d": "#",
"/f/g/h/*": "^",
},
},
}
path.setPaths()
assert.Equal(t, tc.Expected, path.pwd)
}
}

View file

@ -63,12 +63,13 @@ For example, to swap out `C:\Users\Leet\GitHub` with a GitHub icon, you can do t
### Notes
- To make mapped Locations work cross-platform, you should use `/` as the path separator, Oh My Posh will
- To make mapped locations work cross-platform, use `/` as the path separator, Oh My Posh will
automatically match effective separators based on the running operating system.
- If you want to match all child directories, you can use `*` as a wildcard, for example:
`"C:/Users/Bill/*": "$"` will turn `C:/Users/Bill/Downloads` into `$/Downloads`.
- The character `~` at the start of a mapped location will match the user's home directory.
- The match is case-insensitive on Windows and macOS, but case-sensitive on other operating systems.
This means that for user Bill, who has a user account `Bill` on Windows and `bill` on Linux, `~/Foo` might match
- The match is case-insensitive on Windows and macOS, but case-sensitive on other operating systems. This means that for
user Bill, who has a user account `Bill` on Windows and `bill` on Linux, `~/Foo` might match
`C:\Users\Bill\Foo` or `C:\Users\Bill\foo` on Windows but only `/home/bill/Foo` on Linux.
## Style