mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-01-03 15:27:26 -08:00
parent
17181f3bef
commit
9653506e34
|
@ -347,6 +347,9 @@ func (pt *Path) normalize(inputPath string) string {
|
||||||
if goos == environment.WINDOWS || goos == environment.DARWIN {
|
if goos == environment.WINDOWS || goos == environment.DARWIN {
|
||||||
normalized = strings.ToLower(normalized)
|
normalized = strings.ToLower(normalized)
|
||||||
}
|
}
|
||||||
|
if !strings.HasSuffix(normalized, "/") {
|
||||||
|
normalized += "/"
|
||||||
|
}
|
||||||
return normalized
|
return normalized
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -355,13 +358,11 @@ func (pt *Path) replaceMappedLocations(pwd string) string {
|
||||||
pwd = strings.Replace(pwd, "Microsoft.PowerShell.Core\\FileSystem::", "", 1)
|
pwd = strings.Replace(pwd, "Microsoft.PowerShell.Core\\FileSystem::", "", 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
enableMappedLocations := pt.props.GetBool(MappedLocationsEnabled, true)
|
|
||||||
|
|
||||||
// built-in mapped locations, can be disabled
|
|
||||||
mappedLocations := map[string]string{}
|
mappedLocations := map[string]string{}
|
||||||
if enableMappedLocations {
|
if pt.props.GetBool(MappedLocationsEnabled, true) {
|
||||||
mappedLocations["hkcu:"] = pt.props.GetString(WindowsRegistryIcon, "\uF013")
|
mappedLocations[pt.normalize("hkcu:")] = pt.props.GetString(WindowsRegistryIcon, "\uF013")
|
||||||
mappedLocations["hklm:"] = pt.props.GetString(WindowsRegistryIcon, "\uF013")
|
mappedLocations[pt.normalize("hklm:")] = pt.props.GetString(WindowsRegistryIcon, "\uF013")
|
||||||
|
mappedLocations[pt.normalize(pt.env.Home())] = pt.props.GetString(HomeIcon, "~")
|
||||||
}
|
}
|
||||||
|
|
||||||
// merge custom locations with mapped locations
|
// merge custom locations with mapped locations
|
||||||
|
@ -385,16 +386,12 @@ func (pt *Path) replaceMappedLocations(pwd string) string {
|
||||||
normalizedPwd := pt.normalize(pwd)
|
normalizedPwd := pt.normalize(pwd)
|
||||||
for _, key := range keys {
|
for _, key := range keys {
|
||||||
if strings.HasPrefix(normalizedPwd, key) {
|
if strings.HasPrefix(normalizedPwd, key) {
|
||||||
value := mappedLocations[key]
|
replacement := mappedLocations[key]
|
||||||
return value + pwd[len(key):]
|
// -1 as we want to ignore the trailing slash
|
||||||
|
// set by the normalize function
|
||||||
|
return replacement + pwd[len(key)-1:]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// treat this as a built-in mapped location
|
|
||||||
if enableMappedLocations && pt.inHomeDir(pwd) {
|
|
||||||
return pt.props.GetString(HomeIcon, "~") + pwd[len(pt.env.Home()):]
|
|
||||||
}
|
|
||||||
|
|
||||||
return pwd
|
return pwd
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -412,17 +409,6 @@ func (pt *Path) replaceFolderSeparators(pwd string) string {
|
||||||
return pwd
|
return pwd
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pt *Path) inHomeDir(pwd string) bool {
|
|
||||||
home := pt.env.Home()
|
|
||||||
if home == pwd {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
if !strings.HasSuffix(home, pt.env.PathSeparator()) {
|
|
||||||
home += pt.env.PathSeparator()
|
|
||||||
}
|
|
||||||
return strings.HasPrefix(pwd, home)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (pt *Path) rootLocation() string {
|
func (pt *Path) rootLocation() string {
|
||||||
pwd := pt.getPwd()
|
pwd := pt.getPwd()
|
||||||
pwd = strings.TrimPrefix(pwd, pt.env.PathSeparator())
|
pwd = strings.TrimPrefix(pwd, pt.env.PathSeparator())
|
||||||
|
|
|
@ -40,47 +40,11 @@ func renderTemplate(env *mock.MockedEnvironment, segmentTemplate string, context
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
homeBill = "/home/bill"
|
|
||||||
homeJan = "/usr/home/jan"
|
homeJan = "/usr/home/jan"
|
||||||
homeBillWindows = "C:\\Users\\Bill"
|
homeBillWindows = "C:\\Users\\Bill"
|
||||||
levelDir = "/level"
|
levelDir = "/level"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestIsInHomeDir(t *testing.T) {
|
|
||||||
cases := []struct {
|
|
||||||
Case string
|
|
||||||
Expected bool
|
|
||||||
Dir string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
Case: "in home dir",
|
|
||||||
Expected: true,
|
|
||||||
Dir: homeBill,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Case: "in home dir subdirectory",
|
|
||||||
Expected: true,
|
|
||||||
Dir: homeBill + "/go/src/github.com/JanDeDobbeleer/oh-my-posh",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Case: "in similar home dir but not really",
|
|
||||||
Expected: false,
|
|
||||||
Dir: "/home/bill-test",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
for _, tc := range cases {
|
|
||||||
home := homeBill
|
|
||||||
env := new(mock.MockedEnvironment)
|
|
||||||
env.On("Home").Return(home)
|
|
||||||
env.On("PathSeparator").Return("/")
|
|
||||||
path := &Path{
|
|
||||||
env: env,
|
|
||||||
}
|
|
||||||
got := path.inHomeDir(tc.Dir)
|
|
||||||
assert.Equal(t, tc.Expected, got, tc.Case)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestRootLocationHome(t *testing.T) {
|
func TestRootLocationHome(t *testing.T) {
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
Expected string
|
Expected string
|
||||||
|
@ -92,10 +56,10 @@ func TestRootLocationHome(t *testing.T) {
|
||||||
HomeIcon string
|
HomeIcon string
|
||||||
RegistryIcon string
|
RegistryIcon string
|
||||||
}{
|
}{
|
||||||
|
{Expected: "REG", RegistryIcon: "REG", HomePath: "C:\\Users\\Bill", Pwd: "HKCU:\\Program Files\\Go", GOOS: environment.WINDOWS, PathSeparator: "\\"},
|
||||||
{Expected: "~", HomeIcon: "~", HomePath: "/home/bill/", Pwd: "/home/bill/", PathSeparator: "/"},
|
{Expected: "~", HomeIcon: "~", HomePath: "/home/bill/", Pwd: "/home/bill/", PathSeparator: "/"},
|
||||||
{Expected: "usr", HomePath: "/home/bill/", Pwd: "/usr/error/what", PathSeparator: "/"},
|
{Expected: "usr", HomePath: "/home/bill/", Pwd: "/usr/error/what", PathSeparator: "/"},
|
||||||
{Expected: "C:", HomePath: "C:\\Users\\Bill", Pwd: "C:\\Program Files\\Go", GOOS: environment.WINDOWS, PathSeparator: "\\"},
|
{Expected: "C:", HomePath: "C:\\Users\\Bill", Pwd: "C:\\Program Files\\Go", GOOS: environment.WINDOWS, PathSeparator: "\\"},
|
||||||
{Expected: "REG", RegistryIcon: "REG", HomePath: "C:\\Users\\Bill", Pwd: "HKCU:\\Program Files\\Go", GOOS: environment.WINDOWS, PathSeparator: "\\"},
|
|
||||||
{Expected: "~", HomeIcon: "~", HomePath: "C:\\Users\\Bill", Pwd: "Microsoft.PowerShell.Core\\FileSystem::C:\\Users\\Bill", GOOS: environment.WINDOWS, PathSeparator: "\\"},
|
{Expected: "~", HomeIcon: "~", HomePath: "C:\\Users\\Bill", Pwd: "Microsoft.PowerShell.Core\\FileSystem::C:\\Users\\Bill", GOOS: environment.WINDOWS, PathSeparator: "\\"},
|
||||||
{Expected: "C:", HomePath: "C:\\Users\\Jack", Pwd: "Microsoft.PowerShell.Core\\FileSystem::C:\\Users\\Bill", GOOS: environment.WINDOWS, PathSeparator: "\\"},
|
{Expected: "C:", HomePath: "C:\\Users\\Jack", Pwd: "Microsoft.PowerShell.Core\\FileSystem::C:\\Users\\Bill", GOOS: environment.WINDOWS, PathSeparator: "\\"},
|
||||||
{Expected: "", HomePath: "C:\\Users\\Jack", Pwd: "", GOOS: environment.WINDOWS, PathSeparator: "\\"},
|
{Expected: "", HomePath: "C:\\Users\\Jack", Pwd: "", GOOS: environment.WINDOWS, PathSeparator: "\\"},
|
||||||
|
@ -511,14 +475,14 @@ func TestNormalizePath(t *testing.T) {
|
||||||
GOOS string
|
GOOS string
|
||||||
Expected string
|
Expected string
|
||||||
}{
|
}{
|
||||||
{Input: "C:\\Users\\Bob\\Foo", GOOS: environment.LINUX, Expected: "C:/Users/Bob/Foo"},
|
{Input: "C:\\Users\\Bob\\Foo", GOOS: environment.LINUX, Expected: "C:/Users/Bob/Foo/"},
|
||||||
{Input: "C:\\Users\\Bob\\Foo", GOOS: environment.WINDOWS, Expected: "c:/users/bob/foo"},
|
{Input: "C:\\Users\\Bob\\Foo", GOOS: environment.WINDOWS, Expected: "c:/users/bob/foo/"},
|
||||||
{Input: "~\\Bob\\Foo", GOOS: environment.LINUX, Expected: "/usr/home/Bob/Foo"},
|
{Input: "~\\Bob\\Foo", GOOS: environment.LINUX, Expected: "/usr/home/Bob/Foo/"},
|
||||||
{Input: "~\\Bob\\Foo", GOOS: environment.WINDOWS, Expected: "/usr/home/bob/foo"},
|
{Input: "~\\Bob\\Foo", GOOS: environment.WINDOWS, Expected: "/usr/home/bob/foo/"},
|
||||||
{Input: "/foo/~/bar", GOOS: environment.LINUX, Expected: "/foo/~/bar"},
|
{Input: "/foo/~/bar", GOOS: environment.LINUX, Expected: "/foo/~/bar/"},
|
||||||
{Input: "/foo/~/bar", GOOS: environment.WINDOWS, Expected: "/foo/~/bar"},
|
{Input: "/foo/~/bar", GOOS: environment.WINDOWS, Expected: "/foo/~/bar/"},
|
||||||
{Input: "~/baz", GOOS: environment.LINUX, Expected: "/usr/home/baz"},
|
{Input: "~/baz", GOOS: environment.LINUX, Expected: "/usr/home/baz/"},
|
||||||
{Input: "~/baz", GOOS: environment.WINDOWS, Expected: "/usr/home/baz"},
|
{Input: "~/baz", GOOS: environment.WINDOWS, Expected: "/usr/home/baz/"},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range cases {
|
for _, tc := range cases {
|
||||||
|
@ -863,10 +827,10 @@ func TestGetPwd(t *testing.T) {
|
||||||
Pswd string
|
Pswd string
|
||||||
Expected string
|
Expected string
|
||||||
}{
|
}{
|
||||||
|
{MappedLocationsEnabled: true, Pwd: "/usr/home", Expected: "~"},
|
||||||
{MappedLocationsEnabled: true, Pwd: "/usr/home-test", Expected: "/usr/home-test"},
|
{MappedLocationsEnabled: true, Pwd: "/usr/home-test", Expected: "/usr/home-test"},
|
||||||
{MappedLocationsEnabled: true, Pwd: "", Expected: ""},
|
{MappedLocationsEnabled: true, Pwd: "", Expected: ""},
|
||||||
{MappedLocationsEnabled: true, Pwd: "/usr", Expected: "/usr"},
|
{MappedLocationsEnabled: true, Pwd: "/usr", Expected: "/usr"},
|
||||||
{MappedLocationsEnabled: true, Pwd: "/usr/home", Expected: "~"},
|
|
||||||
{MappedLocationsEnabled: true, Pwd: "/usr/home/abc", Expected: "~/abc"},
|
{MappedLocationsEnabled: true, Pwd: "/usr/home/abc", Expected: "~/abc"},
|
||||||
{MappedLocationsEnabled: true, Pwd: "/a/b/c/d", Expected: "#"},
|
{MappedLocationsEnabled: true, Pwd: "/a/b/c/d", Expected: "#"},
|
||||||
{MappedLocationsEnabled: true, Pwd: "/a/b/c/d/e/f/g", Expected: "#/e/f/g"},
|
{MappedLocationsEnabled: true, Pwd: "/a/b/c/d/e/f/g", Expected: "#/e/f/g"},
|
||||||
|
|
Loading…
Reference in a new issue