diff --git a/docs/docs/segment-path.md b/docs/docs/segment-path.md index c202f46b75d8..841f051aaffe 100644 --- a/docs/docs/segment-path.md +++ b/docs/docs/segment-path.md @@ -18,7 +18,10 @@ Display the current path. "foreground": "#ffffff", "background": "#61AFEF", "properties": { - "style": "folder" + "style": "folder", + "mappedlocations": [ + ["C:\\temp", "\ue799"] + ] } } ``` @@ -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 @@ -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 diff --git a/properties.go b/properties.go index 045b224db39e..1fd2af5d130b 100644 --- a/properties.go +++ b/properties.go @@ -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 { @@ -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 +} diff --git a/segment_path.go b/segment_path.go index 05f0750572dc..541dc219358d 100644 --- a/segment_path.go +++ b/segment_path.go @@ -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)