feat: add hide root to agnoster short

This commit is contained in:
jedwillick 2022-03-01 15:39:52 +10:00 committed by Jan De Dobbeleer
parent 6a745ef6c3
commit e075f76718
4 changed files with 52 additions and 6 deletions

View file

@ -35,7 +35,9 @@ Display the current path.
- style: `enum` - how to display the current path
- mixed_threshold: `number` - the maximum length of a path segment that will be displayed when using `Mixed` -
defaults to `4`
- max_depth: `number` - maximum path depth to display before shortening when using `Agnoster Short` - defaults to `1`
- max_depth: `number` - maximum path depth to display before shortening when using `agnoster_short` - defaults to `1`
- hide_root_location: `boolean` - hides the root location if it doesn't fit in the last `max_depth` folders, when using
`agnoster_short` - defaults to `false`
## Mapped Locations
@ -91,8 +93,8 @@ Renders each folder name separated by the `folder_separator_icon`.
### Agnoster Short
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`.
When more than `max_depth` levels deep, it renders one `folder_icon` (if `hide_root_location` is `false`) followed by
the names of the last `max_depth` folders, separated by the `folder_separator_icon`.
### Agnoster Left

View file

@ -56,6 +56,8 @@ const (
MappedLocationsEnabled properties.Property = "mapped_locations_enabled"
// MaxDepth Maximum path depth to display whithout shortening
MaxDepth properties.Property = "max_depth"
// Hides the root location if it doesn't fit in max_depth. Used in Agnoster Short
HideRootLocation properties.Property = "hide_root_location"
)
func (pt *Path) Template() string {
@ -249,18 +251,28 @@ func (pt *Path) getAgnosterShortPath() string {
if maxDepth < 1 {
maxDepth = 1
}
hideRootLocation := pt.props.GetBool(HideRootLocation, false)
if hideRootLocation {
// 1-indexing to avoid showing the root location when exceeding the max depth
pathDepth++
}
if pathDepth <= maxDepth {
return pt.getAgnosterFullPath()
}
pathSeparator := pt.env.PathSeparator()
folderSeparator := pt.props.GetString(FolderSeparatorIcon, pathSeparator)
folderIcon := pt.props.GetString(FolderIcon, "..")
root := pt.rootLocation()
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))
if hideRootLocation {
buffer.WriteString(splitted[splitPos])
splitPos++
} else {
folderIcon := pt.props.GetString(FolderIcon, "..")
root := pt.rootLocation()
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]))
}

View file

@ -170,6 +170,7 @@ func TestAgnosterPathStyles(t *testing.T) {
Style string
GOOS string
MaxDepth int
HideRootLocation bool
}{
{Style: AgnosterFull, Expected: "usr > location > whatever", HomePath: "/usr/home", Pwd: "/usr/location/whatever", PathSeparator: "/", FolderSeparatorIcon: " > "},
{Style: AgnosterShort, Expected: "usr > .. > man", HomePath: "/usr/home", Pwd: "/usr/location/whatever/man", PathSeparator: "/", FolderSeparatorIcon: " > "},
@ -224,6 +225,30 @@ func TestAgnosterPathStyles(t *testing.T) {
{Style: AgnosterFull, Expected: "PSDRIVE: | src", HomePath: homeBillWindows, Pwd: "/foo", Pswd: "PSDRIVE:/src", PathSeparator: "/", FolderSeparatorIcon: " | "},
{Style: AgnosterShort, Expected: "PSDRIVE: | .. | init", HomePath: homeBillWindows, Pwd: "/foo", Pswd: "PSDRIVE:/src/init", PathSeparator: "/", FolderSeparatorIcon: " | "},
{Style: AgnosterShort, Expected: "src | init", HomePath: homeBillWindows, Pwd: "/foo", Pswd: "PSDRIVE:/src/init", PathSeparator: "/", FolderSeparatorIcon: " | ", MaxDepth: 2,
HideRootLocation: true},
{Style: AgnosterShort, Expected: "PSDRIVE: | src", HomePath: homeBillWindows, Pwd: "/foo", Pswd: "PSDRIVE:/src", PathSeparator: "/", FolderSeparatorIcon: " | ", MaxDepth: 2,
HideRootLocation: true},
{Style: AgnosterShort, Expected: "~", HomePath: homeBillWindows, Pwd: homeBillWindows, PathSeparator: "\\", FolderSeparatorIcon: " > ", MaxDepth: 1, HideRootLocation: true},
{Style: AgnosterShort, Expected: "foo", HomePath: homeBillWindows, Pwd: homeBillWindows + "\\foo", PathSeparator: "\\", FolderSeparatorIcon: "\\", MaxDepth: 1,
HideRootLocation: true},
{Style: AgnosterShort, Expected: "~\\foo", HomePath: homeBillWindows, Pwd: homeBillWindows + "\\foo", PathSeparator: "\\", FolderSeparatorIcon: "\\", MaxDepth: 2,
HideRootLocation: true},
{Style: AgnosterShort, Expected: "~", HomePath: "/usr/home", Pwd: "/usr/home", PathSeparator: "/", FolderSeparatorIcon: " > ", MaxDepth: 1, HideRootLocation: true},
{Style: AgnosterShort, Expected: "foo", HomePath: "/usr/home", Pwd: "/usr/home/foo", PathSeparator: "/", FolderSeparatorIcon: "/", MaxDepth: 1, HideRootLocation: true},
{Style: AgnosterShort, Expected: "bar > man", HomePath: "/usr/home", Pwd: "/usr/foo/bar/man", PathSeparator: "/", FolderSeparatorIcon: " > ", MaxDepth: 2,
HideRootLocation: true},
{Style: AgnosterShort, Expected: "foo > bar > man", HomePath: "/usr/home", Pwd: "/usr/foo/bar/man", PathSeparator: "/", FolderSeparatorIcon: " > ", MaxDepth: 3,
HideRootLocation: true},
{Style: AgnosterShort, Expected: "~ > foo", HomePath: "/usr/home", Pwd: "/usr/home/foo", PathSeparator: "/", FolderSeparatorIcon: " > ", MaxDepth: 2, HideRootLocation: true},
{Style: AgnosterShort, Expected: "~ > foo > bar > man", HomePath: "/usr/home", Pwd: "/usr/home/foo/bar/man", PathSeparator: "/", FolderSeparatorIcon: " > ", MaxDepth: 4,
HideRootLocation: true},
{Style: AgnosterShort, Expected: "C:", HomePath: "/usr/home", Pwd: "/mnt/c", Pswd: "C:", PathSeparator: "/", FolderSeparatorIcon: " | ", MaxDepth: 2, HideRootLocation: true},
{Style: AgnosterShort, Expected: "~ | space foo", HomePath: "/usr/home", Pwd: "/usr/home/space foo", PathSeparator: "/", FolderSeparatorIcon: " | ", MaxDepth: 2,
HideRootLocation: true},
{Style: AgnosterShort, Expected: "space foo", HomePath: "/usr/home", Pwd: "/usr/home/space foo", PathSeparator: "/", FolderSeparatorIcon: " | ", MaxDepth: 1,
HideRootLocation: true},
{Style: Mixed, Expected: "~ > .. > man", HomePath: "/usr/home", Pwd: "/usr/home/whatever/man", PathSeparator: "/", FolderSeparatorIcon: " > "},
{Style: Mixed, Expected: "~ > ab > .. > man", HomePath: "/usr/home", Pwd: "/usr/home/ab/whatever/man", PathSeparator: "/", FolderSeparatorIcon: " > "},
@ -261,6 +286,7 @@ func TestAgnosterPathStyles(t *testing.T) {
FolderSeparatorIcon: tc.FolderSeparatorIcon,
properties.Style: tc.Style,
MaxDepth: tc.MaxDepth,
HideRootLocation: tc.HideRootLocation,
},
}
_ = path.Enabled()

View file

@ -1157,6 +1157,12 @@
"title": "Mixed threshold",
"description": "The maximum length of a path segment that will be displayed when using mixed style.",
"default": 4
},
"hide_root_location": {
"type": "boolean",
"title": "Hide the root location",
"description": "Hides the root location, when using agnoster_short style, if it doesn't fit in the last max_depth folders.",
"default": false
}
}
}