feat(path): add powerlevel style

resolves #3607
This commit is contained in:
Jan De Dobbeleer 2023-03-21 20:40:10 +01:00 committed by Jan De Dobbeleer
parent 6e874f97d8
commit 6af47a61d5
4 changed files with 50 additions and 4 deletions

View file

@ -58,6 +58,9 @@ const (
Unique string = "unique"
// AgnosterLeft like agnoster, but keeps the left side of the path
AgnosterLeft string = "agnoster_left"
// Powerlevel tries to mimic the powerlevel10k path,
// used in combination with max_width.
Powerlevel string = "powerlevel"
// MixedThreshold the threshold of the length of the path Mixed will display
MixedThreshold properties.Property = "mixed_threshold"
// MappedLocations allows overriding certain location with an icon
@ -66,6 +69,8 @@ const (
MappedLocationsEnabled properties.Property = "mapped_locations_enabled"
// MaxDepth Maximum path depth to display whithout shortening
MaxDepth properties.Property = "max_depth"
// MaxWidth Maximum path width to display for powerlevel style
MaxWidth properties.Property = "max_width"
// Hides the root location if it doesn't fit in max_depth. Used in Agnoster Short
HideRootLocation properties.Property = "hide_root_location"
)
@ -152,7 +157,7 @@ func (pt *Path) setStyle() {
case Letter:
pt.Path = pt.getLetterPath()
case Unique:
pt.Path = pt.getUniqueLettersPath()
pt.Path = pt.getUniqueLettersPath(0)
case AgnosterLeft:
pt.Path = pt.getAgnosterLeftPath()
case Short:
@ -162,6 +167,9 @@ func (pt *Path) setStyle() {
pt.Path = pt.getFullPath()
case Folder:
pt.Path = pt.getFolderPath()
case Powerlevel:
maxWidth := int(pt.props.GetFloat64(MaxWidth, 0))
pt.Path = pt.getUniqueLettersPath(maxWidth)
default:
pt.Path = fmt.Sprintf("Path style: %s is not available", style)
}
@ -294,7 +302,7 @@ func (pt *Path) getLetterPath() string {
return buffer.String()
}
func (pt *Path) getUniqueLettersPath() string {
func (pt *Path) getUniqueLettersPath(maxWidth int) string {
var buffer strings.Builder
separator := pt.getFolderSeparator()
elements := strings.Split(pt.relative, pt.env.PathSeparator())
@ -317,6 +325,16 @@ func (pt *Path) getUniqueLettersPath() string {
buffer.WriteString(separator)
}
buffer.WriteString(letter)
// only return early on maxWidth > 0
// this enables the powerlevel10k behavior
if maxWidth > 0 {
trailing := strings.Join(elements[i+1:], separator)
leftover := maxWidth - buffer.Len() - len(trailing) - len(separator)
if leftover >= 0 {
buffer.WriteString(fmt.Sprintf("%s%s", separator, trailing))
return buffer.String()
}
}
}
buffer.WriteString(fmt.Sprintf("%s%s", separator, elements[n-1]))
return buffer.String()

View file

@ -162,6 +162,7 @@ func TestAgnosterPathStyles(t *testing.T) {
FolderSeparatorIcon string
GOOS string
MaxDepth int
MaxWidth int
HideRootLocation bool
}{
{
@ -198,6 +199,25 @@ func TestAgnosterPathStyles(t *testing.T) {
FolderSeparatorIcon: " > ",
},
{
Style: Powerlevel,
Expected: "t > w > o > a > v > l > p > wh > we > i > wa > th > the > d > f > u > it > c > to > a > co > stream",
HomePath: homeDir,
Pwd: "there/was/once/a/very/long/path/which/wended/its/way/through/the/dark/forest/until/it/came/to/a/cold/stream",
PathSeparator: "/",
FolderSeparatorIcon: " > ",
MaxWidth: 20,
},
{
Style: Powerlevel,
Expected: "t > w > o > a > v > l > p > which > wended > its > way > through > the",
HomePath: homeDir,
Pwd: "there/was/once/a/very/long/path/which/wended/its/way/through/the",
PathSeparator: "/",
FolderSeparatorIcon: " > ",
MaxWidth: 70,
},
{
Style: Letter,
Expected: "~",
@ -696,6 +716,7 @@ func TestAgnosterPathStyles(t *testing.T) {
FolderSeparatorIcon: tc.FolderSeparatorIcon,
properties.Style: tc.Style,
MaxDepth: tc.MaxDepth,
MaxWidth: tc.MaxWidth,
HideRootLocation: tc.HideRootLocation,
},
}

View file

@ -1740,7 +1740,8 @@
"folder",
"mixed",
"letter",
"unique"
"unique",
"powerlevel"
],
"default": "folder"
},

View file

@ -38,6 +38,7 @@ 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_width` | `number` | maximum path length to display when using `powerlevel`, defaults to `0` |
| `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
@ -74,7 +75,7 @@ For example, to swap out `C:\Users\Leet\GitHub` with a GitHub icon, you can do t
## Style
Style sets the way the path is displayed. Based on previous experience and popular themes, there are 5 flavors.
Style sets the way the path is displayed. Based on previous experience and popular themes, there are 10 flavors.
- `agnoster`
- `agnoster_full`
@ -85,6 +86,7 @@ Style sets the way the path is displayed. Based on previous experience and popul
- `mixed`
- `letter`
- `unique`
- `powerlevel`
### Agnoster
@ -138,6 +140,10 @@ The uniqueness refers to the displayed path, so `C:\dev\dev\dev\development` wil
folders at the same level, so if `C:\projectA\dev` and `C:\projectB\dev` exist, then both will be displayed as
`C\p\dev`.
### Powerlevel
Works like `Unique`, but will stop shortening when `max_width` is reached.
## Template ([info][templates])
:::note default template