feat(path): add 'max_depth' option for 'agnoster_short'

This commit is contained in:
Sebastian Mineur 2021-08-20 17:06:37 +02:00 committed by GitHub
parent 2470abcc86
commit 2b53796c1a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 72 additions and 10 deletions

View file

@ -38,6 +38,7 @@ Display the current path.
- mixed_threshold: `number` - the maximum length of a path segment that will be displayed when using `Mixed` -
defaults to `4`
- stack_count_enabled: `boolean` - displays the stack count when using pushd/popd - defaults to `false`
- max_depth: `number` - maximum path depth to display before shortening when using `Agnoster Short` - defaults to `1`
## Mapped Locations
@ -82,7 +83,8 @@ Renders each folder name separated by the `folder_separator_icon`.
### Agnoster Short
When more than 1 level deep, it renders one `folder_icon` followed by the name of the current folder separated by the `folder_separator_icon`.
When more than `max_depth` levels deep, it renders one `folder_icon` followed by the names of the last `max_depth` folders,
separated by the `folder_separator_icon`.
### Full

View file

@ -45,6 +45,8 @@ const (
MappedLocationsEnabled Property = "mapped_locations_enabled"
// StackCountEnabled enables the stack count display
StackCountEnabled Property = "stack_count_enabled"
// Maximum path depth to display whithout shortening
MaxDepth Property = "max_depth"
)
func (pt *path) enabled() bool {
@ -174,20 +176,28 @@ func (pt *path) getAgnosterFullPath() string {
}
func (pt *path) getAgnosterShortPath() string {
pwd := pt.getPwd()
pathDepth := pt.pathDepth(pwd)
maxDepth := pt.props.getInt(MaxDepth, 1)
if maxDepth < 1 {
maxDepth = 1
}
if pathDepth <= maxDepth {
return pt.getAgnosterFullPath()
}
pathSeparator := pt.env.getPathSeperator()
folderSeparator := pt.props.getString(FolderSeparatorIcon, pathSeparator)
folderIcon := pt.props.getString(FolderIcon, "..")
root := pt.rootLocation()
pwd := pt.getPwd()
base := base(pwd, pt.env)
pathDepth := pt.pathDepth(pwd)
if pathDepth <= 0 {
return root
splitted := strings.Split(pwd, pathSeparator)
fullPathDepth := len(splitted)
splitPos := fullPathDepth - maxDepth
var buffer strings.Builder
buffer.WriteString(fmt.Sprintf("%s%s%s", root, folderSeparator, folderIcon))
for i := splitPos; i < fullPathDepth; i++ {
buffer.WriteString(fmt.Sprintf("%s%s", folderSeparator, splitted[i]))
}
if pathDepth == 1 {
return fmt.Sprintf("%s%s%s", root, folderSeparator, base)
}
return fmt.Sprintf("%s%s%s%s%s", root, folderSeparator, folderIcon, folderSeparator, base)
return buffer.String()
}
func (pt *path) getFullPath() string {

View file

@ -282,6 +282,7 @@ func TestAgnosterPathStyles(t *testing.T) {
FolderSeparatorIcon string
Style string
GOOS string
MaxDepth int
}{
{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: " > "},
@ -291,6 +292,48 @@ func TestAgnosterPathStyles(t *testing.T) {
{Style: AgnosterShort, Expected: "", HomePath: homeBillWindows, Pwd: "/", PathSeperator: "/", FolderSeparatorIcon: " > "},
{Style: AgnosterShort, Expected: "foo", HomePath: homeBillWindows, Pwd: "/foo", PathSeperator: "/", FolderSeparatorIcon: " > "},
{Style: AgnosterShort, Expected: "usr > .. > bar > man", HomePath: "/usr/home", Pwd: "/usr/foo/bar/man", PathSeperator: "/", FolderSeparatorIcon: " > ", MaxDepth: 2},
{Style: AgnosterShort, Expected: "usr > foo > bar > man", HomePath: "/usr/home", Pwd: "/usr/foo/bar/man", PathSeperator: "/", FolderSeparatorIcon: " > ", MaxDepth: 3},
{Style: AgnosterShort, Expected: "~ > .. > bar > man", HomePath: "/usr/home", Pwd: "/usr/home/foo/bar/man", PathSeperator: "/", FolderSeparatorIcon: " > ", MaxDepth: 2},
{Style: AgnosterShort, Expected: "~ > foo > bar > man", HomePath: "/usr/home", Pwd: "/usr/home/foo/bar/man", PathSeperator: "/", FolderSeparatorIcon: " > ", MaxDepth: 3},
{
Style: AgnosterShort,
Expected: "C: > .. > bar > man",
HomePath: homeBillWindows,
Pwd: "C:\\usr\\foo\\bar\\man",
PathSeperator: "\\",
FolderSeparatorIcon: " > ",
MaxDepth: 2,
},
{
Style: AgnosterShort,
Expected: "C: > .. > foo > bar > man",
HomePath: homeBillWindows,
Pwd: "C:\\usr\\foo\\bar\\man",
PathSeperator: "\\",
FolderSeparatorIcon: " > ",
MaxDepth: 3,
},
{
Style: AgnosterShort,
Expected: "~ > .. > bar > man",
HomePath: homeBillWindows,
Pwd: "C:\\Users\\Bill\\foo\\bar\\man",
PathSeperator: "\\",
FolderSeparatorIcon: " > ",
MaxDepth: 2,
},
{
Style: AgnosterShort,
Expected: "~ > foo > bar > man",
HomePath: homeBillWindows,
Pwd: "C:\\Users\\Bill\\foo\\bar\\man",
PathSeperator: "\\",
FolderSeparatorIcon: " > ",
MaxDepth: 3,
},
{Style: AgnosterFull, Expected: "PSDRIVE: | src", HomePath: homeBillWindows, Pwd: "/foo", Pswd: "PSDRIVE:/src", PathSeperator: "/", FolderSeparatorIcon: " | "},
{Style: AgnosterShort, Expected: "PSDRIVE: | .. | init", HomePath: homeBillWindows, Pwd: "/foo", Pswd: "PSDRIVE:/src/init", PathSeperator: "/", FolderSeparatorIcon: " | "},
@ -318,6 +361,7 @@ func TestAgnosterPathStyles(t *testing.T) {
values: map[Property]interface{}{
FolderSeparatorIcon: tc.FolderSeparatorIcon,
Style: tc.Style,
MaxDepth: tc.MaxDepth,
},
},
}

View file

@ -1232,6 +1232,12 @@
"title": "Mapped Locations",
"description": "Custom glyph/text for specific paths",
"default": {}
},
"max_depth": {
"type": "integer",
"title": "Maximum Depth",
"description": "Maximum path depth to display whithout shortening",
"default": 1
}
},
"mapped_locations_enabled": {