Skip to content

Commit

Permalink
feat: customizable short path
Browse files Browse the repository at this point in the history
  • Loading branch information
lnu authored and JanDeDobbeleer committed Nov 20, 2020
1 parent 45b50fb commit 77cd537
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
7 changes: 6 additions & 1 deletion docs/docs/segment-path.md
Expand Up @@ -18,7 +18,10 @@ Display the current path.
"foreground": "#ffffff",
"background": "#61AFEF",
"properties": {
"style": "folder"
"style": "folder",
"mappedlocations": [
["C:\\temp", "\ue799"]
]
}
}
```
Expand All @@ -30,6 +33,7 @@ Display the current path.
- folder_icon: `string` - the icon to use as a folder indication - defaults to `..`
- windows_registry_icon: `string` - the icon to display when in the Windows registry - defaults to `\uE0B1`
- style: `enum` - how to display the current path
- mappedlocations: `[]string` - Custom glyph/text for specific paths(only when `style` is set to `agnoster`, `agnosterfull` or `short`)

## Style

Expand All @@ -55,6 +59,7 @@ Renders each folder name separated by the `folder_separator_icon`.

Display `$PWD` as a string, replace `$HOME` with the `home_icon` if you're inside the `$HOME` location or
one of its children.
Specific folders can be customized using the `mappedlocations` property.

### Full

Expand Down
31 changes: 31 additions & 0 deletions properties.go
Expand Up @@ -85,6 +85,20 @@ func (p *properties) getBool(property Property, defaultValue bool) bool {
return boolValue
}

func (p *properties) getKeyValueMap(property Property, defaultValue map[string]string) map[string]string {
if p == nil || p.values == nil {
return defaultValue
}
val, found := p.values[property]
if !found {
return defaultValue
}

keyValues := parseKeyValueArray(val)

return keyValues
}

func parseStringArray(value interface{}) []string {
expectedValue, ok := value.([]interface{})
if !ok {
Expand All @@ -96,3 +110,20 @@ func parseStringArray(value interface{}) []string {
}
return list
}

func parseKeyValueArray(value interface{}) map[string]string {
locations, ok := value.([]interface{})
if !ok {
return map[string]string{}
}
keyValueArray := make(map[string]string)
for _, s := range locations {
l := parseStringArray(s)
if len(l) == 2 {
key := l[0]
val := l[1]
keyValueArray[key] = val
}
}
return keyValueArray
}
9 changes: 9 additions & 0 deletions segment_path.go
Expand Up @@ -64,11 +64,20 @@ func (pt *path) getShortPath() string {
if strings.HasPrefix(pwd, "Microsoft.PowerShell.Core\\FileSystem::") {
pwd = strings.Replace(pwd, "Microsoft.PowerShell.Core\\FileSystem::", "", 1)
}

mappedLocations := map[string]string{
"HKCU:": pt.props.getString(WindowsRegistryIcon, "\uE0B1"),
"HKLM:": pt.props.getString(WindowsRegistryIcon, "\uE0B1"),
pt.env.homeDir(): pt.props.getString(HomeIcon, "~"),
}

// merge custom locations with mapped locations
// mapped locations can override predefined locations
keyValues := pt.props.getKeyValueMap("mappedlocations", make(map[string]string))
for key, val := range keyValues {
mappedLocations[key] = val
}

for location, value := range mappedLocations {
if strings.HasPrefix(pwd, location) {
return strings.Replace(pwd, location, value, 1)
Expand Down

0 comments on commit 77cd537

Please sign in to comment.