diff --git a/docs/docs/segment-path.md b/docs/docs/segment-path.md index c81fe3fa..97d2f81c 100644 --- a/docs/docs/segment-path.md +++ b/docs/docs/segment-path.md @@ -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 diff --git a/src/segment_path.go b/src/segment_path.go index a3504958..b66d3c62 100644 --- a/src/segment_path.go +++ b/src/segment_path.go @@ -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 { diff --git a/src/segment_path_test.go b/src/segment_path_test.go index 56a941c3..db395bfc 100644 --- a/src/segment_path_test.go +++ b/src/segment_path_test.go @@ -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, }, }, } diff --git a/themes/schema.json b/themes/schema.json index f2e23301..fd34d0f1 100644 --- a/themes/schema.json +++ b/themes/schema.json @@ -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": {