feat(path): add agnoster right

This commit is contained in:
Jan De Dobbeleer 2024-06-27 12:46:29 +02:00
parent cbf8a31e72
commit 544a05d458
No known key found for this signature in database
GPG key ID: D9FE64756B9A61E6
2 changed files with 233 additions and 4 deletions

View file

@ -61,6 +61,8 @@ const (
Letter string = "letter"
// Unique like agnoster, but with the first unique letters of each folder name
Unique string = "unique"
// AgnosterRight like agnoster, but keeps the right side of the path
AgnosterRight string = "agnoster_right"
// AgnosterLeft like agnoster, but keeps the left side of the path
AgnosterLeft string = "agnoster_left"
// Powerlevel tries to mimic the powerlevel10k path,
@ -183,7 +185,7 @@ func (pt *Path) setStyle() {
pt.Path = pt.root
if strings.HasSuffix(pt.Path, ":") {
pt.Path += pt.getFolderSeparator()
pt.Path += pt.pathSeparator
}
return
@ -204,6 +206,9 @@ func (pt *Path) setStyle() {
pt.Path = pt.getUniqueLettersPath(0)
case AgnosterLeft:
pt.Path = pt.getAgnosterLeftPath()
case AgnosterRight:
maxWidth := pt.getMaxWidth()
pt.Path = pt.getAgnosterRightPath(maxWidth)
case Short:
// "short" is a duplicate of "full", just here for backwards compatibility
fallthrough
@ -326,6 +331,59 @@ func (pt *Path) getAgnosterPath() string {
return pt.colorizePath(pt.root, elements)
}
func (pt *Path) getAgnosterRightPath(maxWidth int) string {
folderIcon := pt.props.GetString(FolderIcon, "..")
if pt.root == pt.pathSeparator {
pt.root = pt.Folders[0].Name
pt.Folders = pt.Folders[1:]
}
elements := []string{pt.root}
elements = append(elements, pt.Folders.List()...)
if maxWidth <= 0 {
return pt.colorizePath(pt.root, elements[1:])
}
n := len(pt.Folders)
separator := pt.getFolderSeparator()
for i := n - 2; i >= 0; i-- {
if len(strings.Join(elements, separator)) >= maxWidth {
break
}
elements[i] = folderIcon
}
// for i := 0; i < n; i++ {
// elements = append(elements, folderIcon)
// }
// if n > 0 {
// elements = append(elements, pt.Folders[n-1].Name)
// }
// if n <= 1 {
// return pt.colorizePath(pt.root, elements)
// }
// separator := pt.getFolderSeparator()
// // loop folders backwards
// for i := n - 2; i >= 0; i-- {
// if len(strings.Join(elements, separator)) <= maxWidth {
// elements[i] = pt.Folders[i].Name
// continue
// }
// break
// }
return pt.colorizePath(pt.root, elements[1:])
}
func (pt *Path) getAgnosterLeftPath() string {
folderIcon := pt.props.GetString(FolderIcon, "..")

View file

@ -346,7 +346,7 @@ func TestAgnosterPathStyles(t *testing.T) {
},
{
Style: Letter,
Expected: "C: > ",
Expected: "C:\\",
HomePath: homeDirWindows,
Pwd: "C:\\",
GOOS: platform.WINDOWS,
@ -645,7 +645,7 @@ func TestAgnosterPathStyles(t *testing.T) {
},
{
Style: AgnosterShort,
Expected: "C: | ",
Expected: "C:/",
HomePath: homeDir,
Pwd: "/mnt/c",
Pswd: "C:",
@ -676,7 +676,7 @@ func TestAgnosterPathStyles(t *testing.T) {
},
{
Style: AgnosterShort,
Expected: "C: > ",
Expected: "C:\\",
HomePath: homeDirWindows,
Pwd: "C:",
GOOS: platform.WINDOWS,
@ -1390,6 +1390,177 @@ func TestAgnosterLeftPath(t *testing.T) {
}
}
func TestAgnosterRightPath(t *testing.T) {
cases := []struct {
Case string
Expected string
Home string
PWD string
GOOS string
MaxWidth int
PathSeparator string
}{
{
Case: "Windows inside home, no max width",
Expected: "~ > Documents > Bill > location",
Home: homeDirWindows,
PWD: homeDirWindows + "\\Documents\\Bill\\location",
GOOS: platform.WINDOWS,
PathSeparator: "\\",
},
{
Case: "Windows inside home zero levels, no max width",
Expected: "C: > location",
Home: homeDirWindows,
PWD: "C:\\location",
GOOS: platform.WINDOWS,
PathSeparator: "\\",
},
{
Case: "Windows root drive, no max width",
Expected: "C:\\",
Home: homeDirWindows,
PWD: "C:\\",
GOOS: platform.WINDOWS,
PathSeparator: "\\",
},
{
Case: "Windows inside home one level, maxwidth 24",
Expected: "C: > Program Files > location",
Home: homeDirWindows,
PWD: "C:\\Program Files\\location",
GOOS: platform.WINDOWS,
PathSeparator: "\\",
MaxWidth: 24,
},
{
Case: "Windows inside home one level, maxwidth 20",
Expected: "C: > f > location",
Home: homeDirWindows,
PWD: "C:\\Program Files\\location",
GOOS: platform.WINDOWS,
PathSeparator: "\\",
MaxWidth: 20,
},
// {
// Case: "Windows lower case drive letter",
// Expected: "C: > Windows",
// Home: homeDirWindows,
// PWD: "C:\\Windows\\",
// GOOS: platform.WINDOWS,
// PathSeparator: "\\",
// },
// {
// Case: "Windows lower case drive letter (other)",
// Expected: "P: > Other",
// Home: homeDirWindows,
// PWD: "P:\\Other\\",
// GOOS: platform.WINDOWS,
// PathSeparator: "\\",
// },
// {
// Case: "Windows lower word drive",
// Expected: "some: > some",
// Home: homeDirWindows,
// PWD: "some:\\some\\",
// GOOS: platform.WINDOWS,
// PathSeparator: "\\",
// },
// {
// Case: "Windows lower word drive (ending with c)",
// Expected: "src: > source",
// Home: homeDirWindows,
// PWD: "src:\\source\\",
// GOOS: platform.WINDOWS,
// PathSeparator: "\\",
// },
// {
// Case: "Windows lower word drive (arbitrary cases)",
// Expected: "sRc: > source",
// Home: homeDirWindows,
// PWD: "sRc:\\source\\",
// GOOS: platform.WINDOWS,
// PathSeparator: "\\",
// },
// {
// Case: "Windows registry drive",
// Expected: "\uf013 > SOFTWARE > f",
// Home: homeDirWindows,
// PWD: "HKLM:\\SOFTWARE\\magnetic:test\\",
// GOOS: platform.WINDOWS,
// PathSeparator: "\\",
// },
// {
// Case: "Windows registry drive case sensitive",
// Expected: "\uf013 > SOFTWARE > f",
// Home: homeDirWindows,
// PWD: "HKLM:\\SOFTWARE\\magnetic:TOAST\\",
// GOOS: platform.WINDOWS,
// PathSeparator: "\\",
// },
// {
// Case: "Unix outside home",
// Expected: "mnt > go > f > f",
// Home: homeDir,
// PWD: "/mnt/go/test/location",
// PathSeparator: "/",
// },
// {
// Case: "Unix inside home",
// Expected: "~ > docs > f > f",
// Home: homeDir,
// PWD: homeDir + "/docs/jan/location",
// PathSeparator: "/",
// },
// {
// Case: "Unix outside home zero levels",
// Expected: "mnt > location",
// Home: homeDir,
// PWD: "/mnt/location",
// PathSeparator: "/",
// },
// {
// Case: "Unix outside home one level",
// Expected: "mnt > folder > f",
// Home: homeDir,
// PWD: "/mnt/folder/location",
// PathSeparator: "/",
// },
}
for _, tc := range cases {
env := new(mock.MockedEnvironment)
env.On("Home").Return(tc.Home)
env.On("PathSeparator").Return(tc.PathSeparator)
env.On("Pwd").Return(tc.PWD)
env.On("GOOS").Return(tc.GOOS)
args := &platform.Flags{
PSWD: tc.PWD,
}
env.On("Flags").Return(args)
env.On("Shell").Return(shell.PWSH)
env.On("DebugF", mock2.Anything, mock2.Anything).Return(nil)
path := &Path{
env: env,
props: properties.Map{
properties.Style: AgnosterRight,
FolderSeparatorIcon: " > ",
FolderIcon: "f",
HomeIcon: "~",
MaxWidth: tc.MaxWidth,
},
}
path.setPaths()
path.setStyle()
got := renderTemplateNoTrimSpace(env, "{{ .Path }}", path)
assert.Equal(t, tc.Expected, got, tc.Case)
}
}
func TestGetPwd(t *testing.T) {
cases := []struct {
MappedLocationsEnabled bool